Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / tools / source / datetime / datetime.cxx
blob3056bf89947b7d13e12b6982ef419c1e64f59cdd
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 bool DateTime::IsBetween( const DateTime& rFrom, const DateTime& rTo ) const
24 if ( (*this >= rFrom) && (*this <= rTo) )
25 return true;
26 else
27 return false;
30 bool DateTime::operator >( const DateTime& rDateTime ) const
32 if ( (Date::operator>( rDateTime )) ||
33 (Date::operator==( rDateTime ) && Time::operator>( rDateTime )) )
34 return true;
35 else
36 return false;
39 bool DateTime::operator <( const DateTime& rDateTime ) const
41 if ( (Date::operator<( rDateTime )) ||
42 (Date::operator==( rDateTime ) && Time::operator<( rDateTime )) )
43 return true;
44 else
45 return false;
48 bool DateTime::operator >=( const DateTime& rDateTime ) const
50 if ( (Date::operator>( rDateTime )) ||
51 (Date::operator==( rDateTime ) && Time::operator>=( rDateTime )) )
52 return true;
53 else
54 return false;
57 bool DateTime::operator <=( const DateTime& rDateTime ) const
59 if ( (Date::operator<( rDateTime )) ||
60 (Date::operator==( rDateTime ) && Time::operator<=( rDateTime )) )
61 return true;
62 else
63 return false;
66 long DateTime::GetSecFromDateTime( const Date& rDate ) const
68 if ( Date::operator<( rDate ) )
69 return 0;
70 else
72 long nSec = Date( *this ) - rDate;
73 nSec *= 24UL*60*60;
74 long nHour = GetHour();
75 long nMin = GetMin();
76 nSec += (nHour*3600)+(nMin*60)+GetSec();
77 return nSec;
81 DateTime& DateTime::operator +=( const Time& rTime )
83 Time aTime = *this;
84 aTime += rTime;
85 sal_uInt16 nHours = aTime.GetHour();
86 if ( aTime.GetTime() > 0 )
88 while ( nHours >= 24 )
90 Date::operator++();
91 nHours -= 24;
93 aTime.SetHour( nHours );
95 else if ( aTime.GetTime() != 0 )
97 while ( nHours >= 24 )
99 Date::operator--();
100 nHours -= 24;
102 Date::operator--();
103 aTime = Time( 24, 0, 0 )+aTime;
105 Time::operator=( aTime );
107 return *this;
110 DateTime& DateTime::operator -=( const Time& rTime )
112 Time aTime = *this;
113 aTime -= rTime;
114 sal_uInt16 nHours = aTime.GetHour();
115 if ( aTime.GetTime() > 0 )
117 while ( nHours >= 24 )
119 Date::operator++();
120 nHours -= 24;
122 aTime.SetHour( nHours );
124 else if ( aTime.GetTime() != 0 )
126 while ( nHours >= 24 )
128 Date::operator--();
129 nHours -= 24;
131 Date::operator--();
132 aTime = Time( 24, 0, 0 )+aTime;
134 Time::operator=( aTime );
136 return *this;
139 DateTime operator +( const DateTime& rDateTime, long nDays )
141 DateTime aDateTime( rDateTime );
142 aDateTime += nDays;
143 return aDateTime;
146 DateTime operator -( const DateTime& rDateTime, long nDays )
148 DateTime aDateTime( rDateTime );
149 aDateTime -= nDays;
150 return aDateTime;
153 DateTime operator +( const DateTime& rDateTime, const Time& rTime )
155 DateTime aDateTime( rDateTime );
156 aDateTime += rTime;
157 return aDateTime;
160 DateTime operator -( const DateTime& rDateTime, const Time& rTime )
162 DateTime aDateTime( rDateTime );
163 aDateTime -= rTime;
164 return aDateTime;
167 DateTime& DateTime::operator +=( double fTimeInDays )
169 double fInt, fFrac;
170 if ( fTimeInDays < 0.0 )
172 fInt = ::rtl::math::approxCeil( fTimeInDays );
173 fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
175 else
177 fInt = ::rtl::math::approxFloor( fTimeInDays );
178 fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
180 Date::operator+=( long(fInt) ); // full days
181 if ( fFrac )
183 Time aTime(0); // default ctor calls system time, we don't need that
184 fFrac *= ::Time::nanoSecPerDay; // time expressed in nanoseconds
185 aTime.MakeTimeFromNS( static_cast<sal_Int64>(fFrac) ); // method handles negative ns
186 operator+=( aTime );
188 return *this;
191 DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
193 DateTime aDateTime( rDateTime );
194 aDateTime += fTimeInDays;
195 return aDateTime;
198 double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
200 long nDays = (const Date&) rDateTime1 - (const Date&) rDateTime2;
201 sal_Int64 nTime = rDateTime1.GetNSFromTime() - rDateTime2.GetNSFromTime();
202 if ( nTime )
204 double fTime = double(nTime);
205 fTime /= ::Time::nanoSecPerDay; // convert from nanoseconds to fraction
206 if ( nDays < 0 && fTime > 0.0 )
207 fTime = 1.0 - fTime;
208 return double(nDays) + fTime;
210 return double(nDays);
213 void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper )
215 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
216 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
218 sal_Int64 nYears = GetYear() - 1601;
219 sal_Int64 nDays =
220 nYears * 365 +
221 nYears / 4 - nYears / 100 + nYears / 400 +
222 GetDayOfYear() - 1;
224 sal_Int64 aTime =
225 a100nPerDay * nDays +
226 GetNSFromTime()/100;
228 rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
229 rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
232 DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const sal_uInt32 & rUpper )
234 const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
235 const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
237 sal_Int64 aTime = sal_Int64(
238 sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
239 sal_uInt64( rLower ) );
241 sal_Int64 nDays = aTime / a100nPerDay;
242 sal_Int64 nYears =
243 ( nDays -
244 ( nDays / ( 4 * 365 ) ) +
245 ( nDays / ( 100 * 365 ) ) -
246 ( nDays / ( 400 * 365 ) ) ) / 365;
247 nDays -= nYears * 365 + nYears / 4 - nYears / 100 + nYears / 400;
249 sal_uInt16 nMonths = 0;
250 for( sal_Int64 nDaysCount = nDays; nDaysCount >= 0; )
252 nDays = nDaysCount;
253 nMonths ++;
254 nDaysCount -= Date(
255 1, nMonths, sal::static_int_cast< sal_uInt16 >(1601 + nYears) ).
256 GetDaysInMonth();
259 Date _aDate(
260 (sal_uInt16)( nDays + 1 ), nMonths,
261 sal::static_int_cast< sal_uInt16 >(nYears + 1601) );
262 Time _aTime( sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
263 sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 ) ) % sal_Int64( 60 ) ),
264 sal_uIntPtr( ( aTime / ( a100nPerSecond ) ) % sal_Int64( 60 ) ),
265 (aTime % a100nPerSecond) * 100 );
267 return DateTime( _aDate, _aTime );
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */