xref: /aoo42x/main/sw/source/core/fields/flddat.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 // #include <math.h>
28 #include <tools/datetime.hxx>
29 #include <svl/zforlist.hxx>
30 #include <com/sun/star/util/DateTime.hpp>
31 #include <doc.hxx>
32 #include <fldbas.hxx>
33 #include <flddat.hxx>
34 #include <unofldmid.h>
35 
36 using namespace ::com::sun::star;
37 /*--------------------------------------------------
38 	Beschreibung: Datum/Zeit-Typ
39  ---------------------------------------------------*/
40 
SwDateTimeFieldType(SwDoc * pInitDoc)41 SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
42 	: SwValueFieldType( pInitDoc, RES_DATETIMEFLD )
43 {}
44 
45 /*--------------------------------------------------------------------
46 	Beschreibung:
47  --------------------------------------------------------------------*/
48 
Copy() const49 SwFieldType* SwDateTimeFieldType::Copy() const
50 {
51 	SwDateTimeFieldType *pTmp = new SwDateTimeFieldType(GetDoc());
52 	return pTmp;
53 }
54 
55 /*--------------------------------------------------------------------
56 	Beschreibung: Datum/Zeit-Feld
57  --------------------------------------------------------------------*/
58 
SwDateTimeField(SwDateTimeFieldType * pInitType,sal_uInt16 nSub,sal_uLong nFmt,sal_uInt16 nLng)59 SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, sal_uInt16 nSub, sal_uLong nFmt, sal_uInt16 nLng)
60 	: SwValueField(pInitType, nFmt, nLng, 0.0),
61 	nSubType(nSub),
62 	nOffset(0)
63 {
64 	if (!nFmt)
65 	{
66 		SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
67 		if (nSubType & DATEFLD)
68 			ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
69 		else
70 			ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
71 	}
72 	if (IsFixed())
73 	{
74         DateTime aDateTime;
75         SetDateTime(aDateTime);
76 	}
77 }
78 
79 /*--------------------------------------------------------------------
80 	Beschreibung:
81  --------------------------------------------------------------------*/
82 
Expand() const83 String SwDateTimeField::Expand() const
84 {
85 	double fVal;
86 
87 	if (!(IsFixed()))
88 	{
89         DateTime aDateTime;
90         fVal = GetDateTime(GetDoc(), aDateTime);
91 	}
92 	else
93 		fVal = GetValue();
94 
95 	if (nOffset)
96 		fVal += (double)(nOffset * 60L) / 86400.0;
97 
98 	return ExpandValue(fVal, GetFormat(), GetLanguage());
99 }
100 
101 /*--------------------------------------------------------------------
102 	Beschreibung:
103  --------------------------------------------------------------------*/
104 
Copy() const105 SwField* SwDateTimeField::Copy() const
106 {
107 	SwDateTimeField *pTmp =
108 		new SwDateTimeField((SwDateTimeFieldType*)GetTyp(), nSubType,
109 											GetFormat(), GetLanguage());
110 
111 	pTmp->SetValue(GetValue());
112 	pTmp->SetOffset(nOffset);
113     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
114 
115 	return pTmp;
116 }
117 
118 /*--------------------------------------------------------------------
119 	Beschreibung:
120  --------------------------------------------------------------------*/
121 
GetSubType() const122 sal_uInt16 SwDateTimeField::GetSubType() const
123 {
124 	return nSubType;
125 }
126 
127 /*--------------------------------------------------------------------
128 	Beschreibung:
129  --------------------------------------------------------------------*/
130 
SetSubType(sal_uInt16 nType)131 void SwDateTimeField::SetSubType(sal_uInt16 nType)
132 {
133 	nSubType = nType;
134 }
135 /*--------------------------------------------------------------------
136 	Beschreibung:
137  --------------------------------------------------------------------*/
138 
SetPar2(const String & rStr)139 void SwDateTimeField::SetPar2(const String& rStr)
140 {
141 	nOffset = rStr.ToInt32();
142 }
143 
144 /*--------------------------------------------------------------------
145 	Beschreibung:
146  --------------------------------------------------------------------*/
147 
GetPar2() const148 String SwDateTimeField::GetPar2() const
149 {
150 	if (nOffset)
151 		return String::CreateFromInt32(nOffset);
152 	else
153 		return aEmptyStr;
154 }
155 
156 /*--------------------------------------------------------------------
157 	Beschreibung:
158  --------------------------------------------------------------------*/
159 
SetDateTime(const DateTime & rDT)160 void SwDateTimeField::SetDateTime(const DateTime& rDT)
161 {
162     SetValue(GetDateTime(GetDoc(), rDT));
163 }
164 
165 /*--------------------------------------------------------------------
166 	Beschreibung:
167  --------------------------------------------------------------------*/
168 
GetDateTime(SwDoc * pDoc,const DateTime & rDT)169 double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
170 {
171 	SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
172     Date* pNullDate = pFormatter->GetNullDate();
173 
174     double fResult = rDT - DateTime(*pNullDate);
175 
176     return fResult;
177 }
178 
179 /*--------------------------------------------------------------------
180 	Beschreibung:
181  --------------------------------------------------------------------*/
182 
GetValue() const183 double SwDateTimeField::GetValue() const
184 {
185 	if (IsFixed())
186 		return SwValueField::GetValue();
187 	else
188         return GetDateTime(GetDoc(), DateTime());
189 }
190 
191 /*--------------------------------------------------------------------
192 	Beschreibung:
193  --------------------------------------------------------------------*/
194 
GetDate(sal_Bool bUseOffset) const195 Date SwDateTimeField::GetDate(sal_Bool bUseOffset) const
196 {
197 	SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
198 	Date* pNullDate = pFormatter->GetNullDate();
199 
200 	long nVal = static_cast<long>( GetValue() );
201 
202 	if (bUseOffset && nOffset)
203 		nVal += nOffset / 60 / 24;
204 
205 	Date aDate = *pNullDate + nVal;
206 
207 	return aDate;
208 }
209 
210 /*--------------------------------------------------------------------
211 	Beschreibung:
212  --------------------------------------------------------------------*/
213 
GetTime(sal_Bool bUseOffset) const214 Time SwDateTimeField::GetTime(sal_Bool bUseOffset) const
215 {
216     double fDummy;
217     double fFract = modf(GetValue(), &fDummy);
218     DateTime aDT((long)fDummy, 0);
219     aDT += fFract;
220     if (bUseOffset)
221          aDT += Time(0, nOffset);
222     return (Time)aDT;
223 }
224 
225 /*-----------------04.03.98 11:05-------------------
226 
227 --------------------------------------------------*/
QueryValue(uno::Any & rVal,sal_uInt16 nWhichId) const228 sal_Bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
229 {
230     switch( nWhichId )
231 	{
232 	case FIELD_PROP_BOOL1:
233 		{
234 			sal_Bool bTmp = IsFixed();
235 			rVal.setValue(&bTmp, ::getCppuBooleanType());
236 		}
237 		break;
238 	case FIELD_PROP_BOOL2:
239 		{
240 			sal_Bool bTmp = IsDate();
241 			rVal.setValue(&bTmp, ::getCppuBooleanType());
242 		}
243 		break;
244 	case FIELD_PROP_FORMAT:
245 		rVal <<= (sal_Int32)GetFormat();
246 		break;
247 	case FIELD_PROP_SUBTYPE:
248 		rVal <<= (sal_Int32)nOffset;
249 		break;
250 	case FIELD_PROP_DATE_TIME:
251 		{
252             DateTime aDateTime(GetDate(), GetTime());
253 
254 			util::DateTime DateTimeValue;
255 			DateTimeValue.HundredthSeconds = aDateTime.Get100Sec();
256     		DateTimeValue.Seconds = aDateTime.GetSec();
257     		DateTimeValue.Minutes = aDateTime.GetMin();
258     		DateTimeValue.Hours = aDateTime.GetHour();
259     		DateTimeValue.Day = aDateTime.GetDay();
260     		DateTimeValue.Month = aDateTime.GetMonth();
261     		DateTimeValue.Year = aDateTime.GetYear();
262 			rVal <<= DateTimeValue;
263 		}
264 		break;
265 	default:
266         return SwField::QueryValue(rVal, nWhichId);
267     }
268 	return sal_True;
269 }
270 /*-----------------04.03.98 11:05-------------------
271 
272 --------------------------------------------------*/
PutValue(const uno::Any & rVal,sal_uInt16 nWhichId)273 sal_Bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
274 {
275 	sal_Int32 nTmp = 0;
276     switch( nWhichId )
277 	{
278 	case FIELD_PROP_BOOL1:
279 		if(*(sal_Bool*)rVal.getValue())
280 			nSubType |= FIXEDFLD;
281 		else
282 			nSubType &= ~FIXEDFLD;
283 		break;
284 	case FIELD_PROP_BOOL2:
285 		nSubType &=  ~(DATEFLD|TIMEFLD);
286 		nSubType |= *(sal_Bool*)rVal.getValue() ? DATEFLD : TIMEFLD;
287 		break;
288 	case FIELD_PROP_FORMAT:
289 		rVal >>= nTmp;
290 		ChangeFormat(nTmp);
291 		break;
292 	case FIELD_PROP_SUBTYPE:
293 		rVal >>= nTmp;
294 		nOffset = nTmp;
295 		break;
296 	case FIELD_PROP_DATE_TIME:
297 		{
298 			util::DateTime aDateTimeValue;
299 			if(!(rVal >>= aDateTimeValue))
300 				return sal_False;
301 			DateTime aDateTime;
302 			aDateTime.Set100Sec(aDateTimeValue.HundredthSeconds);
303     		aDateTime.SetSec(aDateTimeValue.Seconds);
304     		aDateTime.SetMin(aDateTimeValue.Minutes);
305     		aDateTime.SetHour(aDateTimeValue.Hours);
306     		aDateTime.SetDay(aDateTimeValue.Day);
307     		aDateTime.SetMonth(aDateTimeValue.Month);
308     		aDateTime.SetYear(aDateTimeValue.Year);
309             SetDateTime(aDateTime);
310 		}
311 		break;
312         default:
313             return SwField::PutValue(rVal, nWhichId);
314 	}
315 	return sal_True;
316 }
317 
318