Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 :
21 : #include <tools/datetime.hxx>
22 : #include <svl/zforlist.hxx>
23 : #include <com/sun/star/util/DateTime.hpp>
24 : #include <doc.hxx>
25 : #include <fldbas.hxx>
26 : #include <flddat.hxx>
27 : #include <unofldmid.h>
28 :
29 : using namespace ::com::sun::star;
30 : /*--------------------------------------------------
31 : Beschreibung: Datum/Zeit-Typ
32 : ---------------------------------------------------*/
33 :
34 276 : SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
35 276 : : SwValueFieldType( pInitDoc, RES_DATETIMEFLD )
36 276 : {}
37 :
38 0 : SwFieldType* SwDateTimeFieldType::Copy() const
39 : {
40 0 : SwDateTimeFieldType *pTmp = new SwDateTimeFieldType(GetDoc());
41 0 : return pTmp;
42 : }
43 :
44 : /*--------------------------------------------------------------------
45 : Beschreibung: Datum/Zeit-Feld
46 : --------------------------------------------------------------------*/
47 :
48 5 : SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, sal_uInt16 nSub, sal_uLong nFmt, sal_uInt16 nLng)
49 : : SwValueField(pInitType, nFmt, nLng, 0.0),
50 : nSubType(nSub),
51 5 : nOffset(0)
52 : {
53 5 : if (!nFmt)
54 : {
55 0 : SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
56 0 : if (nSubType & DATEFLD)
57 0 : ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
58 : else
59 0 : ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
60 : }
61 5 : if (IsFixed())
62 : {
63 0 : DateTime aDateTime( DateTime::SYSTEM );
64 0 : SetDateTime(aDateTime);
65 : }
66 5 : }
67 :
68 2 : String SwDateTimeField::Expand() const
69 : {
70 : double fVal;
71 :
72 2 : if (!(IsFixed()))
73 : {
74 2 : DateTime aDateTime( DateTime::SYSTEM );
75 2 : fVal = GetDateTime(GetDoc(), aDateTime);
76 : }
77 : else
78 0 : fVal = GetValue();
79 :
80 2 : if (nOffset)
81 0 : fVal += (double)(nOffset * 60L) / 86400.0;
82 :
83 2 : return ExpandValue(fVal, GetFormat(), GetLanguage());
84 : }
85 :
86 4 : SwField* SwDateTimeField::Copy() const
87 : {
88 : SwDateTimeField *pTmp =
89 4 : new SwDateTimeField((SwDateTimeFieldType*)GetTyp(), nSubType,
90 4 : GetFormat(), GetLanguage());
91 :
92 4 : pTmp->SetValue(GetValue());
93 4 : pTmp->SetOffset(nOffset);
94 4 : pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
95 :
96 4 : return pTmp;
97 : }
98 :
99 11 : sal_uInt16 SwDateTimeField::GetSubType() const
100 : {
101 11 : return nSubType;
102 : }
103 :
104 0 : void SwDateTimeField::SetSubType(sal_uInt16 nType)
105 : {
106 0 : nSubType = nType;
107 0 : }
108 :
109 0 : void SwDateTimeField::SetPar2(const rtl::OUString& rStr)
110 : {
111 0 : nOffset = rStr.toInt32();
112 0 : }
113 :
114 0 : rtl::OUString SwDateTimeField::GetPar2() const
115 : {
116 0 : if (nOffset)
117 0 : return rtl::OUString::valueOf(static_cast<sal_Int32>(nOffset));
118 0 : return rtl::OUString();
119 : }
120 :
121 0 : void SwDateTimeField::SetDateTime(const DateTime& rDT)
122 : {
123 0 : SetValue(GetDateTime(GetDoc(), rDT));
124 0 : }
125 :
126 6 : double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
127 : {
128 6 : SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
129 6 : Date* pNullDate = pFormatter->GetNullDate();
130 :
131 6 : double fResult = rDT - DateTime(*pNullDate);
132 :
133 6 : return fResult;
134 : }
135 :
136 4 : double SwDateTimeField::GetValue() const
137 : {
138 4 : if (IsFixed())
139 0 : return SwValueField::GetValue();
140 : else
141 4 : return GetDateTime(GetDoc(), DateTime( DateTime::SYSTEM ));
142 : }
143 :
144 0 : Date SwDateTimeField::GetDate(sal_Bool bUseOffset) const
145 : {
146 0 : SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
147 0 : Date* pNullDate = pFormatter->GetNullDate();
148 :
149 0 : long nVal = static_cast<long>( GetValue() );
150 :
151 0 : if (bUseOffset && nOffset)
152 0 : nVal += nOffset / 60 / 24;
153 :
154 0 : Date aDate = *pNullDate + nVal;
155 :
156 0 : return aDate;
157 : }
158 :
159 0 : Time SwDateTimeField::GetTime(sal_Bool bUseOffset) const
160 : {
161 : double fDummy;
162 0 : double fFract = modf(GetValue(), &fDummy);
163 0 : DateTime aDT((long)fDummy, 0);
164 0 : aDT += fFract;
165 0 : if (bUseOffset)
166 0 : aDT += Time(0, nOffset);
167 0 : return (Time)aDT;
168 : }
169 :
170 1 : bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
171 : {
172 1 : switch( nWhichId )
173 : {
174 : case FIELD_PROP_BOOL1:
175 : {
176 0 : sal_Bool bTmp = IsFixed();
177 0 : rVal.setValue(&bTmp, ::getCppuBooleanType());
178 : }
179 0 : break;
180 : case FIELD_PROP_BOOL2:
181 : {
182 0 : sal_Bool bTmp = IsDate();
183 0 : rVal.setValue(&bTmp, ::getCppuBooleanType());
184 : }
185 0 : break;
186 : case FIELD_PROP_FORMAT:
187 1 : rVal <<= (sal_Int32)GetFormat();
188 1 : break;
189 : case FIELD_PROP_SUBTYPE:
190 0 : rVal <<= (sal_Int32)nOffset;
191 0 : break;
192 : case FIELD_PROP_DATE_TIME:
193 : {
194 0 : DateTime aDateTime(GetDate(), GetTime());
195 :
196 0 : util::DateTime DateTimeValue;
197 0 : DateTimeValue.HundredthSeconds = aDateTime.Get100Sec();
198 0 : DateTimeValue.Seconds = aDateTime.GetSec();
199 0 : DateTimeValue.Minutes = aDateTime.GetMin();
200 0 : DateTimeValue.Hours = aDateTime.GetHour();
201 0 : DateTimeValue.Day = aDateTime.GetDay();
202 0 : DateTimeValue.Month = aDateTime.GetMonth();
203 0 : DateTimeValue.Year = aDateTime.GetYear();
204 0 : rVal <<= DateTimeValue;
205 : }
206 0 : break;
207 : default:
208 0 : return SwField::QueryValue(rVal, nWhichId);
209 : }
210 1 : return true;
211 : }
212 :
213 0 : bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
214 : {
215 0 : sal_Int32 nTmp = 0;
216 0 : switch( nWhichId )
217 : {
218 : case FIELD_PROP_BOOL1:
219 0 : if(*(sal_Bool*)rVal.getValue())
220 0 : nSubType |= FIXEDFLD;
221 : else
222 0 : nSubType &= ~FIXEDFLD;
223 0 : break;
224 : case FIELD_PROP_BOOL2:
225 0 : nSubType &= ~(DATEFLD|TIMEFLD);
226 0 : nSubType |= *(sal_Bool*)rVal.getValue() ? DATEFLD : TIMEFLD;
227 0 : break;
228 : case FIELD_PROP_FORMAT:
229 0 : rVal >>= nTmp;
230 0 : ChangeFormat(nTmp);
231 0 : break;
232 : case FIELD_PROP_SUBTYPE:
233 0 : rVal >>= nTmp;
234 0 : nOffset = nTmp;
235 0 : break;
236 : case FIELD_PROP_DATE_TIME:
237 : {
238 0 : util::DateTime aDateTimeValue;
239 0 : if(!(rVal >>= aDateTimeValue))
240 0 : return false;
241 0 : DateTime aDateTime( DateTime::EMPTY );
242 0 : aDateTime.Set100Sec(aDateTimeValue.HundredthSeconds);
243 0 : aDateTime.SetSec(aDateTimeValue.Seconds);
244 0 : aDateTime.SetMin(aDateTimeValue.Minutes);
245 0 : aDateTime.SetHour(aDateTimeValue.Hours);
246 0 : aDateTime.SetDay(aDateTimeValue.Day);
247 0 : aDateTime.SetMonth(aDateTimeValue.Month);
248 0 : aDateTime.SetYear(aDateTimeValue.Year);
249 0 : SetDateTime(aDateTime);
250 : }
251 0 : break;
252 : default:
253 0 : return SwField::PutValue(rVal, nWhichId);
254 : }
255 0 : return true;
256 : }
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|