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 #include <systemdatetime.hxx>
24 DateTime::DateTime(DateTimeInitSystem
)
30 if ( GetSystemDateTime( &nD
, &nT
) )
32 Date::operator=( Date( nD
) );
36 Date::operator=( Date( 1, 1, 1900 ) ); // Time::nTime is already 0
39 DateTime::DateTime( const css::util::DateTime
& rDateTime
)
40 : Date( rDateTime
.Day
, rDateTime
.Month
, rDateTime
.Year
),
41 Time( rDateTime
.Hours
, rDateTime
.Minutes
, rDateTime
.Seconds
, rDateTime
.NanoSeconds
)
45 DateTime
& DateTime::operator =( const css::util::DateTime
& rUDateTime
)
47 Date::operator=( Date( rUDateTime
.Day
, rUDateTime
.Month
, rUDateTime
.Year
));
48 Time::operator=( Time( rUDateTime
));
52 bool DateTime::IsBetween( const DateTime
& rFrom
, const DateTime
& rTo
) const
54 if ( (*this >= rFrom
) && (*this <= rTo
) )
60 bool DateTime::operator >( const DateTime
& rDateTime
) const
62 if ( (Date::operator>( rDateTime
)) ||
63 (Date::operator==( rDateTime
) && tools::Time::operator>( rDateTime
)) )
69 bool DateTime::operator <( const DateTime
& rDateTime
) const
71 if ( (Date::operator<( rDateTime
)) ||
72 (Date::operator==( rDateTime
) && tools::Time::operator<( rDateTime
)) )
78 bool DateTime::operator >=( const DateTime
& rDateTime
) const
80 if ( (Date::operator>( rDateTime
)) ||
81 (Date::operator==( rDateTime
) && tools::Time::operator>=( rDateTime
)) )
87 bool DateTime::operator <=( const DateTime
& rDateTime
) const
89 if ( (Date::operator<( rDateTime
)) ||
90 (Date::operator==( rDateTime
) && tools::Time::operator<=( rDateTime
)) )
96 sal_Int64
DateTime::GetSecFromDateTime( const Date
& rDate
) const
98 if ( Date::operator<( rDate
) )
102 sal_Int64 nSec
= Date( *this ) - rDate
;
104 sal_Int64 nHour
= GetHour();
105 sal_Int64 nMin
= GetMin();
106 nSec
+= (nHour
*3600)+(nMin
*60)+GetSec();
111 DateTime
& DateTime::operator +=( const tools::Time
& rTime
)
113 tools::Time aTime
= *this;
115 sal_uInt16 nHours
= aTime
.GetHour();
116 if ( aTime
.GetTime() > 0 )
118 while ( nHours
>= 24 )
123 aTime
.SetHour( nHours
);
125 else if ( aTime
.GetTime() != 0 )
127 while ( nHours
>= 24 )
133 aTime
= Time( 24, 0, 0 )+aTime
;
135 tools::Time::operator=( aTime
);
140 DateTime
& DateTime::operator -=( const tools::Time
& rTime
)
142 tools::Time aTime
= *this;
144 sal_uInt16 nHours
= aTime
.GetHour();
145 if ( aTime
.GetTime() > 0 )
147 while ( nHours
>= 24 )
152 aTime
.SetHour( nHours
);
154 else if ( aTime
.GetTime() != 0 )
156 while ( nHours
>= 24 )
162 aTime
= Time( 24, 0, 0 )+aTime
;
164 tools::Time::operator=( aTime
);
169 DateTime
operator +( const DateTime
& rDateTime
, sal_Int32 nDays
)
171 DateTime
aDateTime( rDateTime
);
172 aDateTime
.AddDays( nDays
);
176 DateTime
operator -( const DateTime
& rDateTime
, sal_Int32 nDays
)
178 DateTime
aDateTime( rDateTime
);
179 aDateTime
.AddDays( -nDays
);
183 DateTime
operator +( const DateTime
& rDateTime
, const tools::Time
& rTime
)
185 DateTime
aDateTime( rDateTime
);
190 DateTime
operator -( const DateTime
& rDateTime
, const tools::Time
& rTime
)
192 DateTime
aDateTime( rDateTime
);
197 void DateTime::AddTime( double fTimeInDays
)
200 if ( fTimeInDays
< 0.0 )
202 fInt
= ::rtl::math::approxCeil( fTimeInDays
);
203 fFrac
= fInt
<= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
207 fInt
= ::rtl::math::approxFloor( fTimeInDays
);
208 fFrac
= fInt
>= fTimeInDays
? 0.0 : fTimeInDays
- fInt
;
210 AddDays( sal_Int32(fInt
) ); // full days
213 tools::Time
aTime(0); // default ctor calls system time, we don't need that
214 fFrac
*= ::tools::Time::nanoSecPerDay
; // time expressed in nanoseconds
215 aTime
.MakeTimeFromNS( static_cast<sal_Int64
>(fFrac
) ); // method handles negative ns
220 DateTime
operator +( const DateTime
& rDateTime
, double fTimeInDays
)
222 DateTime
aDateTime( rDateTime
);
223 aDateTime
.AddTime( fTimeInDays
);
227 double operator -( const DateTime
& rDateTime1
, const DateTime
& rDateTime2
)
229 sal_Int32 nDays
= static_cast<const Date
&>(rDateTime1
)
230 - static_cast<const Date
&>(rDateTime2
);
231 sal_Int64 nTime
= rDateTime1
.GetNSFromTime() - rDateTime2
.GetNSFromTime();
234 double fTime
= double(nTime
);
235 fTime
/= ::tools::Time::nanoSecPerDay
; // convert from nanoseconds to fraction
236 if ( nDays
< 0 && fTime
> 0.0 )
238 return double(nDays
) + fTime
;
240 return double(nDays
);
243 void DateTime::GetWin32FileDateTime( sal_uInt32
& rLower
, sal_uInt32
& rUpper
) const
245 const sal_Int64 a100nPerSecond
= SAL_CONST_INT64( 10000000 );
246 const sal_Int64 a100nPerDay
= a100nPerSecond
* sal_Int64( 60 * 60 * 24 );
248 // FILETIME is indirectly documented as uint64, see
249 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
250 // mentioning the ULARGE_INTEGER structure.
251 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724280.aspx
252 // mentions that if FILETIME is not less than 0x8000000000000000 then the
253 // FileTimeToSystemTime function fails, which is another indicator.
254 // Unless there's evidence that FILETIME can represent a signed offset from
255 // 1601-01-01 truncate at 0. (reading part below in
256 // CreateFromWin32FileDateTime() would had to be adapted to signed as
258 sal_Int16 nYear
= GetYear();
259 SAL_WARN_IF( nYear
< 1601, "tools.datetime", "DateTime::GetWin32FileDateTime - year < 1601: " << nYear
);
261 sal_Int64 aTime
= (nYear
< 1601 ? 0 : (
262 a100nPerDay
* (*this - Date(1,1,1601)) +
263 GetNSFromTime()/100));
265 rLower
= sal_uInt32( aTime
% SAL_CONST_UINT64( 0x100000000 ) );
266 rUpper
= sal_uInt32( aTime
/ SAL_CONST_UINT64( 0x100000000 ) );
269 DateTime
DateTime::CreateFromWin32FileDateTime( sal_uInt32 rLower
, sal_uInt32 rUpper
)
271 // (rUpper|rLower) = 100-nanosecond intervals since 1601-01-01 00:00
272 const sal_uInt64 a100nPerSecond
= SAL_CONST_UINT64( 10000000 );
273 const sal_uInt64 a100nPerDay
= a100nPerSecond
* sal_uInt64( 60 * 60 * 24 );
276 sal_uInt64( rUpper
) * SAL_CONST_UINT64( 0x100000000 ) +
277 sal_uInt64( rLower
);
279 SAL_WARN_IF( static_cast<sal_Int64
>(aTime
) < 0, "tools.datetime",
280 "DateTime::CreateFromWin32FileDateTime - absurdly high value expected?");
282 sal_uInt64 nDays
= aTime
/ a100nPerDay
;
284 Date
aDate(1,1,1601);
285 // (0xffffffffffffffff / a100nPerDay = 21350398) fits into sal_Int32
286 // (0x7fffffff = 2147483647)
287 aDate
.AddDays(nDays
);
289 SAL_WARN_IF( aDate
- Date(1,1,1601) != static_cast<sal_Int32
>(nDays
), "tools.datetime",
290 "DateTime::CreateFromWin32FileDateTime - date truncated to max");
292 sal_uInt64 nNanos
= (aTime
- (nDays
* a100nPerDay
)) * 100;
293 return DateTime( aDate
, tools::Time(
294 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerHour
) % sal_uInt64( 24 )),
295 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerMinute
) % sal_uInt64( 60 )),
296 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerSec
) % sal_uInt64( 60 )),
297 static_cast<sal_uInt64
>( nNanos
% tools::Time::nanoSecPerSec
)));
300 DateTime
DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch
)
302 double fValue
= fSecondsSinceEpoch
/ Time::secondPerDay
;
303 const sal_Int32 nDays
= static_cast <sal_Int32
>(::rtl::math::approxFloor(fValue
));
305 Date
aDate (1, 1, 1970);
306 aDate
.AddDays(nDays
);
307 SAL_WARN_IF(aDate
- Date(1, 1, 1970) != static_cast<sal_Int32
>(nDays
), "tools.datetime",
308 "DateTime::CreateFromUnixTime - date truncated to max");
312 const sal_uInt64 nNanos
= fValue
* tools::Time::nanoSecPerDay
;
313 return DateTime( aDate
, tools::Time(
314 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerHour
) % sal_uInt64( 24 )),
315 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerMinute
) % sal_uInt64( 60 )),
316 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerSec
) % sal_uInt64( 60 )),
317 static_cast<sal_uInt64
>( nNanos
% tools::Time::nanoSecPerSec
)));
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */