LibreOffice Module tools (master) 1
datetime.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#include <tools/datetime.hxx>
20#include <tools/duration.hxx>
21#include <rtl/math.hxx>
22#include <sal/log.hxx>
23
24#include <systemdatetime.hxx>
25
27 : Date( Date::EMPTY )
28 , Time( Time::EMPTY )
29{
30 sal_Int32 nD = 0;
31 sal_Int64 nT = 0;
32 if ( GetSystemDateTime( &nD, &nT ) )
33 {
34 Date::operator=( Date( nD ) );
35 SetTime( nT );
36 }
37 else
38 Date::operator=( Date( 1, 1, 1900 ) ); // Time::nTime is already 0
39}
40
41DateTime::DateTime( const css::util::DateTime& rDateTime )
42 : Date( rDateTime.Day, rDateTime.Month, rDateTime.Year ),
43 Time( rDateTime.Hours, rDateTime.Minutes, rDateTime.Seconds, rDateTime.NanoSeconds )
44{
45}
46
47DateTime& DateTime::operator =( const css::util::DateTime& rUDateTime )
48{
49 Date::operator=( Date( rUDateTime.Day, rUDateTime.Month, rUDateTime.Year));
50 Time::operator=( Time( rUDateTime));
51 return *this;
52}
53
54bool DateTime::IsBetween( const DateTime& rFrom, const DateTime& rTo ) const
55{
56 return (*this >= rFrom) && (*this <= rTo);
57}
58
59bool DateTime::operator >( const DateTime& rDateTime ) const
60{
61 return (Date::operator>( rDateTime )) ||
62 (Date::operator==( rDateTime ) && tools::Time::operator>( rDateTime ));
63}
64
65bool DateTime::operator <( const DateTime& rDateTime ) const
66{
67 return (Date::operator<( rDateTime )) ||
68 (Date::operator==( rDateTime ) && tools::Time::operator<( rDateTime ));
69}
70
71bool DateTime::operator >=( const DateTime& rDateTime ) const
72{
73 return (Date::operator>( rDateTime )) ||
74 (Date::operator==( rDateTime ) && tools::Time::operator>=( rDateTime ));
75}
76
77bool DateTime::operator <=( const DateTime& rDateTime ) const
78{
79 return (Date::operator<( rDateTime )) ||
80 (Date::operator==( rDateTime ) && tools::Time::operator<=( rDateTime ));
81}
82
83sal_Int64 DateTime::GetSecFromDateTime( const Date& rDate ) const
84{
85 if ( Date::operator<( rDate ) )
86 return 0;
87 else
88 {
89 sal_Int64 nSec = Date( *this ) - rDate;
90 nSec *= 24UL*60*60;
91 sal_Int64 nHour = GetHour();
92 sal_Int64 nMin = GetMin();
93 nSec += (nHour*3600)+(nMin*60)+GetSec();
94 return nSec;
95 }
96}
97
99{
100 sal_uInt16 nHours = rTime.GetHour();
101 if ( rTime.GetTime() > 0 )
102 {
103 if (nHours >= 24)
104 {
105 AddDays( nHours / 24 );
106 nHours %= 24;
107 rTime.SetHour( nHours );
108 }
109 }
110 else if ( rTime.GetTime() != 0 )
111 {
112 if (nHours >= 24)
113 {
114 AddDays( -static_cast<sal_Int32>(nHours) / 24 );
115 nHours %= 24;
116 rTime.SetHour( nHours );
117 }
119 rTime = Time( 24, 0, 0 ) + rTime;
120 }
121 tools::Time::operator=( rTime );
122}
123
125{
126 tools::Time aTime = *this;
127 aTime += rTime;
129 return *this;
130}
131
133{
134 tools::Time aTime = *this;
135 aTime -= rTime;
137 return *this;
138}
139
141{
142 AddDays(rDuration.GetDays());
143 operator+=(rDuration.GetTime());
144 return *this;
145}
146
147DateTime operator +( const DateTime& rDateTime, sal_Int32 nDays )
148{
149 DateTime aDateTime( rDateTime );
150 aDateTime.AddDays( nDays );
151 return aDateTime;
152}
153
154DateTime operator -( const DateTime& rDateTime, sal_Int32 nDays )
155{
156 DateTime aDateTime( rDateTime );
157 aDateTime.AddDays( -nDays );
158 return aDateTime;
159}
160
161DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime )
162{
163 DateTime aDateTime( rDateTime );
164 aDateTime += rTime;
165 return aDateTime;
166}
167
168DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
169{
170 DateTime aDateTime( rDateTime );
171 aDateTime -= rTime;
172 return aDateTime;
173}
174
175DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration )
176{
177 DateTime aDateTime(rDateTime);
178 aDateTime.AddDays( rDuration.GetDays());
179 aDateTime += rDuration.GetTime();
180 return aDateTime;
181}
182
183void DateTime::AddTime( double fTimeInDays )
184{
185 // Use Duration to diminish floating point accuracy errors.
186 tools::Duration aDuration(fTimeInDays);
187 operator+=(aDuration);
188}
189
190DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
191{
192 DateTime aDateTime( rDateTime );
193 aDateTime.AddTime( fTimeInDays );
194 return aDateTime;
195}
196
197tools::Duration operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
198{
199 return tools::Duration( rDateTime2, rDateTime1);
200}
201
202// static
203double DateTime::Sub( const DateTime& rDateTime1, const DateTime& rDateTime2 )
204{
205 if (static_cast<const tools::Time&>(rDateTime1) != static_cast<const tools::Time&>(rDateTime2))
206 {
207 // Use Duration to diminish floating point accuracy errors.
208 const tools::Duration aDuration( rDateTime2, rDateTime1);
209 return aDuration.GetInDays();
210 }
211 return static_cast<const Date&>(rDateTime1) - static_cast<const Date&>(rDateTime2);
212}
213
214void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const
215{
216 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
217 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
218
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
228 // well).
229 sal_Int16 nYear = GetYear();
230 SAL_WARN_IF( nYear < 1601, "tools.datetime", "DateTime::GetWin32FileDateTime - year < 1601: " << nYear);
231
232 sal_Int64 aTime = (nYear < 1601 ? 0 : (
233 a100nPerDay * (*this - Date(1,1,1601)) +
234 GetNSFromTime()/100));
235
236 rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
237 rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
238}
239
240DateTime DateTime::CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper )
241{
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 );
245
246 sal_uInt64 aTime =
247 sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
248 sal_uInt64( rLower );
249
250 SAL_WARN_IF( static_cast<sal_Int64>(aTime) < 0, "tools.datetime",
251 "DateTime::CreateFromWin32FileDateTime - absurdly high value expected?");
252
253 sal_uInt64 nDays = aTime / a100nPerDay;
254
255 Date aDate(1,1,1601);
256 // (0xffffffffffffffff / a100nPerDay = 21350398) fits into sal_Int32
257 // (0x7fffffff = 2147483647)
258 aDate.AddDays(nDays);
259
260 SAL_WARN_IF( aDate - Date(1,1,1601) != static_cast<sal_Int32>(nDays), "tools.datetime",
261 "DateTime::CreateFromWin32FileDateTime - date truncated to max");
262
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)));
269}
270
271DateTime DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch)
272{
273 double fValue = fSecondsSinceEpoch / Time::secondPerDay;
274 const sal_Int32 nDays = static_cast <sal_Int32>(::rtl::math::approxFloor(fValue));
275
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");
280
281 fValue -= nDays;
282
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)));
289}
290
291/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool operator>=(const DateTime &rDateTime) const
Definition: datetime.cxx:71
DateTime & operator+=(const tools::Time &rTime)
Definition: datetime.cxx:124
bool operator<(const DateTime &rDateTime) const
Definition: datetime.cxx:65
static DateTime CreateFromUnixTime(const double fSecondsSinceEpoch)
Creates DateTime given a unix time, which is the number of seconds elapsed since Jan 1st,...
Definition: datetime.cxx:271
bool operator>(const DateTime &rDateTime) const
Definition: datetime.cxx:59
void AddTime(double fTimeInDays)
Definition: datetime.cxx:183
sal_Int64 GetSecFromDateTime(const Date &rDate) const
Definition: datetime.cxx:83
DateTime & operator-=(const tools::Time &rTime)
Definition: datetime.cxx:132
DateTime(DateTimeInitEmpty)
Definition: datetime.hxx:47
void NormalizeTimeRemainderAndApply(tools::Time &rTime)
Definition: datetime.cxx:98
DateTimeInitSystem
Definition: datetime.hxx:38
bool IsBetween(const DateTime &rFrom, const DateTime &rTo) const
Definition: datetime.cxx:54
void GetWin32FileDateTime(sal_uInt32 &rLower, sal_uInt32 &rUpper) const
Definition: datetime.cxx:214
DateTime & operator=(const DateTime &rDateTime)
Definition: datetime.hxx:129
static double Sub(const DateTime &rDateTime1, const DateTime &rDateTime2)
Use Sub() if the floating point "time in days" value is to be processed.
Definition: datetime.cxx:203
bool operator<=(const DateTime &rDateTime) const
Definition: datetime.cxx:77
static DateTime CreateFromWin32FileDateTime(sal_uInt32 rLower, sal_uInt32 rUpper)
Definition: datetime.cxx:240
Represents a date in the proleptic Gregorian calendar.
Definition: date.hxx:55
Date(DateInitEmpty)
Definition: date.hxx:71
Date & operator=(const Date &rDate)
Definition: date.hxx:223
void AddDays(sal_Int32 nAddDays)
Add days skipping year 0 and truncating at limits.
Definition: tdate.cxx:375
sal_Int16 GetYear() const
Definition: date.hxx:113
bool operator==(const Date &rDate) const
Definition: date.hxx:210
Date & operator--()
Definition: tdate.cxx:387
Duration in days and time.
Definition: duration.hxx:22
const Time & GetTime() const
Definition: duration.hxx:52
sal_Int32 GetDays() const
Definition: duration.hxx:51
double GetInDays() const
Definition: duration.hxx:53
static const sal_Int64 nanoSecPerDay
Definition: time.hxx:59
sal_uInt16 GetSec() const
Definition: time.hxx:91
static const sal_Int64 nanoSecPerSec
Definition: time.hxx:56
sal_Int64 GetTime() const
Definition: time.hxx:78
sal_uInt16 GetMin() const
Definition: time.hxx:88
bool operator<=(const tools::Time &rTime) const
Definition: time.hxx:138
Time(TimeInitEmpty)
Definition: time.hxx:67
bool operator<(const tools::Time &rTime) const
Definition: time.hxx:134
sal_uInt16 GetHour() const
Definition: time.hxx:85
void SetTime(sal_Int64 nNewTime)
Definition: time.hxx:77
bool operator>(const tools::Time &rTime) const
Definition: time.hxx:132
bool operator>=(const tools::Time &rTime) const
Definition: time.hxx:136
tools::Time & operator=(const tools::Time &rTime)
Definition: ttime.cxx:357
sal_Int64 GetNSFromTime() const
Definition: ttime.cxx:212
static const sal_Int64 nanoSecPerHour
Definition: time.hxx:58
void SetHour(sal_uInt16 nNewHour)
Definition: ttime.cxx:147
static const sal_Int64 nanoSecPerMinute
Definition: time.hxx:57
DateTime operator-(const DateTime &rDateTime, sal_Int32 nDays)
Definition: datetime.cxx:154
DateTime operator+(const DateTime &rDateTime, sal_Int32 nDays)
Definition: datetime.cxx:147
#define SAL_WARN_IF(condition, area, stream)
constexpr OUStringLiteral EMPTY
bool GetSystemDateTime(sal_Int32 *pDate, sal_Int64 *pTime)
Get current local timestamp.