1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: flddat.cxx,v $
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"
35 #include <tools/datetime.hxx>
36 #include <svtools/zforlist.hxx>
37 #include <com/sun/star/util/DateTime.hpp>
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 /*--------------------------------------------------------------------
54 --------------------------------------------------------------------*/
56 SwFieldType
* SwDateTimeFieldType::Copy() const
58 SwDateTimeFieldType
*pTmp
= new SwDateTimeFieldType(GetDoc());
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),
73 SvNumberFormatter
* pFormatter
= GetDoc()->GetNumberFormatter();
74 if (nSubType
& DATEFLD
)
75 ChangeFormat(pFormatter
->GetFormatIndex(NF_DATE_SYSTEM_SHORT
, GetLanguage()));
77 ChangeFormat(pFormatter
->GetFormatIndex(NF_TIME_HHMMSS
, GetLanguage()));
82 SetDateTime(aDateTime
);
86 /*--------------------------------------------------------------------
88 --------------------------------------------------------------------*/
90 String
SwDateTimeField::Expand() const
97 fVal
= GetDateTime(GetDoc(), aDateTime
);
103 fVal
+= (double)(nOffset
* 60L) / 86400.0;
105 return ExpandValue(fVal
, GetFormat(), GetLanguage());
108 /*--------------------------------------------------------------------
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());
125 /*--------------------------------------------------------------------
127 --------------------------------------------------------------------*/
129 USHORT
SwDateTimeField::GetSubType() const
134 /*--------------------------------------------------------------------
136 --------------------------------------------------------------------*/
138 void SwDateTimeField::SetSubType(USHORT nType
)
142 /*--------------------------------------------------------------------
144 --------------------------------------------------------------------*/
146 void SwDateTimeField::SetPar2(const String
& rStr
)
148 nOffset
= rStr
.ToInt32();
151 /*--------------------------------------------------------------------
153 --------------------------------------------------------------------*/
155 String
SwDateTimeField::GetPar2() const
158 return String::CreateFromInt32(nOffset
);
163 /*--------------------------------------------------------------------
165 --------------------------------------------------------------------*/
167 void SwDateTimeField::SetDateTime(const DateTime
& rDT
)
169 SetValue(GetDateTime(GetDoc(), rDT
));
172 /*--------------------------------------------------------------------
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
);
186 /*--------------------------------------------------------------------
188 --------------------------------------------------------------------*/
190 double SwDateTimeField::GetValue() const
193 return SwValueField::GetValue();
195 return GetDateTime(GetDoc(), DateTime());
198 /*--------------------------------------------------------------------
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
;
217 /*--------------------------------------------------------------------
219 --------------------------------------------------------------------*/
221 Time
SwDateTimeField::GetTime(BOOL bUseOffset
) const
224 double fFract
= modf(GetValue(), &fDummy
);
225 DateTime
aDT((long)fDummy
, 0);
228 aDT
+= Time(0, nOffset
);
232 /*-----------------04.03.98 11:05-------------------
234 --------------------------------------------------*/
235 BOOL
SwDateTimeField::QueryValue( uno::Any
& rVal
, USHORT nWhichId
) const
239 case FIELD_PROP_BOOL1
:
241 BOOL bTmp
= IsFixed();
242 rVal
.setValue(&bTmp
, ::getCppuBooleanType());
245 case FIELD_PROP_BOOL2
:
247 BOOL bTmp
= IsDate();
248 rVal
.setValue(&bTmp
, ::getCppuBooleanType());
251 case FIELD_PROP_FORMAT
:
252 rVal
<<= (sal_Int32
)GetFormat();
254 case FIELD_PROP_SUBTYPE
:
255 rVal
<<= (sal_Int32
)nOffset
;
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
;
273 return SwField::QueryValue(rVal
, nWhichId
);
277 /*-----------------04.03.98 11:05-------------------
279 --------------------------------------------------*/
280 BOOL
SwDateTimeField::PutValue( const uno::Any
& rVal
, USHORT nWhichId
)
285 case FIELD_PROP_BOOL1
:
286 if(*(sal_Bool
*)rVal
.getValue())
287 nSubType
|= FIXEDFLD
;
289 nSubType
&= ~FIXEDFLD
;
291 case FIELD_PROP_BOOL2
:
292 nSubType
&= ~(DATEFLD
|TIMEFLD
);
293 nSubType
|= *(sal_Bool
*)rVal
.getValue() ? DATEFLD
: TIMEFLD
;
295 case FIELD_PROP_FORMAT
:
299 case FIELD_PROP_SUBTYPE
:
303 case FIELD_PROP_DATE_TIME
:
305 util::DateTime aDateTimeValue
;
306 if(!(rVal
>>= aDateTimeValue
))
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
);
320 return SwField::PutValue(rVal
, nWhichId
);