update dev300-m58
[ooovba.git] / sw / source / core / fields / flddat.cxx
blob648fb6353923fad4648ec1f63f8fed38b95662a2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: flddat.cxx,v $
10 * $Revision: 1.15 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 // #include <math.h>
35 #include <tools/datetime.hxx>
36 #include <svtools/zforlist.hxx>
37 #include <com/sun/star/util/DateTime.hpp>
38 #include <doc.hxx>
39 #include <fldbas.hxx>
40 #include <flddat.hxx>
41 #include <unofldmid.h>
43 using namespace ::com::sun::star;
44 /*--------------------------------------------------
45 Beschreibung: Datum/Zeit-Typ
46 ---------------------------------------------------*/
48 SwDateTimeFieldType::SwDateTimeFieldType(SwDoc* pInitDoc)
49 : SwValueFieldType( pInitDoc, RES_DATETIMEFLD )
52 /*--------------------------------------------------------------------
53 Beschreibung:
54 --------------------------------------------------------------------*/
56 SwFieldType* SwDateTimeFieldType::Copy() const
58 SwDateTimeFieldType *pTmp = new SwDateTimeFieldType(GetDoc());
59 return pTmp;
62 /*--------------------------------------------------------------------
63 Beschreibung: Datum/Zeit-Feld
64 --------------------------------------------------------------------*/
66 SwDateTimeField::SwDateTimeField(SwDateTimeFieldType* pInitType, USHORT nSub, ULONG nFmt, USHORT nLng)
67 : SwValueField(pInitType, nFmt, nLng, 0.0),
68 nSubType(nSub),
69 nOffset(0)
71 if (!nFmt)
73 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
74 if (nSubType & DATEFLD)
75 ChangeFormat(pFormatter->GetFormatIndex(NF_DATE_SYSTEM_SHORT, GetLanguage()));
76 else
77 ChangeFormat(pFormatter->GetFormatIndex(NF_TIME_HHMMSS, GetLanguage()));
79 if (IsFixed())
81 DateTime aDateTime;
82 SetDateTime(aDateTime);
86 /*--------------------------------------------------------------------
87 Beschreibung:
88 --------------------------------------------------------------------*/
90 String SwDateTimeField::Expand() const
92 double fVal;
94 if (!(IsFixed()))
96 DateTime aDateTime;
97 fVal = GetDateTime(GetDoc(), aDateTime);
99 else
100 fVal = GetValue();
102 if (nOffset)
103 fVal += (double)(nOffset * 60L) / 86400.0;
105 return ExpandValue(fVal, GetFormat(), GetLanguage());
108 /*--------------------------------------------------------------------
109 Beschreibung:
110 --------------------------------------------------------------------*/
112 SwField* SwDateTimeField::Copy() const
114 SwDateTimeField *pTmp =
115 new SwDateTimeField((SwDateTimeFieldType*)GetTyp(), nSubType,
116 GetFormat(), GetLanguage());
118 pTmp->SetValue(GetValue());
119 pTmp->SetOffset(nOffset);
120 pTmp->SetAutomaticLanguage(IsAutomaticLanguage());
122 return pTmp;
125 /*--------------------------------------------------------------------
126 Beschreibung:
127 --------------------------------------------------------------------*/
129 USHORT SwDateTimeField::GetSubType() const
131 return nSubType;
134 /*--------------------------------------------------------------------
135 Beschreibung:
136 --------------------------------------------------------------------*/
138 void SwDateTimeField::SetSubType(USHORT nType)
140 nSubType = nType;
142 /*--------------------------------------------------------------------
143 Beschreibung:
144 --------------------------------------------------------------------*/
146 void SwDateTimeField::SetPar2(const String& rStr)
148 nOffset = rStr.ToInt32();
151 /*--------------------------------------------------------------------
152 Beschreibung:
153 --------------------------------------------------------------------*/
155 String SwDateTimeField::GetPar2() const
157 if (nOffset)
158 return String::CreateFromInt32(nOffset);
159 else
160 return aEmptyStr;
163 /*--------------------------------------------------------------------
164 Beschreibung:
165 --------------------------------------------------------------------*/
167 void SwDateTimeField::SetDateTime(const DateTime& rDT)
169 SetValue(GetDateTime(GetDoc(), rDT));
172 /*--------------------------------------------------------------------
173 Beschreibung:
174 --------------------------------------------------------------------*/
176 double SwDateTimeField::GetDateTime(SwDoc* pDoc, const DateTime& rDT)
178 SvNumberFormatter* pFormatter = pDoc->GetNumberFormatter();
179 Date* pNullDate = pFormatter->GetNullDate();
181 double fResult = rDT - DateTime(*pNullDate);
183 return fResult;
186 /*--------------------------------------------------------------------
187 Beschreibung:
188 --------------------------------------------------------------------*/
190 double SwDateTimeField::GetValue() const
192 if (IsFixed())
193 return SwValueField::GetValue();
194 else
195 return GetDateTime(GetDoc(), DateTime());
198 /*--------------------------------------------------------------------
199 Beschreibung:
200 --------------------------------------------------------------------*/
202 Date SwDateTimeField::GetDate(BOOL bUseOffset) const
204 SvNumberFormatter* pFormatter = GetDoc()->GetNumberFormatter();
205 Date* pNullDate = pFormatter->GetNullDate();
207 long nVal = static_cast<long>( GetValue() );
209 if (bUseOffset && nOffset)
210 nVal += nOffset / 60 / 24;
212 Date aDate = *pNullDate + nVal;
214 return aDate;
217 /*--------------------------------------------------------------------
218 Beschreibung:
219 --------------------------------------------------------------------*/
221 Time SwDateTimeField::GetTime(BOOL bUseOffset) const
223 double fDummy;
224 double fFract = modf(GetValue(), &fDummy);
225 DateTime aDT((long)fDummy, 0);
226 aDT += fFract;
227 if (bUseOffset)
228 aDT += Time(0, nOffset);
229 return (Time)aDT;
232 /*-----------------04.03.98 11:05-------------------
234 --------------------------------------------------*/
235 BOOL SwDateTimeField::QueryValue( uno::Any& rVal, USHORT nWhichId ) const
237 switch( nWhichId )
239 case FIELD_PROP_BOOL1:
241 BOOL bTmp = IsFixed();
242 rVal.setValue(&bTmp, ::getCppuBooleanType());
244 break;
245 case FIELD_PROP_BOOL2:
247 BOOL bTmp = IsDate();
248 rVal.setValue(&bTmp, ::getCppuBooleanType());
250 break;
251 case FIELD_PROP_FORMAT:
252 rVal <<= (sal_Int32)GetFormat();
253 break;
254 case FIELD_PROP_SUBTYPE:
255 rVal <<= (sal_Int32)nOffset;
256 break;
257 case FIELD_PROP_DATE_TIME:
259 DateTime aDateTime(GetDate(), GetTime());
261 util::DateTime DateTimeValue;
262 DateTimeValue.HundredthSeconds = aDateTime.Get100Sec();
263 DateTimeValue.Seconds = aDateTime.GetSec();
264 DateTimeValue.Minutes = aDateTime.GetMin();
265 DateTimeValue.Hours = aDateTime.GetHour();
266 DateTimeValue.Day = aDateTime.GetDay();
267 DateTimeValue.Month = aDateTime.GetMonth();
268 DateTimeValue.Year = aDateTime.GetYear();
269 rVal <<= DateTimeValue;
271 break;
272 default:
273 return SwField::QueryValue(rVal, nWhichId);
275 return TRUE;
277 /*-----------------04.03.98 11:05-------------------
279 --------------------------------------------------*/
280 BOOL SwDateTimeField::PutValue( const uno::Any& rVal, USHORT nWhichId )
282 sal_Int32 nTmp = 0;
283 switch( nWhichId )
285 case FIELD_PROP_BOOL1:
286 if(*(sal_Bool*)rVal.getValue())
287 nSubType |= FIXEDFLD;
288 else
289 nSubType &= ~FIXEDFLD;
290 break;
291 case FIELD_PROP_BOOL2:
292 nSubType &= ~(DATEFLD|TIMEFLD);
293 nSubType |= *(sal_Bool*)rVal.getValue() ? DATEFLD : TIMEFLD;
294 break;
295 case FIELD_PROP_FORMAT:
296 rVal >>= nTmp;
297 ChangeFormat(nTmp);
298 break;
299 case FIELD_PROP_SUBTYPE:
300 rVal >>= nTmp;
301 nOffset = nTmp;
302 break;
303 case FIELD_PROP_DATE_TIME:
305 util::DateTime aDateTimeValue;
306 if(!(rVal >>= aDateTimeValue))
307 return FALSE;
308 DateTime aDateTime;
309 aDateTime.Set100Sec(aDateTimeValue.HundredthSeconds);
310 aDateTime.SetSec(aDateTimeValue.Seconds);
311 aDateTime.SetMin(aDateTimeValue.Minutes);
312 aDateTime.SetHour(aDateTimeValue.Hours);
313 aDateTime.SetDay(aDateTimeValue.Day);
314 aDateTime.SetMonth(aDateTimeValue.Month);
315 aDateTime.SetYear(aDateTimeValue.Year);
316 SetDateTime(aDateTime);
318 break;
319 default:
320 return SwField::PutValue(rVal, nWhichId);
322 return TRUE;