merge the formfield patch from ooo-build
[ooovba.git] / tools / source / datetime / datetime.cxx
blobbc1351fdc4e9388a708ce87d506514f86e36b756
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: datetime.cxx,v $
10 * $Revision: 1.10 $
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_tools.hxx"
34 #include <tools/datetime.hxx>
35 #include <rtl/math.hxx>
37 /*************************************************************************
39 |* DateTime::IsBetween()
41 |* Beschreibung DATETIME.SDW
42 |* Ersterstellung TH 18.05.92
43 |* Letzte Aenderung TH 18.05.92
45 *************************************************************************/
47 BOOL DateTime::IsBetween( const DateTime& rFrom,
48 const DateTime& rTo ) const
50 if ( (*this >= rFrom) && (*this <= rTo) )
51 return TRUE;
52 else
53 return FALSE;
56 /*************************************************************************
58 |* DateTime::operator >()
60 |* Beschreibung DATETIME.SDW
61 |* Ersterstellung TH 18.05.92
62 |* Letzte Aenderung TH 18.05.92
64 *************************************************************************/
66 BOOL DateTime::operator >( const DateTime& rDateTime ) const
68 if ( (Date::operator>( rDateTime )) ||
69 (Date::operator==( rDateTime ) && Time::operator>( rDateTime )) )
70 return TRUE;
71 else
72 return FALSE;
75 /*************************************************************************
77 |* DateTime::operator <()
79 |* Beschreibung DATETIME.SDW
80 |* Ersterstellung TH 18.05.92
81 |* Letzte Aenderung TH 18.05.92
83 *************************************************************************/
85 BOOL DateTime::operator <( const DateTime& rDateTime ) const
87 if ( (Date::operator<( rDateTime )) ||
88 (Date::operator==( rDateTime ) && Time::operator<( rDateTime )) )
89 return TRUE;
90 else
91 return FALSE;
94 /*************************************************************************
96 |* DateTime::operator >=()
98 |* Beschreibung DATETIME.SDW
99 |* Ersterstellung TH 18.05.92
100 |* Letzte Aenderung TH 18.05.92
102 *************************************************************************/
104 BOOL DateTime::operator >=( const DateTime& rDateTime ) const
106 if ( (Date::operator>( rDateTime )) ||
107 (Date::operator==( rDateTime ) && Time::operator>=( rDateTime )) )
108 return TRUE;
109 else
110 return FALSE;
113 /*************************************************************************
115 |* DateTime::operator <=()
117 |* Beschreibung DATETIME.SDW
118 |* Ersterstellung TH 18.05.92
119 |* Letzte Aenderung TH 18.05.92
121 *************************************************************************/
123 BOOL DateTime::operator <=( const DateTime& rDateTime ) const
125 if ( (Date::operator<( rDateTime )) ||
126 (Date::operator==( rDateTime ) && Time::operator<=( rDateTime )) )
127 return TRUE;
128 else
129 return FALSE;
132 /*************************************************************************
134 |* DateTime::GetSecFromDateTime()
136 |* Beschreibung DATETIME.SDW
137 |* Ersterstellung TH 02.10.96
138 |* Letzte Aenderung TH 02.10.96
140 *************************************************************************/
142 long DateTime::GetSecFromDateTime( const Date& rDate ) const
144 if ( Date::operator<( rDate ) )
145 return 0;
146 else
148 long nSec = Date( *this ) - rDate;
149 nSec *= 24UL*60*60;
150 long nHour = GetHour();
151 long nMin = GetMin();
152 nSec += (nHour*3600)+(nMin*60)+GetSec();
153 return nSec;
157 /*************************************************************************
159 |* DateTime::GetSecFromDateTime()
161 |* Beschreibung DATETIME.SDW
162 |* Ersterstellung TH 02.10.96
163 |* Letzte Aenderung TH 02.10.96
165 *************************************************************************/
167 void DateTime::MakeDateTimeFromSec( const Date& rDate, ULONG nSec )
169 long nDays = nSec / (24UL*60*60);
170 ((Date*)this)->operator=( rDate );
171 nSec -= nDays * (24UL*60*60);
172 USHORT nMin = (USHORT)(nSec / 60);
173 nSec -= nMin * 60;
174 ((Time*)this)->operator=( Time( 0, nMin, (USHORT)nSec ) );
175 operator+=( nDays );
178 /*************************************************************************
180 |* DateTime::operator +=()
182 |* Beschreibung DATETIME.SDW
183 |* Ersterstellung TH 02.10.96
184 |* Letzte Aenderung TH 02.10.96
186 *************************************************************************/
188 DateTime& DateTime::operator +=( const Time& rTime )
190 Time aTime = *this;
191 aTime += rTime;
192 USHORT nHours = aTime.GetHour();
193 if ( aTime.GetTime() > 0 )
195 while ( nHours >= 24 )
197 Date::operator++();
198 nHours -= 24;
200 aTime.SetHour( nHours );
202 else if ( aTime.GetTime() != 0 )
204 while ( nHours >= 24 )
206 Date::operator--();
207 nHours -= 24;
209 Date::operator--();
210 aTime = Time( 24, 0, 0 )+aTime;
212 Time::operator=( aTime );
214 return *this;
217 /*************************************************************************
219 |* DateTime::operator -=()
221 |* Beschreibung DATETIME.SDW
222 |* Ersterstellung TH 02.10.96
223 |* Letzte Aenderung TH 02.10.96
225 *************************************************************************/
227 DateTime& DateTime::operator -=( const Time& rTime )
229 Time aTime = *this;
230 aTime -= rTime;
231 USHORT nHours = aTime.GetHour();
232 if ( aTime.GetTime() > 0 )
234 while ( nHours >= 24 )
236 Date::operator++();
237 nHours -= 24;
239 aTime.SetHour( nHours );
241 else if ( aTime.GetTime() != 0 )
243 while ( nHours >= 24 )
245 Date::operator--();
246 nHours -= 24;
248 Date::operator--();
249 aTime = Time( 24, 0, 0 )+aTime;
251 Time::operator=( aTime );
253 return *this;
256 /*************************************************************************
258 |* DateTime::operator+()
260 |* Beschreibung DATETIME.SDW
261 |* Ersterstellung TH 02.10.96
262 |* Letzte Aenderung TH 02.10.96
264 *************************************************************************/
266 DateTime operator +( const DateTime& rDateTime, long nDays )
268 DateTime aDateTime( rDateTime );
269 aDateTime += nDays;
270 return aDateTime;
273 /*************************************************************************
275 |* DateTime::operator-()
277 |* Beschreibung DATETIME.SDW
278 |* Ersterstellung TH 02.10.96
279 |* Letzte Aenderung TH 02.10.96
281 *************************************************************************/
283 DateTime operator -( const DateTime& rDateTime, long nDays )
285 DateTime aDateTime( rDateTime );
286 aDateTime -= nDays;
287 return aDateTime;
290 /*************************************************************************
292 |* DateTime::operator+()
294 |* Beschreibung DATETIME.SDW
295 |* Ersterstellung TH 02.10.96
296 |* Letzte Aenderung TH 02.10.96
298 *************************************************************************/
300 DateTime operator +( const DateTime& rDateTime, const Time& rTime )
302 DateTime aDateTime( rDateTime );
303 aDateTime += rTime;
304 return aDateTime;
307 /*************************************************************************
309 |* DateTime::operator-()
311 |* Beschreibung DATETIME.SDW
312 |* Ersterstellung TH 02.10.96
313 |* Letzte Aenderung TH 02.10.96
315 *************************************************************************/
317 DateTime operator -( const DateTime& rDateTime, const Time& rTime )
319 DateTime aDateTime( rDateTime );
320 aDateTime -= rTime;
321 return aDateTime;
324 /*************************************************************************
326 |* DateTime::operator +=( double )
328 *************************************************************************/
330 DateTime& DateTime::operator +=( double fTimeInDays )
332 double fInt, fFrac;
333 if ( fTimeInDays < 0.0 )
335 fInt = ::rtl::math::approxCeil( fTimeInDays );
336 fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
338 else
340 fInt = ::rtl::math::approxFloor( fTimeInDays );
341 fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
343 Date::operator+=( long(fInt) ); // full days
344 if ( fFrac )
346 Time aTime(0); // default ctor calls system time, we don't need that
347 fFrac *= 24UL * 60 * 60 * 1000; // time expressed in milliseconds
348 aTime.MakeTimeFromMS( long(fFrac) ); // method handles negative ms
349 operator+=( aTime );
351 return *this;
354 /*************************************************************************
356 |* DateTime::operator +( double )
358 *************************************************************************/
360 DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
362 DateTime aDateTime( rDateTime );
363 aDateTime += fTimeInDays;
364 return aDateTime;
367 /*************************************************************************
369 |* DateTime::operator -()
371 *************************************************************************/
373 double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
375 long nDays = (const Date&) rDateTime1 - (const Date&) rDateTime2;
376 long nTime = rDateTime1.GetMSFromTime() - rDateTime2.GetMSFromTime();
377 if ( nTime )
379 double fTime = double(nTime);
380 fTime /= 24UL * 60 * 60 * 1000; // convert from milliseconds to fraction
381 if ( nDays < 0 && fTime > 0.0 )
382 fTime = 1.0 - fTime;
383 return double(nDays) + fTime;
385 return double(nDays);
388 void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper )
390 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
391 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
393 sal_Int64 nYears = GetYear() - 1601;
394 sal_Int64 nDays =
395 nYears * 365 +
396 nYears / 4 - nYears / 100 + nYears / 400 +
397 GetDayOfYear() - 1;
399 sal_Int64 aTime =
400 a100nPerDay * nDays +
401 a100nPerSecond * (
402 sal_Int64( GetSec() ) +
403 60 * sal_Int64( GetMin() ) +
404 60 * 60 * sal_Int64( GetHour() ) );
406 rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
407 rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
410 DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const sal_uInt32 & rUpper )
412 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
413 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
415 sal_Int64 aTime = sal_Int64(
416 sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
417 sal_uInt64( rLower ) );
419 sal_Int64 nDays = aTime / a100nPerDay;
420 sal_Int64 nYears =
421 ( nDays -
422 ( nDays / ( 4 * 365 ) ) +
423 ( nDays / ( 100 * 365 ) ) -
424 ( nDays / ( 400 * 365 ) ) ) / 365;
425 nDays -= nYears * 365 + nYears / 4 - nYears / 100 + nYears / 400;
427 USHORT nMonths = 0;
428 for( sal_Int64 nDaysCount = nDays; nDaysCount >= 0; )
430 nDays = nDaysCount;
431 nMonths ++;
432 nDaysCount -= Date(
433 1, nMonths, sal::static_int_cast< USHORT >(1601 + nYears) ).
434 GetDaysInMonth();
437 Date _aDate(
438 (USHORT)( nDays + 1 ), nMonths,
439 sal::static_int_cast< USHORT >(nYears + 1601) );
440 Time _aTime( ULONG( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
441 ULONG( ( aTime / ( a100nPerSecond * 60 ) ) % sal_Int64( 60 ) ),
442 ULONG( ( aTime / ( a100nPerSecond ) ) % sal_Int64( 60 ) ) );
444 return DateTime( _aDate, _aTime );