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>
21 #include <sal/log.hxx>
23 #include <systemdatetime.hxx>
25 DateTime::DateTime(DateTimeInitSystem
)
31 if ( GetSystemDateTime( &nD
, &nT
) )
33 Date::operator=( Date( nD
) );
37 Date::operator=( Date( 1, 1, 1900 ) ); // Time::nTime is already 0
40 DateTime::DateTime( const css::util::DateTime
& rDateTime
)
41 : Date( rDateTime
.Day
, rDateTime
.Month
, rDateTime
.Year
),
42 Time( rDateTime
.Hours
, rDateTime
.Minutes
, rDateTime
.Seconds
, rDateTime
.NanoSeconds
)
46 DateTime
& DateTime::operator =( const css::util::DateTime
& rUDateTime
)
48 Date::operator=( Date( rUDateTime
.Day
, rUDateTime
.Month
, rUDateTime
.Year
));
49 Time::operator=( Time( rUDateTime
));
53 bool DateTime::IsBetween( const DateTime
& rFrom
, const DateTime
& rTo
) const
55 return (*this >= rFrom
) && (*this <= rTo
);
58 bool DateTime::operator >( const DateTime
& rDateTime
) const
60 return (Date::operator>( rDateTime
)) ||
61 (Date::operator==( rDateTime
) && tools::Time::operator>( rDateTime
));
64 bool DateTime::operator <( const DateTime
& rDateTime
) const
66 return (Date::operator<( rDateTime
)) ||
67 (Date::operator==( rDateTime
) && tools::Time::operator<( rDateTime
));
70 bool DateTime::operator >=( const DateTime
& rDateTime
) const
72 return (Date::operator>( rDateTime
)) ||
73 (Date::operator==( rDateTime
) && tools::Time::operator>=( rDateTime
));
76 bool DateTime::operator <=( const DateTime
& rDateTime
) const
78 return (Date::operator<( rDateTime
)) ||
79 (Date::operator==( rDateTime
) && tools::Time::operator<=( rDateTime
));
82 sal_Int64
DateTime::GetSecFromDateTime( const Date
& rDate
) const
84 if ( Date::operator<( rDate
) )
88 sal_Int64 nSec
= Date( *this ) - rDate
;
90 sal_Int64 nHour
= GetHour();
91 sal_Int64 nMin
= GetMin();
92 nSec
+= (nHour
*3600)+(nMin
*60)+GetSec();
97 DateTime
& DateTime::operator +=( const tools::Time
& rTime
)
99 tools::Time aTime
= *this;
101 sal_uInt16 nHours
= aTime
.GetHour();
102 if ( aTime
.GetTime() > 0 )
104 while ( nHours
>= 24 )
109 aTime
.SetHour( nHours
);
111 else if ( aTime
.GetTime() != 0 )
113 while ( nHours
>= 24 )
119 aTime
= Time( 24, 0, 0 )+aTime
;
121 tools::Time::operator=( aTime
);
126 DateTime
& DateTime::operator -=( const tools::Time
& rTime
)
128 tools::Time aTime
= *this;
130 sal_uInt16 nHours
= aTime
.GetHour();
131 if ( aTime
.GetTime() > 0 )
133 while ( nHours
>= 24 )
138 aTime
.SetHour( nHours
);
140 else if ( aTime
.GetTime() != 0 )
142 while ( nHours
>= 24 )
148 aTime
= Time( 24, 0, 0 )+aTime
;
150 tools::Time::operator=( aTime
);
155 DateTime
operator +( const DateTime
& rDateTime
, sal_Int32 nDays
)
157 DateTime
aDateTime( rDateTime
);
158 aDateTime
.AddDays( nDays
);
162 DateTime
operator -( const DateTime
& rDateTime
, sal_Int32 nDays
)
164 DateTime
aDateTime( rDateTime
);
165 aDateTime
.AddDays( -nDays
);
169 DateTime
operator +( const DateTime
& rDateTime
, const tools::Time
& rTime
)
171 DateTime
aDateTime( rDateTime
);
176 DateTime
operator -( const DateTime
& rDateTime
, const tools::Time
& rTime
)
178 DateTime
aDateTime( rDateTime
);
183 void DateTime::AddTime( double fTimeInDays
)
186 if ( fTimeInDays
< 0.0 )
188 fInt
= ::rtl::math::approxCeil( fTimeInDays
);
189 fFrac
= fInt
<= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
193 fInt
= ::rtl::math::approxFloor( fTimeInDays
);
194 fFrac
= fInt
>= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
196 AddDays( sal_Int32(fInt
) ); // full days
199 tools::Time
aTime(0); // default ctor calls system time, we don't need that
200 fFrac
*= ::tools::Time::nanoSecPerDay
; // time expressed in nanoseconds
201 aTime
.MakeTimeFromNS( static_cast<sal_Int64
>(fFrac
) ); // method handles negative ns
206 DateTime
operator +( const DateTime
& rDateTime
, double fTimeInDays
)
208 DateTime
aDateTime( rDateTime
);
209 aDateTime
.AddTime( fTimeInDays
);
213 double operator -( const DateTime
& rDateTime1
, const DateTime
& rDateTime2
)
215 sal_Int32 nDays
= static_cast<const Date
&>(rDateTime1
)
216 - static_cast<const Date
&>(rDateTime2
);
217 sal_Int64 nTime
= rDateTime1
.GetNSFromTime() - rDateTime2
.GetNSFromTime();
220 double fTime
= double(nTime
);
221 fTime
/= ::tools::Time::nanoSecPerDay
; // convert from nanoseconds to fraction
222 if ( nDays
< 0 && fTime
> 0.0 )
224 return double(nDays
) + fTime
;
226 return double(nDays
);
229 void DateTime::GetWin32FileDateTime( sal_uInt32
& rLower
, sal_uInt32
& rUpper
) const
231 const sal_Int64 a100nPerSecond
= SAL_CONST_INT64( 10000000 );
232 const sal_Int64 a100nPerDay
= a100nPerSecond
* sal_Int64( 60 * 60 * 24 );
234 // FILETIME is indirectly documented as uint64, see
235 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
236 // mentioning the ULARGE_INTEGER structure.
237 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724280.aspx
238 // mentions that if FILETIME is not less than 0x8000000000000000 then the
239 // FileTimeToSystemTime function fails, which is another indicator.
240 // Unless there's evidence that FILETIME can represent a signed offset from
241 // 1601-01-01 truncate at 0. (reading part below in
242 // CreateFromWin32FileDateTime() would had to be adapted to signed as
244 sal_Int16 nYear
= GetYear();
245 SAL_WARN_IF( nYear
< 1601, "tools.datetime", "DateTime::GetWin32FileDateTime - year < 1601: " << nYear
);
247 sal_Int64 aTime
= (nYear
< 1601 ? 0 : (
248 a100nPerDay
* (*this - Date(1,1,1601)) +
249 GetNSFromTime()/100));
251 rLower
= sal_uInt32( aTime
% SAL_CONST_UINT64( 0x100000000 ) );
252 rUpper
= sal_uInt32( aTime
/ SAL_CONST_UINT64( 0x100000000 ) );
255 DateTime
DateTime::CreateFromWin32FileDateTime( sal_uInt32 rLower
, sal_uInt32 rUpper
)
257 // (rUpper|rLower) = 100-nanosecond intervals since 1601-01-01 00:00
258 const sal_uInt64 a100nPerSecond
= SAL_CONST_UINT64( 10000000 );
259 const sal_uInt64 a100nPerDay
= a100nPerSecond
* sal_uInt64( 60 * 60 * 24 );
262 sal_uInt64( rUpper
) * SAL_CONST_UINT64( 0x100000000 ) +
263 sal_uInt64( rLower
);
265 SAL_WARN_IF( static_cast<sal_Int64
>(aTime
) < 0, "tools.datetime",
266 "DateTime::CreateFromWin32FileDateTime - absurdly high value expected?");
268 sal_uInt64 nDays
= aTime
/ a100nPerDay
;
270 Date
aDate(1,1,1601);
271 // (0xffffffffffffffff / a100nPerDay = 21350398) fits into sal_Int32
272 // (0x7fffffff = 2147483647)
273 aDate
.AddDays(nDays
);
275 SAL_WARN_IF( aDate
- Date(1,1,1601) != static_cast<sal_Int32
>(nDays
), "tools.datetime",
276 "DateTime::CreateFromWin32FileDateTime - date truncated to max");
278 sal_uInt64 nNanos
= (aTime
- (nDays
* a100nPerDay
)) * 100;
279 return DateTime( aDate
, tools::Time(
280 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerHour
) % sal_uInt64( 24 )),
281 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerMinute
) % sal_uInt64( 60 )),
282 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerSec
) % sal_uInt64( 60 )),
283 static_cast<sal_uInt64
>( nNanos
% tools::Time::nanoSecPerSec
)));
286 DateTime
DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch
)
288 double fValue
= fSecondsSinceEpoch
/ Time::secondPerDay
;
289 const sal_Int32 nDays
= static_cast <sal_Int32
>(::rtl::math::approxFloor(fValue
));
291 Date
aDate (1, 1, 1970);
292 aDate
.AddDays(nDays
);
293 SAL_WARN_IF(aDate
- Date(1, 1, 1970) != nDays
, "tools.datetime",
294 "DateTime::CreateFromUnixTime - date truncated to max");
298 const sal_uInt64 nNanos
= fValue
* tools::Time::nanoSecPerDay
;
299 return DateTime( aDate
, tools::Time(
300 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerHour
) % sal_uInt64( 24 )),
301 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerMinute
) % sal_uInt64( 60 )),
302 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerSec
) % sal_uInt64( 60 )),
303 static_cast<sal_uInt64
>( nNanos
% tools::Time::nanoSecPerSec
)));
306 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */