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 DateTime::DateTime( const css::util::DateTime
& rDateTime
)
23 : Date( rDateTime
.Day
, rDateTime
.Month
, rDateTime
.Year
),
24 Time( rDateTime
.Hours
, rDateTime
.Minutes
, rDateTime
.Seconds
, rDateTime
.NanoSeconds
)
28 bool DateTime::IsBetween( const DateTime
& rFrom
, const DateTime
& rTo
) const
30 if ( (*this >= rFrom
) && (*this <= rTo
) )
36 bool DateTime::operator >( const DateTime
& rDateTime
) const
38 if ( (Date::operator>( rDateTime
)) ||
39 (Date::operator==( rDateTime
) && tools::Time::operator>( rDateTime
)) )
45 bool DateTime::operator <( const DateTime
& rDateTime
) const
47 if ( (Date::operator<( rDateTime
)) ||
48 (Date::operator==( rDateTime
) && tools::Time::operator<( rDateTime
)) )
54 bool DateTime::operator >=( const DateTime
& rDateTime
) const
56 if ( (Date::operator>( rDateTime
)) ||
57 (Date::operator==( rDateTime
) && tools::Time::operator>=( rDateTime
)) )
63 bool DateTime::operator <=( const DateTime
& rDateTime
) const
65 if ( (Date::operator<( rDateTime
)) ||
66 (Date::operator==( rDateTime
) && tools::Time::operator<=( rDateTime
)) )
72 long DateTime::GetSecFromDateTime( const Date
& rDate
) const
74 if ( Date::operator<( rDate
) )
78 long nSec
= Date( *this ) - rDate
;
80 long nHour
= GetHour();
82 nSec
+= (nHour
*3600)+(nMin
*60)+GetSec();
87 DateTime
& DateTime::operator +=( const tools::Time
& rTime
)
89 tools::Time aTime
= *this;
91 sal_uInt16 nHours
= aTime
.GetHour();
92 if ( aTime
.GetTime() > 0 )
94 while ( nHours
>= 24 )
99 aTime
.SetHour( nHours
);
101 else if ( aTime
.GetTime() != 0 )
103 while ( nHours
>= 24 )
109 aTime
= Time( 24, 0, 0 )+aTime
;
111 tools::Time::operator=( aTime
);
116 DateTime
& DateTime::operator -=( const tools::Time
& rTime
)
118 tools::Time aTime
= *this;
120 sal_uInt16 nHours
= aTime
.GetHour();
121 if ( aTime
.GetTime() > 0 )
123 while ( nHours
>= 24 )
128 aTime
.SetHour( nHours
);
130 else if ( aTime
.GetTime() != 0 )
132 while ( nHours
>= 24 )
138 aTime
= Time( 24, 0, 0 )+aTime
;
140 tools::Time::operator=( aTime
);
145 DateTime
operator +( const DateTime
& rDateTime
, long nDays
)
147 DateTime
aDateTime( rDateTime
);
152 DateTime
operator -( const DateTime
& rDateTime
, long nDays
)
154 DateTime
aDateTime( rDateTime
);
159 DateTime
operator +( const DateTime
& rDateTime
, const tools::Time
& rTime
)
161 DateTime
aDateTime( rDateTime
);
166 DateTime
operator -( const DateTime
& rDateTime
, const tools::Time
& rTime
)
168 DateTime
aDateTime( rDateTime
);
173 DateTime
& DateTime::operator +=( double fTimeInDays
)
176 if ( fTimeInDays
< 0.0 )
178 fInt
= ::rtl::math::approxCeil( fTimeInDays
);
179 fFrac
= fInt
<= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
183 fInt
= ::rtl::math::approxFloor( fTimeInDays
);
184 fFrac
= fInt
>= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
186 Date::operator+=( long(fInt
) ); // full days
189 tools::Time
aTime(0); // default ctor calls system time, we don't need that
190 fFrac
*= ::tools::Time::nanoSecPerDay
; // time expressed in nanoseconds
191 aTime
.MakeTimeFromNS( static_cast<sal_Int64
>(fFrac
) ); // method handles negative ns
197 DateTime
operator +( const DateTime
& rDateTime
, double fTimeInDays
)
199 DateTime
aDateTime( rDateTime
);
200 aDateTime
+= fTimeInDays
;
204 double operator -( const DateTime
& rDateTime1
, const DateTime
& rDateTime2
)
206 long nDays
= (const Date
&) rDateTime1
- (const Date
&) rDateTime2
;
207 sal_Int64 nTime
= rDateTime1
.GetNSFromTime() - rDateTime2
.GetNSFromTime();
210 double fTime
= double(nTime
);
211 fTime
/= ::tools::Time::nanoSecPerDay
; // convert from nanoseconds to fraction
212 if ( nDays
< 0 && fTime
> 0.0 )
214 return double(nDays
) + fTime
;
216 return double(nDays
);
219 void DateTime::GetWin32FileDateTime( sal_uInt32
& rLower
, sal_uInt32
& rUpper
)
221 const sal_Int64 a100nPerSecond
= SAL_CONST_INT64( 10000000 );
222 const sal_Int64 a100nPerDay
= a100nPerSecond
* sal_Int64( 60 * 60 * 24 );
224 sal_Int64 nYears
= GetYear() - 1601;
227 nYears
/ 4 - nYears
/ 100 + nYears
/ 400 +
231 a100nPerDay
* nDays
+
234 rLower
= sal_uInt32( aTime
% SAL_CONST_UINT64( 0x100000000 ) );
235 rUpper
= sal_uInt32( aTime
/ SAL_CONST_UINT64( 0x100000000 ) );
238 DateTime
DateTime::CreateFromWin32FileDateTime( const sal_uInt32
& rLower
, const sal_uInt32
& rUpper
)
240 const sal_Int64 a100nPerSecond
= SAL_CONST_INT64( 10000000 );
241 const sal_Int64 a100nPerDay
= a100nPerSecond
* sal_Int64( 60 * 60 * 24 );
243 sal_Int64 aTime
= sal_Int64(
244 sal_uInt64( rUpper
) * SAL_CONST_UINT64( 0x100000000 ) +
245 sal_uInt64( rLower
) );
247 sal_Int64 nDays
= aTime
/ a100nPerDay
;
250 ( nDays
/ ( 4 * 365 ) ) +
251 ( nDays
/ ( 100 * 365 ) ) -
252 ( nDays
/ ( 400 * 365 ) ) ) / 365;
253 nDays
-= nYears
* 365 + nYears
/ 4 - nYears
/ 100 + nYears
/ 400;
255 sal_uInt16 nMonths
= 0;
256 for( sal_Int64 nDaysCount
= nDays
; nDaysCount
>= 0; )
261 1, nMonths
, sal::static_int_cast
< sal_uInt16
>(1601 + nYears
) ).
266 (sal_uInt16
)( nDays
+ 1 ), nMonths
,
267 sal::static_int_cast
< sal_uInt16
>(nYears
+ 1601) );
268 tools::Time
_aTime( sal_uIntPtr( ( aTime
/ ( a100nPerSecond
* 60 * 60 ) ) % sal_Int64( 24 ) ),
269 sal_uIntPtr( ( aTime
/ ( a100nPerSecond
* 60 ) ) % sal_Int64( 60 ) ),
270 sal_uIntPtr( ( aTime
/ ( a100nPerSecond
) ) % sal_Int64( 60 ) ),
271 (aTime
% a100nPerSecond
) * 100 );
273 return DateTime( _aDate
, _aTime
);
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */