Line data Source code
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 <rtl/math.hxx>
21 :
22 0 : bool DateTime::IsBetween( const DateTime& rFrom, const DateTime& rTo ) const
23 : {
24 0 : if ( (*this >= rFrom) && (*this <= rTo) )
25 0 : return true;
26 : else
27 0 : return false;
28 : }
29 :
30 0 : bool DateTime::operator >( const DateTime& rDateTime ) const
31 : {
32 0 : if ( (Date::operator>( rDateTime )) ||
33 0 : (Date::operator==( rDateTime ) && tools::Time::operator>( rDateTime )) )
34 0 : return true;
35 : else
36 0 : return false;
37 : }
38 :
39 6 : bool DateTime::operator <( const DateTime& rDateTime ) const
40 : {
41 12 : if ( (Date::operator<( rDateTime )) ||
42 8 : (Date::operator==( rDateTime ) && tools::Time::operator<( rDateTime )) )
43 2 : return true;
44 : else
45 4 : return false;
46 : }
47 :
48 44 : bool DateTime::operator >=( const DateTime& rDateTime ) const
49 : {
50 88 : if ( (Date::operator>( rDateTime )) ||
51 88 : (Date::operator==( rDateTime ) && tools::Time::operator>=( rDateTime )) )
52 0 : return true;
53 : else
54 44 : return false;
55 : }
56 :
57 4 : bool DateTime::operator <=( const DateTime& rDateTime ) const
58 : {
59 8 : if ( (Date::operator<( rDateTime )) ||
60 4 : (Date::operator==( rDateTime ) && tools::Time::operator<=( rDateTime )) )
61 0 : return true;
62 : else
63 4 : return false;
64 : }
65 :
66 8 : long DateTime::GetSecFromDateTime( const Date& rDate ) const
67 : {
68 8 : if ( Date::operator<( rDate ) )
69 0 : return 0;
70 : else
71 : {
72 8 : long nSec = Date( *this ) - rDate;
73 8 : nSec *= 24UL*60*60;
74 8 : long nHour = GetHour();
75 8 : long nMin = GetMin();
76 8 : nSec += (nHour*3600)+(nMin*60)+GetSec();
77 8 : return nSec;
78 : }
79 : }
80 :
81 720 : DateTime& DateTime::operator +=( const tools::Time& rTime )
82 : {
83 720 : tools::Time aTime = *this;
84 720 : aTime += rTime;
85 720 : sal_uInt16 nHours = aTime.GetHour();
86 720 : if ( aTime.GetTime() > 0 )
87 : {
88 1448 : while ( nHours >= 24 )
89 : {
90 8 : Date::operator++();
91 8 : nHours -= 24;
92 : }
93 720 : aTime.SetHour( nHours );
94 : }
95 0 : else if ( aTime.GetTime() != 0 )
96 : {
97 0 : while ( nHours >= 24 )
98 : {
99 0 : Date::operator--();
100 0 : nHours -= 24;
101 : }
102 0 : Date::operator--();
103 0 : aTime = Time( 24, 0, 0 )+aTime;
104 : }
105 720 : tools::Time::operator=( aTime );
106 :
107 720 : return *this;
108 : }
109 :
110 244 : DateTime& DateTime::operator -=( const tools::Time& rTime )
111 : {
112 244 : tools::Time aTime = *this;
113 244 : aTime -= rTime;
114 244 : sal_uInt16 nHours = aTime.GetHour();
115 244 : if ( aTime.GetTime() > 0 )
116 : {
117 488 : while ( nHours >= 24 )
118 : {
119 0 : Date::operator++();
120 0 : nHours -= 24;
121 : }
122 244 : aTime.SetHour( nHours );
123 : }
124 0 : else if ( aTime.GetTime() != 0 )
125 : {
126 0 : while ( nHours >= 24 )
127 : {
128 0 : Date::operator--();
129 0 : nHours -= 24;
130 : }
131 0 : Date::operator--();
132 0 : aTime = Time( 24, 0, 0 )+aTime;
133 : }
134 244 : tools::Time::operator=( aTime );
135 :
136 244 : return *this;
137 : }
138 :
139 0 : DateTime operator +( const DateTime& rDateTime, long nDays )
140 : {
141 0 : DateTime aDateTime( rDateTime );
142 0 : aDateTime += nDays;
143 0 : return aDateTime;
144 : }
145 :
146 0 : DateTime operator -( const DateTime& rDateTime, long nDays )
147 : {
148 0 : DateTime aDateTime( rDateTime );
149 0 : aDateTime -= nDays;
150 0 : return aDateTime;
151 : }
152 :
153 0 : DateTime operator +( const DateTime& rDateTime, const tools::Time& rTime )
154 : {
155 0 : DateTime aDateTime( rDateTime );
156 0 : aDateTime += rTime;
157 0 : return aDateTime;
158 : }
159 :
160 0 : DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
161 : {
162 0 : DateTime aDateTime( rDateTime );
163 0 : aDateTime -= rTime;
164 0 : return aDateTime;
165 : }
166 :
167 4 : DateTime& DateTime::operator +=( double fTimeInDays )
168 : {
169 : double fInt, fFrac;
170 4 : if ( fTimeInDays < 0.0 )
171 : {
172 0 : fInt = ::rtl::math::approxCeil( fTimeInDays );
173 0 : fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
174 : }
175 : else
176 : {
177 4 : fInt = ::rtl::math::approxFloor( fTimeInDays );
178 4 : fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
179 : }
180 4 : Date::operator+=( long(fInt) ); // full days
181 4 : if ( fFrac )
182 : {
183 2 : tools::Time aTime(0); // default ctor calls system time, we don't need that
184 2 : fFrac *= ::tools::Time::nanoSecPerDay; // time expressed in nanoseconds
185 2 : aTime.MakeTimeFromNS( static_cast<sal_Int64>(fFrac) ); // method handles negative ns
186 2 : operator+=( aTime );
187 : }
188 4 : return *this;
189 : }
190 :
191 2 : DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
192 : {
193 2 : DateTime aDateTime( rDateTime );
194 2 : aDateTime += fTimeInDays;
195 2 : return aDateTime;
196 : }
197 :
198 3264 : double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
199 : {
200 3264 : long nDays = (const Date&) rDateTime1 - (const Date&) rDateTime2;
201 3264 : sal_Int64 nTime = rDateTime1.GetNSFromTime() - rDateTime2.GetNSFromTime();
202 3264 : if ( nTime )
203 : {
204 178 : double fTime = double(nTime);
205 178 : fTime /= ::tools::Time::nanoSecPerDay; // convert from nanoseconds to fraction
206 178 : if ( nDays < 0 && fTime > 0.0 )
207 0 : fTime = 1.0 - fTime;
208 178 : return double(nDays) + fTime;
209 : }
210 3086 : return double(nDays);
211 : }
212 :
213 240 : void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper )
214 : {
215 240 : const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
216 240 : const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
217 :
218 240 : sal_Int64 nYears = GetYear() - 1601;
219 : sal_Int64 nDays =
220 480 : nYears * 365 +
221 720 : nYears / 4 - nYears / 100 + nYears / 400 +
222 480 : GetDayOfYear() - 1;
223 :
224 : sal_Int64 aTime =
225 240 : a100nPerDay * nDays +
226 240 : GetNSFromTime()/100;
227 :
228 240 : rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
229 240 : rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
230 240 : }
231 :
232 1010 : DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const sal_uInt32 & rUpper )
233 : {
234 1010 : const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
235 1010 : const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
236 :
237 : sal_Int64 aTime = sal_Int64(
238 1010 : sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
239 1010 : sal_uInt64( rLower ) );
240 :
241 1010 : sal_Int64 nDays = aTime / a100nPerDay;
242 : sal_Int64 nYears =
243 1010 : ( nDays -
244 2020 : ( nDays / ( 4 * 365 ) ) +
245 2020 : ( nDays / ( 100 * 365 ) ) -
246 2020 : ( nDays / ( 400 * 365 ) ) ) / 365;
247 1010 : nDays -= nYears * 365 + nYears / 4 - nYears / 100 + nYears / 400;
248 :
249 1010 : sal_uInt16 nMonths = 0;
250 6500 : for( sal_Int64 nDaysCount = nDays; nDaysCount >= 0; )
251 : {
252 4480 : nDays = nDaysCount;
253 4480 : nMonths ++;
254 : nDaysCount -= Date(
255 4480 : 1, nMonths, sal::static_int_cast< sal_uInt16 >(1601 + nYears) ).
256 4480 : GetDaysInMonth();
257 : }
258 :
259 : Date _aDate(
260 : (sal_uInt16)( nDays + 1 ), nMonths,
261 1010 : sal::static_int_cast< sal_uInt16 >(nYears + 1601) );
262 1010 : tools::Time _aTime( sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
263 1010 : sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 ) ) % sal_Int64( 60 ) ),
264 1010 : sal_uIntPtr( ( aTime / ( a100nPerSecond ) ) % sal_Int64( 60 ) ),
265 4040 : (aTime % a100nPerSecond) * 100 );
266 :
267 1010 : return DateTime( _aDate, _aTime );
268 : }
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|