xref: /trunk/main/sw/source/core/fields/flddat.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 // #include <math.h>
32 #include <tools/datetime.hxx>
33 #include <svl/zforlist.hxx>
34 #include <com/sun/star/util/DateTime.hpp>
35 #include <doc.hxx>
36 #include <fldbas.hxx>
37 #include <flddat.hxx>
38 #include <unofldmid.h>
39 
40 using namespace ::com::sun::star;
41 /*--------------------------------------------------
42     Beschreibung: Datum/Zeit-Typ
43  ---------------------------------------------------*/
44 
45 SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
46     : SwValueFieldType( pInitDoc, RES_DATETIMEFLD )
47 {}
48 
49 /*--------------------------------------------------------------------
50     Beschreibung:
51  --------------------------------------------------------------------*/
52 
53 SwFieldType* SwDateTimeFieldType::Copy() const
54 {
55     SwDateTimeFieldType *pTmp = new SwDateTimeFieldType(GetDoc());
56     return pTmp;
57 }
58 
59 /*--------------------------------------------------------------------
60     Beschreibung: Datum/Zeit-Feld
61  --------------------------------------------------------------------*/
62 
63 SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, sal_uInt16 nSub, sal_uLong nFmt, sal_uInt16 nLng)
64     : SwValueField(pInitType, nFmt, nLng, 0.0),
65     nSubType(nSub),
66     nOffset(0)
67 {
68     if (!nFmt)
69     {
70         SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
71         if (nSubType & DATEFLD)
72             ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
73         else
74             ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
75     }
76     if (IsFixed())
77     {
78         DateTime aDateTime;
79         SetDateTime(aDateTime);
80     }
81 }
82 
83 /*--------------------------------------------------------------------
84     Beschreibung:
85  --------------------------------------------------------------------*/
86 
87 String SwDateTimeField::Expand() const
88 {
89     double fVal;
90 
91     if (!(IsFixed()))
92     {
93         DateTime aDateTime;
94         fVal = GetDateTime(GetDoc(), aDateTime);
95     }
96     else
97         fVal = GetValue();
98 
99     if (nOffset)
100         fVal += (double)(nOffset * 60L) / 86400.0;
101 
102     return ExpandValue(fVal, GetFormat(), GetLanguage());
103 }
104 
105 /*--------------------------------------------------------------------
106     Beschreibung:
107  --------------------------------------------------------------------*/
108 
109 SwField* SwDateTimeField::Copy() const
110 {
111     SwDateTimeField *pTmp =
112         new SwDateTimeField((SwDateTimeFieldType*)GetTyp(), nSubType,
113                                             GetFormat(), GetLanguage());
114 
115     pTmp->SetValue(GetValue());
116     pTmp->SetOffset(nOffset);
117     pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
118 
119     return pTmp;
120 }
121 
122 /*--------------------------------------------------------------------
123     Beschreibung:
124  --------------------------------------------------------------------*/
125 
126 sal_uInt16 SwDateTimeField::GetSubType() const
127 {
128     return nSubType;
129 }
130 
131 /*--------------------------------------------------------------------
132     Beschreibung:
133  --------------------------------------------------------------------*/
134 
135 void SwDateTimeField::SetSubType(sal_uInt16 nType)
136 {
137     nSubType = nType;
138 }
139 /*--------------------------------------------------------------------
140     Beschreibung:
141  --------------------------------------------------------------------*/
142 
143 void SwDateTimeField::SetPar2(const String& rStr)
144 {
145     nOffset = rStr.ToInt32();
146 }
147 
148 /*--------------------------------------------------------------------
149     Beschreibung:
150  --------------------------------------------------------------------*/
151 
152 String SwDateTimeField::GetPar2() const
153 {
154     if (nOffset)
155         return String::CreateFromInt32(nOffset);
156     else
157         return aEmptyStr;
158 }
159 
160 /*--------------------------------------------------------------------
161     Beschreibung:
162  --------------------------------------------------------------------*/
163 
164 void SwDateTimeField::SetDateTime(const DateTime& rDT)
165 {
166     SetValue(GetDateTime(GetDoc(), rDT));
167 }
168 
169 /*--------------------------------------------------------------------
170     Beschreibung:
171  --------------------------------------------------------------------*/
172 
173 double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
174 {
175     SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
176     Date* pNullDate = pFormatter->GetNullDate();
177 
178     double fResult = rDT - DateTime(*pNullDate);
179 
180     return fResult;
181 }
182 
183 /*--------------------------------------------------------------------
184     Beschreibung:
185  --------------------------------------------------------------------*/
186 
187 double SwDateTimeField::GetValue() const
188 {
189     if (IsFixed())
190         return SwValueField::GetValue();
191     else
192         return GetDateTime(GetDoc(), DateTime());
193 }
194 
195 /*--------------------------------------------------------------------
196     Beschreibung:
197  --------------------------------------------------------------------*/
198 
199 Date SwDateTimeField::GetDate(sal_Bool bUseOffset) const
200 {
201     SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
202     Date* pNullDate = pFormatter->GetNullDate();
203 
204     long nVal = static_cast<long>( GetValue() );
205 
206     if (bUseOffset && nOffset)
207         nVal += nOffset / 60 / 24;
208 
209     Date aDate = *pNullDate + nVal;
210 
211     return aDate;
212 }
213 
214 /*--------------------------------------------------------------------
215     Beschreibung:
216  --------------------------------------------------------------------*/
217 
218 Time SwDateTimeField::GetTime(sal_Bool bUseOffset) const
219 {
220     double fDummy;
221     double fFract = modf(GetValue(), &fDummy);
222     DateTime aDT((long)fDummy, 0);
223     aDT += fFract;
224     if (bUseOffset)
225          aDT += Time(0, nOffset);
226     return (Time)aDT;
227 }
228 
229 /*-----------------04.03.98 11:05-------------------
230 
231 --------------------------------------------------*/
232 sal_Bool SwDateTimeField::QueryValue( uno::Any& rVal, sal_uInt16 nWhichId ) const
233 {
234     switch( nWhichId )
235     {
236     case FIELD_PROP_BOOL1:
237         {
238             sal_Bool bTmp = IsFixed();
239             rVal.setValue(&bTmp, ::getCppuBooleanType());
240         }
241         break;
242     case FIELD_PROP_BOOL2:
243         {
244             sal_Bool bTmp = IsDate();
245             rVal.setValue(&bTmp, ::getCppuBooleanType());
246         }
247         break;
248     case FIELD_PROP_FORMAT:
249         rVal <<= (sal_Int32)GetFormat();
250         break;
251     case FIELD_PROP_SUBTYPE:
252         rVal <<= (sal_Int32)nOffset;
253         break;
254     case FIELD_PROP_DATE_TIME:
255         {
256             DateTime aDateTime(GetDate(), GetTime());
257 
258             util::DateTime DateTimeValue;
259             DateTimeValue.HundredthSeconds = aDateTime.Get100Sec();
260             DateTimeValue.Seconds = aDateTime.GetSec();
261             DateTimeValue.Minutes = aDateTime.GetMin();
262             DateTimeValue.Hours = aDateTime.GetHour();
263             DateTimeValue.Day = aDateTime.GetDay();
264             DateTimeValue.Month = aDateTime.GetMonth();
265             DateTimeValue.Year = aDateTime.GetYear();
266             rVal <<= DateTimeValue;
267         }
268         break;
269     default:
270         return SwField::QueryValue(rVal, nWhichId);
271     }
272     return sal_True;
273 }
274 /*-----------------04.03.98 11:05-------------------
275 
276 --------------------------------------------------*/
277 sal_Bool SwDateTimeField::PutValue( const uno::Any& rVal, sal_uInt16 nWhichId )
278 {
279     sal_Int32 nTmp = 0;
280     switch( nWhichId )
281     {
282     case FIELD_PROP_BOOL1:
283         if(*(sal_Bool*)rVal.getValue())
284             nSubType |= FIXEDFLD;
285         else
286             nSubType &= ~FIXEDFLD;
287         break;
288     case FIELD_PROP_BOOL2:
289         nSubType &=  ~(DATEFLD|TIMEFLD);
290         nSubType |= *(sal_Bool*)rVal.getValue() ? DATEFLD : TIMEFLD;
291         break;
292     case FIELD_PROP_FORMAT:
293         rVal >>= nTmp;
294         ChangeFormat(nTmp);
295         break;
296     case FIELD_PROP_SUBTYPE:
297         rVal >>= nTmp;
298         nOffset = nTmp;
299         break;
300     case FIELD_PROP_DATE_TIME:
301         {
302             util::DateTime aDateTimeValue;
303             if(!(rVal >>= aDateTimeValue))
304                 return sal_False;
305             DateTime aDateTime;
306             aDateTime.Set100Sec(aDateTimeValue.HundredthSeconds);
307             aDateTime.SetSec(aDateTimeValue.Seconds);
308             aDateTime.SetMin(aDateTimeValue.Minutes);
309             aDateTime.SetHour(aDateTimeValue.Hours);
310             aDateTime.SetDay(aDateTimeValue.Day);
311             aDateTime.SetMonth(aDateTimeValue.Month);
312             aDateTime.SetYear(aDateTimeValue.Year);
313             SetDateTime(aDateTime);
314         }
315         break;
316         default:
317             return SwField::PutValue(rVal, nWhichId);
318     }
319     return sal_True;
320 }
321 
322