bump product version to 5.0.4.1
[LibreOffice.git] / tools / source / datetime / datetime.cxx
blob8399415cd802e52509cbd12de5c209108ddd6392
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>
22 DateTime::DateTime( const css::util::DateTime& rDateTime )
23 : Date( rDateTime.Day, rDateTime.Month, rDateTime.Year ),
24 Time( rDateTime.Hours, rDateTime.Minutes, rDateTime.Seconds, rDateTime.NanoSeconds )
28 bool DateTime::IsBetween( const DateTime& rFrom, const DateTime& rTo ) const
30 if ( (*this >= rFrom) && (*this <= rTo) )
31 return true;
32 else
33 return false;
36 bool DateTime::operator >( const DateTime& rDateTime ) const
38 if ( (Date::operator>( rDateTime )) ||
39 (Date::operator==( rDateTime ) && tools::Time::operator>( rDateTime )) )
40 return true;
41 else
42 return false;
45 bool DateTime::operator <( const DateTime& rDateTime ) const
47 if ( (Date::operator<( rDateTime )) ||
48 (Date::operator==( rDateTime ) && tools::Time::operator<( rDateTime )) )
49 return true;
50 else
51 return false;
54 bool DateTime::operator >=( const DateTime& rDateTime ) const
56 if ( (Date::operator>( rDateTime )) ||
57 (Date::operator==( rDateTime ) && tools::Time::operator>=( rDateTime )) )
58 return true;
59 else
60 return false;
63 bool DateTime::operator <=( const DateTime& rDateTime ) const
65 if ( (Date::operator<( rDateTime )) ||
66 (Date::operator==( rDateTime ) && tools::Time::operator<=( rDateTime )) )
67 return true;
68 else
69 return false;
72 long DateTime::GetSecFromDateTime( const Date& rDate ) const
74 if ( Date::operator<( rDate ) )
75 return 0;
76 else
78 long nSec = Date( *this ) - rDate;
79 nSec *= 24UL*60*60;
80 long nHour = GetHour();
81 long nMin = GetMin();
82 nSec += (nHour*3600)+(nMin*60)+GetSec();
83 return nSec;
87 DateTime& DateTime::operator +=( const tools::Time& rTime )
89 tools::Time aTime = *this;
90 aTime += rTime;
91 sal_uInt16 nHours = aTime.GetHour();
92 if ( aTime.GetTime() > 0 )
94 while ( nHours >= 24 )
96 Date::operator++();
97 nHours -= 24;
99 aTime.SetHour( nHours );
101 else if ( aTime.GetTime() != 0 )
103 while ( nHours >= 24 )
105 Date::operator--();
106 nHours -= 24;
108 Date::operator--();
109 aTime = Time( 24, 0, 0 )+aTime;
111 tools::Time::operator=( aTime );
113 return *this;
116 DateTime& DateTime::operator -=( const tools::Time& rTime )
118 tools::Time aTime = *this;
119 aTime -= rTime;
120 sal_uInt16 nHours = aTime.GetHour();
121 if ( aTime.GetTime() > 0 )
123 while ( nHours >= 24 )
125 Date::operator++();
126 nHours -= 24;
128 aTime.SetHour( nHours );
130 else if ( aTime.GetTime() != 0 )
132 while ( nHours >= 24 )
134 Date::operator--();
135 nHours -= 24;
137 Date::operator--();
138 aTime = Time( 24, 0, 0 )+aTime;
140 tools::Time::operator=( aTime );
142 return *this;
145 DateTime operator +( const DateTime& rDateTime, long nDays )
147 DateTime aDateTime( rDateTime );
148 aDateTime += nDays;
149 return aDateTime;
152 DateTime operator -( const DateTime& rDateTime, long nDays )
154 DateTime aDateTime( rDateTime );
155 aDateTime -= nDays;
156 return aDateTime;
159 DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime )
161 DateTime aDateTime( rDateTime );
162 aDateTime += rTime;
163 return aDateTime;
166 DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
168 DateTime aDateTime( rDateTime );
169 aDateTime -= rTime;
170 return aDateTime;
173 DateTime& DateTime::operator +=( double fTimeInDays )
175 double fInt, fFrac;
176 if ( fTimeInDays < 0.0 )
178 fInt = ::rtl::math::approxCeil( fTimeInDays );
179 fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
181 else
183 fInt = ::rtl::math::approxFloor( fTimeInDays );
184 fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
186 Date::operator+=( long(fInt) ); // full days
187 if ( fFrac )
189 tools::Time aTime(0); // default ctor calls system time, we don't need that
190 fFrac *= ::tools::Time::nanoSecPerDay; // time expressed in nanoseconds
191 aTime.MakeTimeFromNS( static_cast<sal_Int64>(fFrac) ); // method handles negative ns
192 operator+=( aTime );
194 return *this;
197 DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
199 DateTime aDateTime( rDateTime );
200 aDateTime += fTimeInDays;
201 return aDateTime;
204 double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
206 long nDays = (const Date&) rDateTime1 - (const Date&) rDateTime2;
207 sal_Int64 nTime = rDateTime1.GetNSFromTime() - rDateTime2.GetNSFromTime();
208 if ( nTime )
210 double fTime = double(nTime);
211 fTime /= ::tools::Time::nanoSecPerDay; // convert from nanoseconds to fraction
212 if ( nDays < 0 && fTime > 0.0 )
213 fTime = 1.0 - fTime;
214 return double(nDays) + fTime;
216 return double(nDays);
219 void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper )
221 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
222 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
224 sal_Int64 nYears = GetYear() - 1601;
225 sal_Int64 nDays =
226 nYears * 365 +
227 nYears / 4 - nYears / 100 + nYears / 400 +
228 GetDayOfYear() - 1;
230 sal_Int64 aTime =
231 a100nPerDay * nDays +
232 GetNSFromTime()/100;
234 rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
235 rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
238 DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const sal_uInt32 & rUpper )
240 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
241 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
243 sal_Int64 aTime = sal_Int64(
244 sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
245 sal_uInt64( rLower ) );
247 sal_Int64 nDays = aTime / a100nPerDay;
248 sal_Int64 nYears =
249 ( nDays -
250 ( nDays / ( 4 * 365 ) ) +
251 ( nDays / ( 100 * 365 ) ) -
252 ( nDays / ( 400 * 365 ) ) ) / 365;
253 nDays -= nYears * 365 + nYears / 4 - nYears / 100 + nYears / 400;
255 sal_uInt16 nMonths = 0;
256 for( sal_Int64 nDaysCount = nDays; nDaysCount >= 0; )
258 nDays = nDaysCount;
259 nMonths ++;
260 nDaysCount -= Date(
261 1, nMonths, sal::static_int_cast< sal_uInt16 >(1601 + nYears) ).
262 GetDaysInMonth();
265 Date _aDate(
266 (sal_uInt16)( nDays + 1 ), nMonths,
267 sal::static_int_cast< sal_uInt16 >(nYears + 1601) );
268 tools::Time _aTime( sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
269 sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 ) ) % sal_Int64( 60 ) ),
270 sal_uIntPtr( ( aTime / ( a100nPerSecond ) ) % sal_Int64( 60 ) ),
271 (aTime % a100nPerSecond) * 100 );
273 return DateTime( _aDate, _aTime );
276 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */