/src/libreoffice/tools/source/datetime/ttime.cxx
Line | Count | Source |
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 | | |
20 | | #include <sal/config.h> |
21 | | |
22 | | #include <algorithm> |
23 | | |
24 | | #if defined(_WIN32) |
25 | | #if !defined WIN32_LEAN_AND_MEAN |
26 | | # define WIN32_LEAN_AND_MEAN |
27 | | #endif |
28 | | #include <windows.h> |
29 | | #include <mmsystem.h> |
30 | | #elif defined UNX |
31 | | #include <sys/time.h> |
32 | | #include <unistd.h> |
33 | | #endif |
34 | | |
35 | | #include <time.h> |
36 | | #ifdef __MACH__ |
37 | | #include <mach/clock.h> |
38 | | #include <mach/mach.h> |
39 | | #include <mach/mach_time.h> |
40 | | #endif |
41 | | |
42 | | #include <rtl/math.hxx> |
43 | | #include <tools/time.hxx> |
44 | | #include <com/sun/star/util/DateTime.hpp> |
45 | | |
46 | | #include <systemdatetime.hxx> |
47 | | |
48 | | #if defined(__sun) && defined(__GNUC__) |
49 | | extern long altzone; |
50 | | #endif |
51 | | |
52 | | namespace tools { |
53 | | |
54 | | Time::Time( TimeInitSystem ) |
55 | 14.6k | { |
56 | 14.6k | if ( !GetSystemDateTime( nullptr, &nTime ) ) |
57 | 0 | nTime = 0; |
58 | 14.6k | } |
59 | | |
60 | | Time::Time( sal_uInt32 nHour, sal_uInt32 nMin, sal_uInt32 nSec, sal_uInt64 nNanoSec ) |
61 | 12.6M | { |
62 | | // normalize time |
63 | 12.6M | if (nNanoSec >= nanoSecPerSec) |
64 | 6.19M | { |
65 | 6.19M | nSec += nNanoSec / nanoSecPerSec; |
66 | 6.19M | nNanoSec %= nanoSecPerSec; |
67 | 6.19M | } |
68 | 12.6M | if (nSec >= secondPerMinute) |
69 | 6.19M | { |
70 | 6.19M | nMin += nSec / secondPerMinute; |
71 | 6.19M | nSec %= secondPerMinute; |
72 | 6.19M | } |
73 | 12.6M | if (nMin >= minutePerHour) |
74 | 6.15M | { |
75 | 6.15M | nHour += nMin / minutePerHour; |
76 | 6.15M | nMin %= minutePerHour; |
77 | 6.15M | } |
78 | | |
79 | | // 922337 * HOUR_MASK = 9223370000000000000 largest possible value, 922338 |
80 | | // would be -9223364073709551616. |
81 | 12.6M | assert(HOUR_MASK * nHour >= 0 && "use tools::Duration with days instead!"); |
82 | 12.6M | if (HOUR_MASK * nHour < 0) |
83 | 0 | nHour = 922337; |
84 | | |
85 | | // But as is, GetHour() retrieves only sal_uInt16. Though retrieving in |
86 | | // nanoseconds or milliseconds might be possible this is all crap. |
87 | 12.6M | assert(nHour <= SAL_MAX_UINT16 && "use tools::Duration with days instead!"); |
88 | 12.6M | if (nHour > SAL_MAX_UINT16) |
89 | 0 | nHour = SAL_MAX_UINT16; |
90 | | |
91 | | // construct time |
92 | 12.6M | nTime = assemble(nHour, nMin, nSec, nNanoSec); |
93 | 12.6M | } |
94 | | Time::Time( const css::util::Time &_rTime ) |
95 | 0 | : Time(_rTime.Hours, _rTime.Minutes, _rTime.Seconds, _rTime.NanoSeconds) |
96 | 0 | { |
97 | 0 | } |
98 | | Time::Time( const css::util::DateTime &_rDateTime ) |
99 | 483 | : Time(_rDateTime.Hours, _rDateTime.Minutes, _rDateTime.Seconds, _rDateTime.NanoSeconds) |
100 | 483 | { |
101 | 483 | } |
102 | | |
103 | | // static |
104 | | sal_Int64 tools::Time::assemble(sal_uInt32 h, sal_uInt32 m, sal_uInt32 s, sal_uInt64 ns) |
105 | 12.6M | { |
106 | 12.6M | return ns + s * SEC_MASK + m * MIN_MASK + h * HOUR_MASK; |
107 | 12.6M | } |
108 | | |
109 | | void tools::Time::SetHour( sal_uInt16 nNewHour ) |
110 | 1.12k | { |
111 | 1.12k | nTime = GetSign() * assemble(nNewHour, GetMin(), GetSec(), GetNanoSec()); |
112 | 1.12k | } |
113 | | |
114 | | void tools::Time::SetMin( sal_uInt16 nNewMin ) |
115 | 0 | { |
116 | | // no overflow |
117 | 0 | nTime = GetSign() * assemble(GetHour(), nNewMin % minutePerHour, GetSec(), GetNanoSec()); |
118 | 0 | } |
119 | | |
120 | | void tools::Time::SetSec( sal_uInt16 nNewSec ) |
121 | 0 | { |
122 | | // no overflow |
123 | 0 | nTime = GetSign() * assemble(GetHour(), GetMin(), nNewSec % secondPerMinute, GetNanoSec()); |
124 | 0 | } |
125 | | |
126 | | void tools::Time::SetNanoSec( sal_uInt32 nNewNanoSec ) |
127 | 8.05k | { |
128 | | // no overflow |
129 | 8.05k | nTime = GetSign() * assemble(GetHour(), GetMin(), GetSec(), nNewNanoSec % nanoSecPerSec); |
130 | 8.05k | } |
131 | | |
132 | | sal_Int64 tools::Time::GetNSFromTime() const |
133 | 12.4M | { |
134 | 12.4M | return GetSign() * |
135 | 12.4M | ( GetNanoSec() + |
136 | 12.4M | GetSec() * nanoSecPerSec + |
137 | 12.4M | GetMin() * nanoSecPerMinute + |
138 | 12.4M | GetHour() * nanoSecPerHour ); |
139 | 12.4M | } |
140 | | |
141 | | void tools::Time::MakeTimeFromNS( sal_Int64 nNS ) |
142 | 6.19M | { |
143 | 6.19M | short nSign; |
144 | 6.19M | if ( nNS < 0 ) |
145 | 0 | { |
146 | 0 | nNS *= -1; |
147 | 0 | nSign = -1; |
148 | 0 | } |
149 | 6.19M | else |
150 | 6.19M | nSign = 1; |
151 | | |
152 | 6.19M | tools::Time aTime(0, 0, 0, nNS); |
153 | 6.19M | SetTime( aTime.GetTime() * nSign ); |
154 | 6.19M | } |
155 | | |
156 | | sal_Int32 tools::Time::GetMSFromTime() const |
157 | 0 | { |
158 | 0 | return GetNSFromTime() / nanoPerMilli; |
159 | 0 | } |
160 | | |
161 | | void tools::Time::MakeTimeFromMS( sal_Int32 nMS ) |
162 | 0 | { |
163 | 0 | MakeTimeFromNS(nMS * nanoPerMilli); |
164 | 0 | } |
165 | | |
166 | | double tools::Time::GetTimeInDays() const |
167 | 40.3k | { |
168 | 40.3k | return GetNSFromTime() / double(nanoSecPerDay); |
169 | 40.3k | } |
170 | | |
171 | | // static |
172 | | void tools::Time::GetClock( double fTimeInDays, |
173 | | sal_uInt16& nHour, sal_uInt16& nMinute, sal_uInt16& nSecond, |
174 | | double& fFractionOfSecond, int nFractionDecimals ) |
175 | 7.89k | { |
176 | 7.89k | const double fTime = fTimeInDays - rtl::math::approxFloor(fTimeInDays); // date part absent |
177 | | |
178 | | // If 0 then full day (or no day), shortcut. |
179 | | // If < 0 then approxFloor() effectively returned the ceiling (note this |
180 | | // also holds for negative fTimeInDays values) because of a near identical |
181 | | // value, shortcut this to a full day as well. |
182 | | // If >= 1.0 (actually == 1.0) then fTimeInDays is a negative small value |
183 | | // not significant for a representable time and approxFloor() returned -1, |
184 | | // shortcut to 0:0:0, otherwise it would become 24:0:0. |
185 | 7.89k | if (fTime <= 0.0 || fTime >= 1.0) |
186 | 3.85k | { |
187 | 3.85k | nHour = nMinute = nSecond = 0; |
188 | 3.85k | fFractionOfSecond = 0.0; |
189 | 3.85k | return; |
190 | 3.85k | } |
191 | | |
192 | | // In seconds, including milli and nano. |
193 | 4.03k | const double fRawSeconds = fTime * tools::Time::secondPerDay; |
194 | | |
195 | | // Round to nanoseconds most, which is the highest resolution this could be |
196 | | // influenced by, but if the original value included a date round to at |
197 | | // most 14 significant digits (including adding 4 for *86400), otherwise we |
198 | | // might end up with a fake precision of h:m:s.999999986 which in fact |
199 | | // should had been h:m:s+1 |
200 | | // BUT, leave at least 2 decimals to round. Which shouldn't be a problem in |
201 | | // practice because class Date can calculate only 8-digit days for it's |
202 | | // sal_Int16 year range, which exactly leaves us with 14-4-8=2. |
203 | 4.03k | int nDec = 9; |
204 | 4.03k | const double fAbsTimeInDays = fabs( fTimeInDays); |
205 | 4.03k | if (fAbsTimeInDays >= 1.0) |
206 | 3.35k | { |
207 | 3.35k | const int nDig = static_cast<int>(ceil( log10( fAbsTimeInDays))); |
208 | 3.35k | nDec = std::clamp( 10 - nDig, 2, 9 ); |
209 | 3.35k | } |
210 | 4.03k | double fSeconds = rtl::math::round( fRawSeconds, nDec); |
211 | | |
212 | | // If this ended up as a full day the original value was very very close |
213 | | // but not quite. Take that. |
214 | 4.03k | if (fSeconds >= tools::Time::secondPerDay) |
215 | 0 | fSeconds = fRawSeconds; |
216 | | |
217 | | // Now do not round values (specifically not up), but truncate to the next |
218 | | // magnitude, so 23:59:59.99 is still 23:59:59 and not 24:00:00 (or even |
219 | | // 00:00:00 which Excel does). |
220 | 4.03k | nHour = fSeconds / tools::Time::secondPerHour; |
221 | 4.03k | fSeconds -= nHour * tools::Time::secondPerHour; |
222 | 4.03k | nMinute = fSeconds / tools::Time::secondPerMinute; |
223 | 4.03k | fSeconds -= nMinute * tools::Time::secondPerMinute; |
224 | 4.03k | nSecond = fSeconds; |
225 | 4.03k | fSeconds -= nSecond; |
226 | | |
227 | 4.03k | assert(fSeconds < 1.0); // or back to the drawing board... |
228 | | |
229 | 4.03k | if (nFractionDecimals > 0) |
230 | 78 | { |
231 | | // Do not simply round the fraction, otherwise .999 would end up as .00 |
232 | | // again. Truncate instead if rounding would round up into an integer |
233 | | // value. |
234 | 78 | fFractionOfSecond = rtl::math::round( fSeconds, nFractionDecimals); |
235 | 78 | if (fFractionOfSecond >= 1.0) |
236 | 0 | fFractionOfSecond = rtl::math::pow10Exp( std::trunc( |
237 | 0 | rtl::math::pow10Exp( fSeconds, nFractionDecimals)), -nFractionDecimals); |
238 | 78 | } |
239 | 3.95k | else |
240 | 3.95k | fFractionOfSecond = fSeconds; |
241 | 4.03k | } |
242 | | |
243 | | Time& tools::Time::operator +=( const tools::Time& rTime ) |
244 | 164k | { |
245 | 164k | MakeTimeFromNS(GetNSFromTime() + rTime.GetNSFromTime()); |
246 | 164k | return *this; |
247 | 164k | } |
248 | | |
249 | | Time& tools::Time::operator -=( const tools::Time& rTime ) |
250 | 5.99M | { |
251 | 5.99M | MakeTimeFromNS(GetNSFromTime() - rTime.GetNSFromTime()); |
252 | 5.99M | return *this; |
253 | 5.99M | } |
254 | | |
255 | | Time operator +( const tools::Time& rTime1, const tools::Time& rTime2 ) |
256 | 0 | { |
257 | 0 | tools::Time result(rTime1); |
258 | 0 | return result += rTime2; |
259 | 0 | } |
260 | | |
261 | | Time operator -( const tools::Time& rTime1, const tools::Time& rTime2 ) |
262 | 0 | { |
263 | 0 | tools::Time result(rTime1); |
264 | 0 | return result -= rTime2; |
265 | 0 | } |
266 | | |
267 | | bool tools::Time::IsEqualIgnoreNanoSec( const tools::Time& rTime ) const |
268 | 0 | { |
269 | 0 | return nTime / SEC_MASK == rTime.nTime / SEC_MASK; |
270 | 0 | } |
271 | | |
272 | | Time tools::Time::GetUTCOffset() |
273 | 6.00M | { |
274 | | #if defined(_WIN32) |
275 | | TIME_ZONE_INFORMATION aTimeZone; |
276 | | aTimeZone.Bias = 0; |
277 | | DWORD nTimeZoneRet = GetTimeZoneInformation( &aTimeZone ); |
278 | | sal_Int32 nTempTime = aTimeZone.Bias; |
279 | | if ( nTimeZoneRet == TIME_ZONE_ID_STANDARD ) |
280 | | nTempTime += aTimeZone.StandardBias; |
281 | | else if ( nTimeZoneRet == TIME_ZONE_ID_DAYLIGHT ) |
282 | | nTempTime += aTimeZone.DaylightBias; |
283 | | tools::Time aTime( 0, static_cast<sal_uInt16>(abs( nTempTime )) ); |
284 | | if ( nTempTime > 0 ) |
285 | | aTime = -aTime; |
286 | | return aTime; |
287 | | #else |
288 | 6.00M | static sal_uInt64 nCacheTicks = 0; |
289 | 6.00M | static sal_Int32 nCacheSecOffset = -1; |
290 | 6.00M | sal_uInt64 nTicks = tools::Time::GetSystemTicks(); |
291 | 6.00M | time_t nTime; |
292 | 6.00M | tm aTM; |
293 | 6.00M | short nTempTime; |
294 | | |
295 | | // determine value again if needed |
296 | 6.00M | if ( (nCacheSecOffset == -1) || |
297 | 6.00M | ((nTicks - nCacheTicks) > 360000) || |
298 | 6.00M | ( nTicks < nCacheTicks ) // handle overflow |
299 | 6.00M | ) |
300 | 20 | { |
301 | 20 | nTime = time( nullptr ); |
302 | 20 | localtime_r( &nTime, &aTM ); |
303 | 20 | auto nLocalTime = mktime( &aTM ); |
304 | | #if defined(__sun) |
305 | | // Solaris gmtime_r() seems not to handle daylight saving time |
306 | | // flags correctly |
307 | | auto nUTC = nLocalTime + ( aTM.tm_isdst == 0 ? timezone : altzone ); |
308 | | #elif defined( LINUX ) |
309 | | // Linux mktime() seems not to handle tm_isdst correctly |
310 | 20 | auto nUTC = nLocalTime - aTM.tm_gmtoff; |
311 | | #else |
312 | | gmtime_r( &nTime, &aTM ); |
313 | | auto nUTC = mktime( &aTM ); |
314 | | #endif |
315 | 20 | nCacheTicks = nTicks; |
316 | 20 | nCacheSecOffset = (nLocalTime-nUTC) / 60; |
317 | 20 | } |
318 | | |
319 | 6.00M | nTempTime = abs( nCacheSecOffset ); |
320 | 6.00M | tools::Time aTime( 0, static_cast<sal_uInt16>(nTempTime) ); |
321 | 6.00M | if ( nCacheSecOffset < 0 ) |
322 | 0 | aTime = -aTime; |
323 | 6.00M | return aTime; |
324 | 6.00M | #endif |
325 | 6.00M | } |
326 | | |
327 | | sal_uInt64 tools::Time::GetSystemTicks() |
328 | 7.62M | { |
329 | 7.62M | return tools::Time::GetMonotonicTicks() / 1000; |
330 | 7.62M | } |
331 | | |
332 | | #ifdef _WIN32 |
333 | | static LARGE_INTEGER initPerformanceFrequency() |
334 | | { |
335 | | LARGE_INTEGER nTicksPerSecond = { 0, 0 }; |
336 | | if (!QueryPerformanceFrequency(&nTicksPerSecond)) |
337 | | nTicksPerSecond.QuadPart = 0; |
338 | | return nTicksPerSecond; |
339 | | } |
340 | | #endif |
341 | | |
342 | | sal_uInt64 tools::Time::GetMonotonicTicks() |
343 | 7.70M | { |
344 | | #ifdef _WIN32 |
345 | | static const LARGE_INTEGER nTicksPerSecond = initPerformanceFrequency(); |
346 | | if (nTicksPerSecond.QuadPart > 0) |
347 | | { |
348 | | LARGE_INTEGER nPerformanceCount; |
349 | | QueryPerformanceCounter(&nPerformanceCount); |
350 | | return static_cast<sal_uInt64>( |
351 | | ( nPerformanceCount.QuadPart * 1000 * 1000 ) / nTicksPerSecond.QuadPart ); |
352 | | } |
353 | | else |
354 | | { |
355 | | return static_cast<sal_uInt64>( timeGetTime() * 1000 ); |
356 | | } |
357 | | #else |
358 | 7.70M | sal_uInt64 nMicroSeconds; |
359 | | #ifdef __MACH__ |
360 | | static mach_timebase_info_data_t info = { 0, 0 }; |
361 | | if ( 0 == info.numer ) |
362 | | mach_timebase_info( &info ); |
363 | | nMicroSeconds = mach_absolute_time() * static_cast<double>(info.numer / info.denom) / 1000; |
364 | | #else |
365 | 7.70M | #if defined(_POSIX_TIMERS) |
366 | 7.70M | struct timespec currentTime; |
367 | 7.70M | clock_gettime( CLOCK_MONOTONIC, ¤tTime ); |
368 | 7.70M | nMicroSeconds |
369 | 7.70M | = static_cast<sal_uInt64>(currentTime.tv_sec) * 1000 * 1000 + currentTime.tv_nsec / 1000; |
370 | | #else |
371 | | struct timeval currentTime; |
372 | | gettimeofday( ¤tTime, nullptr ); |
373 | | nMicroSeconds = static_cast<sal_uInt64>(currentTime.tv_sec) * 1000 * 1000 + currentTime.tv_usec; |
374 | | #endif |
375 | 7.70M | #endif // __MACH__ |
376 | 7.70M | return nMicroSeconds; |
377 | 7.70M | #endif // _WIN32 |
378 | 7.70M | } |
379 | | |
380 | | } /* namespace tools */ |
381 | | |
382 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |