Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / tools / source / datetime / datetime.cxx
blobbb4c1ff173a57463f2b2e973e2e282c4edfa3171
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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)
27 : Date( Date::EMPTY )
28 , Time( Time::EMPTY )
30 sal_Int32 nD = 0;
31 sal_Int64 nT = 0;
32 if ( GetSystemDateTime( &nD, &nT ) )
34 Date::operator=( Date( nD ) );
35 SetTime( nT );
37 else
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));
51 return *this;
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 ) )
86 return 0;
87 else
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;
98 void DateTime::NormalizeTimeRemainderAndApply( tools::Time& rTime )
100 sal_uInt16 nHours = rTime.GetHour();
101 if ( rTime.GetTime() > 0 )
103 if (nHours >= 24)
105 AddDays( nHours / 24 );
106 nHours %= 24;
107 rTime.SetHour( nHours );
110 else if ( rTime.GetTime() != 0 )
112 if (nHours >= 24)
114 AddDays( -static_cast<sal_Int32>(nHours) / 24 );
115 nHours %= 24;
116 rTime.SetHour( nHours );
118 Date::operator--();
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;
127 aTime += rTime;
128 NormalizeTimeRemainderAndApply(aTime);
129 return *this;
132 DateTime& DateTime::operator -=( const tools::Time& rTime )
134 tools::Time aTime = *this;
135 aTime -= rTime;
136 NormalizeTimeRemainderAndApply(aTime);
137 return *this;
140 DateTime& DateTime::operator +=( const tools::Duration& rDuration )
142 AddDays(rDuration.GetDays());
143 operator+=(rDuration.GetTime());
144 return *this;
147 DateTime operator +( const DateTime& rDateTime, sal_Int32 nDays )
149 DateTime aDateTime( rDateTime );
150 aDateTime.AddDays( nDays );
151 return aDateTime;
154 DateTime operator -( const DateTime& rDateTime, sal_Int32 nDays )
156 DateTime aDateTime( rDateTime );
157 aDateTime.AddDays( -nDays );
158 return aDateTime;
161 DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime )
163 DateTime aDateTime( rDateTime );
164 aDateTime += rTime;
165 return aDateTime;
168 DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
170 DateTime aDateTime( rDateTime );
171 aDateTime -= rTime;
172 return aDateTime;
175 DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration )
177 DateTime aDateTime(rDateTime);
178 aDateTime.AddDays( rDuration.GetDays());
179 aDateTime += rDuration.GetTime();
180 return aDateTime;
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 );
194 return aDateTime;
197 double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
199 if (static_cast<const tools::Time&>(rDateTime1) != static_cast<const tools::Time&>(rDateTime2))
201 // Use Duration to diminish floating point accuracy errors.
202 const tools::Duration aDuration( rDateTime2, rDateTime1);
203 return aDuration.GetInDays();
205 return static_cast<const Date&>(rDateTime1) - static_cast<const Date&>(rDateTime2);
208 void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const
210 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
211 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
213 // FILETIME is indirectly documented as uint64, see
214 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724284.aspx
215 // mentioning the ULARGE_INTEGER structure.
216 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724280.aspx
217 // mentions that if FILETIME is not less than 0x8000000000000000 then the
218 // FileTimeToSystemTime function fails, which is another indicator.
219 // Unless there's evidence that FILETIME can represent a signed offset from
220 // 1601-01-01 truncate at 0. (reading part below in
221 // CreateFromWin32FileDateTime() would had to be adapted to signed as
222 // well).
223 sal_Int16 nYear = GetYear();
224 SAL_WARN_IF( nYear < 1601, "tools.datetime", "DateTime::GetWin32FileDateTime - year < 1601: " << nYear);
226 sal_Int64 aTime = (nYear < 1601 ? 0 : (
227 a100nPerDay * (*this - Date(1,1,1601)) +
228 GetNSFromTime()/100));
230 rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
231 rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
234 DateTime DateTime::CreateFromWin32FileDateTime( sal_uInt32 rLower, sal_uInt32 rUpper )
236 // (rUpper|rLower) = 100-nanosecond intervals since 1601-01-01 00:00
237 const sal_uInt64 a100nPerSecond = SAL_CONST_UINT64( 10000000 );
238 const sal_uInt64 a100nPerDay = a100nPerSecond * sal_uInt64( 60 * 60 * 24 );
240 sal_uInt64 aTime =
241 sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
242 sal_uInt64( rLower );
244 SAL_WARN_IF( static_cast<sal_Int64>(aTime) < 0, "tools.datetime",
245 "DateTime::CreateFromWin32FileDateTime - absurdly high value expected?");
247 sal_uInt64 nDays = aTime / a100nPerDay;
249 Date aDate(1,1,1601);
250 // (0xffffffffffffffff / a100nPerDay = 21350398) fits into sal_Int32
251 // (0x7fffffff = 2147483647)
252 aDate.AddDays(nDays);
254 SAL_WARN_IF( aDate - Date(1,1,1601) != static_cast<sal_Int32>(nDays), "tools.datetime",
255 "DateTime::CreateFromWin32FileDateTime - date truncated to max");
257 sal_uInt64 nNanos = (aTime - (nDays * a100nPerDay)) * 100;
258 return DateTime( aDate, tools::Time(
259 static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerHour) % sal_uInt64( 24 )),
260 static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerMinute) % sal_uInt64( 60 )),
261 static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerSec) % sal_uInt64( 60 )),
262 static_cast<sal_uInt64>( nNanos % tools::Time::nanoSecPerSec)));
265 DateTime DateTime::CreateFromUnixTime(const double fSecondsSinceEpoch)
267 double fValue = fSecondsSinceEpoch / Time::secondPerDay;
268 const sal_Int32 nDays = static_cast <sal_Int32>(::rtl::math::approxFloor(fValue));
270 Date aDate (1, 1, 1970);
271 aDate.AddDays(nDays);
272 SAL_WARN_IF(aDate - Date(1, 1, 1970) != nDays, "tools.datetime",
273 "DateTime::CreateFromUnixTime - date truncated to max");
275 fValue -= nDays;
277 const sal_uInt64 nNanos = fValue * tools::Time::nanoSecPerDay;
278 return DateTime( aDate, tools::Time(
279 static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerHour) % sal_uInt64( 24 )),
280 static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerMinute) % sal_uInt64( 60 )),
281 static_cast<sal_uInt32>((nNanos / tools::Time::nanoSecPerSec) % sal_uInt64( 60 )),
282 static_cast<sal_uInt64>( nNanos % tools::Time::nanoSecPerSec)));
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */