1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <tools/datetime.hxx>
20 #include <rtl/math.hxx>
22 bool DateTime::IsBetween( const DateTime
& rFrom
, const DateTime
& rTo
) const
24 if ( (*this >= rFrom
) && (*this <= rTo
) )
30 bool DateTime::operator >( const DateTime
& rDateTime
) const
32 if ( (Date::operator>( rDateTime
)) ||
33 (Date::operator==( rDateTime
) && Time::operator>( rDateTime
)) )
39 bool DateTime::operator <( const DateTime
& rDateTime
) const
41 if ( (Date::operator<( rDateTime
)) ||
42 (Date::operator==( rDateTime
) && Time::operator<( rDateTime
)) )
48 bool DateTime::operator >=( const DateTime
& rDateTime
) const
50 if ( (Date::operator>( rDateTime
)) ||
51 (Date::operator==( rDateTime
) && Time::operator>=( rDateTime
)) )
57 bool DateTime::operator <=( const DateTime
& rDateTime
) const
59 if ( (Date::operator<( rDateTime
)) ||
60 (Date::operator==( rDateTime
) && Time::operator<=( rDateTime
)) )
66 long DateTime::GetSecFromDateTime( const Date
& rDate
) const
68 if ( Date::operator<( rDate
) )
72 long nSec
= Date( *this ) - rDate
;
74 long nHour
= GetHour();
76 nSec
+= (nHour
*3600)+(nMin
*60)+GetSec();
81 DateTime
& DateTime::operator +=( const Time
& rTime
)
85 sal_uInt16 nHours
= aTime
.GetHour();
86 if ( aTime
.GetTime() > 0 )
88 while ( nHours
>= 24 )
93 aTime
.SetHour( nHours
);
95 else if ( aTime
.GetTime() != 0 )
97 while ( nHours
>= 24 )
103 aTime
= Time( 24, 0, 0 )+aTime
;
105 Time::operator=( aTime
);
110 DateTime
& DateTime::operator -=( const Time
& rTime
)
114 sal_uInt16 nHours
= aTime
.GetHour();
115 if ( aTime
.GetTime() > 0 )
117 while ( nHours
>= 24 )
122 aTime
.SetHour( nHours
);
124 else if ( aTime
.GetTime() != 0 )
126 while ( nHours
>= 24 )
132 aTime
= Time( 24, 0, 0 )+aTime
;
134 Time::operator=( aTime
);
139 DateTime
operator +( const DateTime
& rDateTime
, long nDays
)
141 DateTime
aDateTime( rDateTime
);
146 DateTime
operator -( const DateTime
& rDateTime
, long nDays
)
148 DateTime
aDateTime( rDateTime
);
153 DateTime
operator +( const DateTime
& rDateTime
, const Time
& rTime
)
155 DateTime
aDateTime( rDateTime
);
160 DateTime
operator -( const DateTime
& rDateTime
, const Time
& rTime
)
162 DateTime
aDateTime( rDateTime
);
167 DateTime
& DateTime::operator +=( double fTimeInDays
)
170 if ( fTimeInDays
< 0.0 )
172 fInt
= ::rtl::math::approxCeil( fTimeInDays
);
173 fFrac
= fInt
<= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
177 fInt
= ::rtl::math::approxFloor( fTimeInDays
);
178 fFrac
= fInt
>= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
180 Date::operator+=( long(fInt
) ); // full days
183 Time
aTime(0); // default ctor calls system time, we don't need that
184 fFrac
*= ::Time::nanoSecPerDay
; // time expressed in nanoseconds
185 aTime
.MakeTimeFromNS( static_cast<sal_Int64
>(fFrac
) ); // method handles negative ns
191 DateTime
operator +( const DateTime
& rDateTime
, double fTimeInDays
)
193 DateTime
aDateTime( rDateTime
);
194 aDateTime
+= fTimeInDays
;
198 double operator -( const DateTime
& rDateTime1
, const DateTime
& rDateTime2
)
200 long nDays
= (const Date
&) rDateTime1
- (const Date
&) rDateTime2
;
201 sal_Int64 nTime
= rDateTime1
.GetNSFromTime() - rDateTime2
.GetNSFromTime();
204 double fTime
= double(nTime
);
205 fTime
/= ::Time::nanoSecPerDay
; // convert from nanoseconds to fraction
206 if ( nDays
< 0 && fTime
> 0.0 )
208 return double(nDays
) + fTime
;
210 return double(nDays
);
213 void DateTime::GetWin32FileDateTime( sal_uInt32
& rLower
, sal_uInt32
& rUpper
)
215 const sal_Int64 a100nPerSecond
= SAL_CONST_INT64( 10000000 );
216 const sal_Int64 a100nPerDay
= a100nPerSecond
* sal_Int64( 60 * 60 * 24 );
218 sal_Int64 nYears
= GetYear() - 1601;
221 nYears
/ 4 - nYears
/ 100 + nYears
/ 400 +
225 a100nPerDay
* nDays
+
228 rLower
= sal_uInt32( aTime
% SAL_CONST_UINT64( 0x100000000 ) );
229 rUpper
= sal_uInt32( aTime
/ SAL_CONST_UINT64( 0x100000000 ) );
232 DateTime
DateTime::CreateFromWin32FileDateTime( const sal_uInt32
& rLower
, const sal_uInt32
& rUpper
)
234 const sal_Int64 a100nPerSecond
= SAL_CONST_INT64( 10000000 );
235 const sal_Int64 a100nPerDay
= a100nPerSecond
* sal_Int64( 60 * 60 * 24 );
237 sal_Int64 aTime
= sal_Int64(
238 sal_uInt64( rUpper
) * SAL_CONST_UINT64( 0x100000000 ) +
239 sal_uInt64( rLower
) );
241 sal_Int64 nDays
= aTime
/ a100nPerDay
;
244 ( nDays
/ ( 4 * 365 ) ) +
245 ( nDays
/ ( 100 * 365 ) ) -
246 ( nDays
/ ( 400 * 365 ) ) ) / 365;
247 nDays
-= nYears
* 365 + nYears
/ 4 - nYears
/ 100 + nYears
/ 400;
249 sal_uInt16 nMonths
= 0;
250 for( sal_Int64 nDaysCount
= nDays
; nDaysCount
>= 0; )
255 1, nMonths
, sal::static_int_cast
< sal_uInt16
>(1601 + nYears
) ).
260 (sal_uInt16
)( nDays
+ 1 ), nMonths
,
261 sal::static_int_cast
< sal_uInt16
>(nYears
+ 1601) );
262 Time
_aTime( sal_uIntPtr( ( aTime
/ ( a100nPerSecond
* 60 * 60 ) ) % sal_Int64( 24 ) ),
263 sal_uIntPtr( ( aTime
/ ( a100nPerSecond
* 60 ) ) % sal_Int64( 60 ) ),
264 sal_uIntPtr( ( aTime
/ ( a100nPerSecond
) ) % sal_Int64( 60 ) ),
265 (aTime
% a100nPerSecond
) * 100 );
267 return DateTime( _aDate
, _aTime
);
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */