Bump version to 21.06.18.1
[LibreOffice.git] / tools / source / datetime / datetime.cxx
blob00790ff78dd4f82d9a562f0487afeb9a21aa34fb
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 <rtl/math.hxx>
21 #include <sal/log.hxx>
23 #include <systemdatetime.hxx>
25 DateTime::DateTime(DateTimeInitSystem)
26 : Date( Date::EMPTY )
27 , Time( Time::EMPTY )
29 sal_Int32 nD = 0;
30 sal_Int64 nT = 0;
31 if ( GetSystemDateTime( &nD, &nT ) )
33 Date::operator=( Date( nD ) );
34 SetTime( nT );
36 else
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));
50 return *this;
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 ) )
85 return 0;
86 else
88 sal_Int64 nSec = Date( *this ) - rDate;
89 nSec *= 24UL*60*60;
90 sal_Int64 nHour = GetHour();
91 sal_Int64 nMin = GetMin();
92 nSec += (nHour*3600)+(nMin*60)+GetSec();
93 return nSec;
97 DateTime& DateTime::operator +=( const tools::Time& rTime )
99 tools::Time aTime = *this;
100 aTime += rTime;
101 sal_uInt16 nHours = aTime.GetHour();
102 if ( aTime.GetTime() > 0 )
104 while ( nHours >= 24 )
106 Date::operator++();
107 nHours -= 24;
109 aTime.SetHour( nHours );
111 else if ( aTime.GetTime() != 0 )
113 while ( nHours >= 24 )
115 Date::operator--();
116 nHours -= 24;
118 Date::operator--();
119 aTime = Time( 24, 0, 0 )+aTime;
121 tools::Time::operator=( aTime );
123 return *this;
126 DateTime& DateTime::operator -=( const tools::Time& rTime )
128 tools::Time aTime = *this;
129 aTime -= rTime;
130 sal_uInt16 nHours = aTime.GetHour();
131 if ( aTime.GetTime() > 0 )
133 while ( nHours >= 24 )
135 Date::operator++();
136 nHours -= 24;
138 aTime.SetHour( nHours );
140 else if ( aTime.GetTime() != 0 )
142 while ( nHours >= 24 )
144 Date::operator--();
145 nHours -= 24;
147 Date::operator--();
148 aTime = Time( 24, 0, 0 )+aTime;
150 tools::Time::operator=( aTime );
152 return *this;
155 DateTime operator +( const DateTime& rDateTime, sal_Int32 nDays )
157 DateTime aDateTime( rDateTime );
158 aDateTime.AddDays( nDays );
159 return aDateTime;
162 DateTime operator -( const DateTime& rDateTime, sal_Int32 nDays )
164 DateTime aDateTime( rDateTime );
165 aDateTime.AddDays( -nDays );
166 return aDateTime;
169 DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime )
171 DateTime aDateTime( rDateTime );
172 aDateTime += rTime;
173 return aDateTime;
176 DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
178 DateTime aDateTime( rDateTime );
179 aDateTime -= rTime;
180 return aDateTime;
183 void DateTime::AddTime( double fTimeInDays )
185 double fInt, fFrac;
186 if ( fTimeInDays < 0.0 )
188 fInt = ::rtl::math::approxCeil( fTimeInDays );
189 fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
191 else
193 fInt = ::rtl::math::approxFloor( fTimeInDays );
194 fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
196 AddDays( sal_Int32(fInt) ); // full days
197 if ( fFrac )
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
202 operator+=( aTime );
206 DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
208 DateTime aDateTime( rDateTime );
209 aDateTime.AddTime( fTimeInDays );
210 return aDateTime;
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();
218 if ( nTime )
220 double fTime = double(nTime);
221 fTime /= ::tools::Time::nanoSecPerDay; // convert from nanoseconds to fraction
222 if ( nDays < 0 && fTime > 0.0 )
223 fTime = 1.0 - fTime;
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
243 // well).
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 );
261 sal_uInt64 aTime =
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");
296 fValue -= nDays;
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: */