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 <tools/duration.hxx>
21 #include <rtl/math.hxx>
22 #include <sal/log.hxx>
24 #include <systemdatetime.hxx>
26 DateTime::DateTime(DateTimeInitSystem
)
32 if ( GetSystemDateTime( &nD
, &nT
) )
34 Date::operator=( Date( nD
) );
38 Date::operator=( Date( 1, 1, 1900 ) ); // Time::nTime is already 0
41 DateTime::DateTime( const css::util::DateTime
& rDateTime
)
42 : Date( rDateTime
.Day
, rDateTime
.Month
, rDateTime
.Year
),
43 Time( rDateTime
.Hours
, rDateTime
.Minutes
, rDateTime
.Seconds
, rDateTime
.NanoSeconds
)
47 DateTime
& DateTime::operator =( const css::util::DateTime
& rUDateTime
)
49 Date::operator=( Date( rUDateTime
.Day
, rUDateTime
.Month
, rUDateTime
.Year
));
50 Time::operator=( Time( rUDateTime
));
54 bool DateTime::IsBetween( const DateTime
& rFrom
, const DateTime
& rTo
) const
56 return (*this >= rFrom
) && (*this <= rTo
);
59 bool DateTime::operator >( const DateTime
& rDateTime
) const
61 return (Date::operator>( rDateTime
)) ||
62 (Date::operator==( rDateTime
) && tools::Time::operator>( rDateTime
));
65 bool DateTime::operator <( const DateTime
& rDateTime
) const
67 return (Date::operator<( rDateTime
)) ||
68 (Date::operator==( rDateTime
) && tools::Time::operator<( rDateTime
));
71 bool DateTime::operator >=( const DateTime
& rDateTime
) const
73 return (Date::operator>( rDateTime
)) ||
74 (Date::operator==( rDateTime
) && tools::Time::operator>=( rDateTime
));
77 bool DateTime::operator <=( const DateTime
& rDateTime
) const
79 return (Date::operator<( rDateTime
)) ||
80 (Date::operator==( rDateTime
) && tools::Time::operator<=( rDateTime
));
83 sal_Int64
DateTime::GetSecFromDateTime( const Date
& rDate
) const
85 if ( Date::operator<( rDate
) )
89 sal_Int64 nSec
= Date( *this ) - rDate
;
91 sal_Int64 nHour
= GetHour();
92 sal_Int64 nMin
= GetMin();
93 nSec
+= (nHour
*3600)+(nMin
*60)+GetSec();
98 void DateTime::NormalizeTimeRemainderAndApply( tools::Time
& rTime
)
100 sal_uInt16 nHours
= rTime
.GetHour();
101 if ( rTime
.GetTime() > 0 )
105 AddDays( nHours
/ 24 );
107 rTime
.SetHour( nHours
);
110 else if ( rTime
.GetTime() != 0 )
114 AddDays( -static_cast<sal_Int32
>(nHours
) / 24 );
116 rTime
.SetHour( nHours
);
119 rTime
= Time( 24, 0, 0 ) + rTime
;
121 tools::Time::operator=( rTime
);
124 DateTime
& DateTime::operator +=( const tools::Time
& rTime
)
126 tools::Time aTime
= *this;
128 NormalizeTimeRemainderAndApply(aTime
);
132 DateTime
& DateTime::operator -=( const tools::Time
& rTime
)
134 tools::Time aTime
= *this;
136 NormalizeTimeRemainderAndApply(aTime
);
140 DateTime
& DateTime::operator +=( const tools::Duration
& rDuration
)
142 AddDays(rDuration
.GetDays());
143 operator+=(rDuration
.GetTime());
147 DateTime
operator +( const DateTime
& rDateTime
, sal_Int32 nDays
)
149 DateTime
aDateTime( rDateTime
);
150 aDateTime
.AddDays( nDays
);
154 DateTime
operator -( const DateTime
& rDateTime
, sal_Int32 nDays
)
156 DateTime
aDateTime( rDateTime
);
157 aDateTime
.AddDays( -nDays
);
161 DateTime
operator +( const DateTime
& rDateTime
, const tools::Time
& rTime
)
163 DateTime
aDateTime( rDateTime
);
168 DateTime
operator -( const DateTime
& rDateTime
, const tools::Time
& rTime
)
170 DateTime
aDateTime( rDateTime
);
175 DateTime
operator +( const DateTime
& rDateTime
, const tools::Duration
& rDuration
)
177 DateTime
aDateTime(rDateTime
);
178 aDateTime
.AddDays( rDuration
.GetDays());
179 aDateTime
+= rDuration
.GetTime();
183 void DateTime::AddTime( double fTimeInDays
)
185 // Use Duration to diminish floating point accuracy errors.
186 tools::Duration
aDuration(fTimeInDays
);
187 operator+=(aDuration
);
190 DateTime
operator +( const DateTime
& rDateTime
, double fTimeInDays
)
192 DateTime
aDateTime( rDateTime
);
193 aDateTime
.AddTime( fTimeInDays
);
197 tools::Duration
operator -( const DateTime
& rDateTime1
, const DateTime
& rDateTime2
)
199 return tools::Duration( rDateTime2
, rDateTime1
);
203 double DateTime::Sub( const DateTime
& rDateTime1
, const DateTime
& rDateTime2
)
205 if (static_cast<const tools::Time
&>(rDateTime1
) != static_cast<const tools::Time
&>(rDateTime2
))
207 // Use Duration to diminish floating point accuracy errors.
208 const tools::Duration
aDuration( rDateTime2
, rDateTime1
);
209 return aDuration
.GetInDays();
211 return static_cast<const Date
&>(rDateTime1
) - static_cast<const Date
&>(rDateTime2
);
214 void DateTime::GetWin32FileDateTime( sal_uInt32
& rLower
, sal_uInt32
& rUpper
) const
216 const sal_Int64 a100nPerSecond
= SAL_CONST_INT64( 10000000 );
217 const sal_Int64 a100nPerDay
= a100nPerSecond
* sal_Int64( 60 * 60 * 24 );
219 // FILETIME is indirectly documented as uint64, see
220 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
221 // mentioning the ULARGE_INTEGER structure.
222 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724280.aspx
223 // mentions that if FILETIME is not less than 0x8000000000000000 then the
224 // FileTimeToSystemTime function fails, which is another indicator.
225 // Unless there's evidence that FILETIME can represent a signed offset from
226 // 1601-01-01 truncate at 0. (reading part below in
227 // CreateFromWin32FileDateTime() would had to be adapted to signed as
229 sal_Int16 nYear
= GetYear();
230 SAL_WARN_IF( nYear
< 1601, "tools.datetime", "DateTime::GetWin32FileDateTime - year < 1601: " << nYear
);
232 sal_Int64 aTime
= (nYear
< 1601 ? 0 : (
233 a100nPerDay
* (*this - Date(1,1,1601)) +
234 GetNSFromTime()/100));
236 rLower
= sal_uInt32( aTime
% SAL_CONST_UINT64( 0x100000000 ) );
237 rUpper
= sal_uInt32( aTime
/ SAL_CONST_UINT64( 0x100000000 ) );
240 DateTime
DateTime::CreateFromWin32FileDateTime( sal_uInt32 rLower
, sal_uInt32 rUpper
)
242 // (rUpper|rLower) = 100-nanosecond intervals since 1601-01-01 00:00
243 const sal_uInt64 a100nPerSecond
= SAL_CONST_UINT64( 10000000 );
244 const sal_uInt64 a100nPerDay
= a100nPerSecond
* sal_uInt64( 60 * 60 * 24 );
247 sal_uInt64( rUpper
) * SAL_CONST_UINT64( 0x100000000 ) +
248 sal_uInt64( rLower
);
250 SAL_WARN_IF( static_cast<sal_Int64
>(aTime
) < 0, "tools.datetime",
251 "DateTime::CreateFromWin32FileDateTime - absurdly high value expected?");
253 sal_uInt64 nDays
= aTime
/ a100nPerDay
;
255 Date
aDate(1,1,1601);
256 // (0xffffffffffffffff / a100nPerDay = 21350398) fits into sal_Int32
257 // (0x7fffffff = 2147483647)
258 aDate
.AddDays(nDays
);
260 SAL_WARN_IF( aDate
- Date(1,1,1601) != static_cast<sal_Int32
>(nDays
), "tools.datetime",
261 "DateTime::CreateFromWin32FileDateTime - date truncated to max");
263 sal_uInt64 nNanos
= (aTime
- (nDays
* a100nPerDay
)) * 100;
264 return DateTime( aDate
, tools::Time(
265 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerHour
) % sal_uInt64( 24 )),
266 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerMinute
) % sal_uInt64( 60 )),
267 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerSec
) % sal_uInt64( 60 )),
268 static_cast<sal_uInt64
>( nNanos
% tools::Time::nanoSecPerSec
)));
271 DateTime
DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch
)
273 double fValue
= fSecondsSinceEpoch
/ Time::secondPerDay
;
274 const sal_Int32 nDays
= static_cast <sal_Int32
>(::rtl::math::approxFloor(fValue
));
276 Date
aDate (1, 1, 1970);
277 aDate
.AddDays(nDays
);
278 SAL_WARN_IF(aDate
- Date(1, 1, 1970) != nDays
, "tools.datetime",
279 "DateTime::CreateFromUnixTime - date truncated to max");
283 const sal_uInt64 nNanos
= fValue
* tools::Time::nanoSecPerDay
;
284 return DateTime( aDate
, tools::Time(
285 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerHour
) % sal_uInt64( 24 )),
286 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerMinute
) % sal_uInt64( 60 )),
287 static_cast<sal_uInt32
>((nNanos
/ tools::Time::nanoSecPerSec
) % sal_uInt64( 60 )),
288 static_cast<sal_uInt64
>( nNanos
% tools::Time::nanoSecPerSec
)));
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */