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 ) && Time::operator>( rDateTime )) )
34 0 : return true;
35 : else
36 0 : return false;
37 : }
38 :
39 0 : bool DateTime::operator <( const DateTime& rDateTime ) const
40 : {
41 0 : if ( (Date::operator<( rDateTime )) ||
42 0 : (Date::operator==( rDateTime ) && Time::operator<( rDateTime )) )
43 0 : return true;
44 : else
45 0 : return false;
46 : }
47 :
48 0 : bool DateTime::operator >=( const DateTime& rDateTime ) const
49 : {
50 0 : if ( (Date::operator>( rDateTime )) ||
51 0 : (Date::operator==( rDateTime ) && Time::operator>=( rDateTime )) )
52 0 : return true;
53 : else
54 0 : return false;
55 : }
56 :
57 0 : bool DateTime::operator <=( const DateTime& rDateTime ) const
58 : {
59 0 : if ( (Date::operator<( rDateTime )) ||
60 0 : (Date::operator==( rDateTime ) && Time::operator<=( rDateTime )) )
61 0 : return true;
62 : else
63 0 : return false;
64 : }
65 :
66 0 : long DateTime::GetSecFromDateTime( const Date& rDate ) const
67 : {
68 0 : if ( Date::operator<( rDate ) )
69 0 : return 0;
70 : else
71 : {
72 0 : long nSec = Date( *this ) - rDate;
73 0 : nSec *= 24UL*60*60;
74 0 : long nHour = GetHour();
75 0 : long nMin = GetMin();
76 0 : nSec += (nHour*3600)+(nMin*60)+GetSec();
77 0 : return nSec;
78 : }
79 : }
80 :
81 0 : DateTime& DateTime::operator +=( const Time& rTime )
82 : {
83 0 : Time aTime = *this;
84 0 : aTime += rTime;
85 0 : sal_uInt16 nHours = aTime.GetHour();
86 0 : if ( aTime.GetTime() > 0 )
87 : {
88 0 : while ( nHours >= 24 )
89 : {
90 0 : Date::operator++();
91 0 : nHours -= 24;
92 : }
93 0 : 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 0 : Time::operator=( aTime );
106 :
107 0 : return *this;
108 : }
109 :
110 0 : DateTime& DateTime::operator -=( const Time& rTime )
111 : {
112 0 : Time aTime = *this;
113 0 : aTime -= rTime;
114 0 : sal_uInt16 nHours = aTime.GetHour();
115 0 : if ( aTime.GetTime() > 0 )
116 : {
117 0 : while ( nHours >= 24 )
118 : {
119 0 : Date::operator++();
120 0 : nHours -= 24;
121 : }
122 0 : 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 0 : Time::operator=( aTime );
135 :
136 0 : 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 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 Time& rTime )
161 : {
162 0 : DateTime aDateTime( rDateTime );
163 0 : aDateTime -= rTime;
164 0 : return aDateTime;
165 : }
166 :
167 0 : DateTime& DateTime::operator +=( double fTimeInDays )
168 : {
169 : double fInt, fFrac;
170 0 : 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 0 : fInt = ::rtl::math::approxFloor( fTimeInDays );
178 0 : fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
179 : }
180 0 : Date::operator+=( long(fInt) ); // full days
181 0 : if ( fFrac )
182 : {
183 0 : Time aTime(0); // default ctor calls system time, we don't need that
184 0 : fFrac *= ::Time::nanoSecPerDay; // time expressed in nanoseconds
185 0 : aTime.MakeTimeFromNS( static_cast<sal_Int64>(fFrac) ); // method handles negative ns
186 0 : operator+=( aTime );
187 : }
188 0 : return *this;
189 : }
190 :
191 0 : DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
192 : {
193 0 : DateTime aDateTime( rDateTime );
194 0 : aDateTime += fTimeInDays;
195 0 : return aDateTime;
196 : }
197 :
198 0 : double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
199 : {
200 0 : long nDays = (const Date&) rDateTime1 - (const Date&) rDateTime2;
201 0 : sal_Int64 nTime = rDateTime1.GetNSFromTime() - rDateTime2.GetNSFromTime();
202 0 : if ( nTime )
203 : {
204 0 : double fTime = double(nTime);
205 0 : fTime /= ::Time::nanoSecPerDay; // convert from nanoseconds to fraction
206 0 : if ( nDays < 0 && fTime > 0.0 )
207 0 : fTime = 1.0 - fTime;
208 0 : return double(nDays) + fTime;
209 : }
210 0 : return double(nDays);
211 : }
212 :
213 0 : void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper )
214 : {
215 0 : const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
216 0 : const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
217 :
218 0 : sal_Int64 nYears = GetYear() - 1601;
219 : sal_Int64 nDays =
220 0 : nYears * 365 +
221 0 : nYears / 4 - nYears / 100 + nYears / 400 +
222 0 : GetDayOfYear() - 1;
223 :
224 : sal_Int64 aTime =
225 0 : a100nPerDay * nDays +
226 0 : GetNSFromTime()/100;
227 :
228 0 : rLower = sal_uInt32( aTime % SAL_CONST_UINT64( 0x100000000 ) );
229 0 : rUpper = sal_uInt32( aTime / SAL_CONST_UINT64( 0x100000000 ) );
230 0 : }
231 :
232 0 : DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const sal_uInt32 & rUpper )
233 : {
234 0 : const sal_Int64 a100nPerSecond = SAL_CONST_INT64( 10000000 );
235 0 : const sal_Int64 a100nPerDay = a100nPerSecond * sal_Int64( 60 * 60 * 24 );
236 :
237 : sal_Int64 aTime = sal_Int64(
238 0 : sal_uInt64( rUpper ) * SAL_CONST_UINT64( 0x100000000 ) +
239 0 : sal_uInt64( rLower ) );
240 :
241 0 : sal_Int64 nDays = aTime / a100nPerDay;
242 : sal_Int64 nYears =
243 0 : ( nDays -
244 0 : ( nDays / ( 4 * 365 ) ) +
245 0 : ( nDays / ( 100 * 365 ) ) -
246 0 : ( nDays / ( 400 * 365 ) ) ) / 365;
247 0 : nDays -= nYears * 365 + nYears / 4 - nYears / 100 + nYears / 400;
248 :
249 0 : sal_uInt16 nMonths = 0;
250 0 : for( sal_Int64 nDaysCount = nDays; nDaysCount >= 0; )
251 : {
252 0 : nDays = nDaysCount;
253 0 : nMonths ++;
254 : nDaysCount -= Date(
255 0 : 1, nMonths, sal::static_int_cast< sal_uInt16 >(1601 + nYears) ).
256 0 : GetDaysInMonth();
257 : }
258 :
259 : Date _aDate(
260 : (sal_uInt16)( nDays + 1 ), nMonths,
261 0 : sal::static_int_cast< sal_uInt16 >(nYears + 1601) );
262 0 : Time _aTime( sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
263 0 : sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 ) ) % sal_Int64( 60 ) ),
264 0 : sal_uIntPtr( ( aTime / ( a100nPerSecond ) ) % sal_Int64( 60 ) ),
265 0 : (aTime % a100nPerSecond) * 100 );
266 :
267 0 : return DateTime( _aDate, _aTime );
268 : }
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|