/src/libreoffice/include/tools/time.hxx
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 | | #ifndef INCLUDED_TOOLS_TIME_HXX |
20 | | #define INCLUDED_TOOLS_TIME_HXX |
21 | | |
22 | | #include <sal/config.h> |
23 | | |
24 | | #include <cmath> |
25 | | |
26 | | #include <tools/toolsdllapi.h> |
27 | | #include <com/sun/star/util/Time.hpp> |
28 | | |
29 | | namespace com::sun::star::util { struct DateTime; } |
30 | | |
31 | | /** |
32 | | @WARNING: This class can serve both as wall clock time and time duration, and |
33 | | the mixing of these concepts leads to problems such as there being |
34 | | 25 hours or 10 minus 20 seconds being (non-negative) 10 seconds. |
35 | | */ |
36 | | |
37 | | namespace tools { |
38 | | |
39 | | class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Time |
40 | | { |
41 | | private: |
42 | | sal_Int64 nTime; |
43 | 3.43k | explicit Time(sal_Int64 _nTime) { nTime = _nTime; } |
44 | | static sal_Int64 assemble(sal_uInt32 h, sal_uInt32 m, sal_uInt32 s, sal_uInt64 ns); |
45 | 11.6M | short GetSign() const { return (nTime >= 0) ? +1 : -1; } |
46 | | |
47 | | public: |
48 | | enum TimeInitSystem |
49 | | { |
50 | | SYSTEM |
51 | | }; |
52 | | |
53 | | // temporary until all uses are inspected and resolved |
54 | | enum TimeInitEmpty |
55 | | { |
56 | | EMPTY |
57 | | }; |
58 | | static const sal_Int64 hourPerDay = 24; |
59 | | static const sal_Int64 minutePerHour = 60; |
60 | | static const sal_Int64 secondPerMinute = 60; |
61 | | |
62 | | static const sal_Int64 nanoSecPerSec = 1000000000; |
63 | | static const sal_Int64 nanoSecPerMinute = nanoSecPerSec * secondPerMinute; |
64 | | static const sal_Int64 nanoSecPerHour = nanoSecPerSec * secondPerMinute * minutePerHour; |
65 | | static const sal_Int64 nanoSecPerDay = nanoSecPerSec * secondPerMinute * minutePerHour * hourPerDay; |
66 | | static const sal_Int64 secondPerHour = secondPerMinute * minutePerHour; |
67 | | static const sal_Int64 secondPerDay = secondPerMinute * minutePerHour * hourPerDay; |
68 | | static const sal_Int64 minutePerDay = minutePerHour * hourPerDay; |
69 | | static const sal_Int64 nanoPerMicro = 1000; |
70 | | static const sal_Int64 nanoPerMilli = 1000000; |
71 | | static const sal_Int64 nanoPerCenti = 10000000; |
72 | | |
73 | | static const sal_Int64 milliSecPerSec = 1000; |
74 | | static const sal_Int64 milliSecPerMinute = milliSecPerSec * secondPerMinute; |
75 | | static const sal_Int64 milliSecPerHour = milliSecPerMinute * minutePerHour; |
76 | | static const sal_Int64 milliSecPerDay = milliSecPerHour * hourPerDay; |
77 | | |
78 | | |
79 | | explicit Time( TimeInitEmpty ) |
80 | 7.74M | { nTime = 0; } |
81 | | explicit Time( TimeInitSystem ); |
82 | | Time( const tools::Time& rTime ) = default; |
83 | | explicit Time( const css::util::Time& rTime ); |
84 | | explicit Time( const css::util::DateTime& rDateTime ); |
85 | | Time( sal_uInt32 nHour, sal_uInt32 nMin, |
86 | | sal_uInt32 nSec = 0, sal_uInt64 nNanoSec = 0 ); |
87 | | |
88 | | // The argument is not nanoseconds, it's what nTime must contain! |
89 | 3.43k | static Time fromEncodedTime(sal_Int64 _nTime) { return Time(_nTime); } |
90 | | |
91 | 12.0M | void SetTime( sal_Int64 nNewTime ) { nTime = nNewTime; } |
92 | 12.6M | sal_Int64 GetTime() const { return nTime; } |
93 | 0 | css::util::Time GetUNOTime() const { return css::util::Time(GetNanoSec(),GetSec(),GetMin(),GetHour(),false); } |
94 | | |
95 | | void SetHour( sal_uInt16 nNewHour ); |
96 | | void SetMin( sal_uInt16 nNewMin ); |
97 | | void SetSec( sal_uInt16 nNewSec ); |
98 | | void SetNanoSec( sal_uInt32 nNewNanoSec ); |
99 | 18.1M | sal_uInt16 GetHour() const { return std::abs(nTime) / SAL_CONST_UINT64(10000000000000); } |
100 | 12.3M | sal_uInt16 GetMin() const { return (std::abs(nTime) / SAL_CONST_UINT64(100000000000)) % 100; } |
101 | 12.3M | sal_uInt16 GetSec() const { return (std::abs(nTime) / SAL_CONST_UINT64(1000000000)) % 100; } |
102 | 11.7M | sal_uInt32 GetNanoSec() const { return std::abs(nTime) % SAL_CONST_UINT64(1000000000); } |
103 | | |
104 | | // TODO: consider removing GetMSFromTime and MakeTimeFromMS? |
105 | | sal_Int32 GetMSFromTime() const; |
106 | | void MakeTimeFromMS( sal_Int32 nMS ); |
107 | | sal_Int64 GetNSFromTime() const; |
108 | | void MakeTimeFromNS( sal_Int64 nNS ); |
109 | | |
110 | | /// 12 hours == 0.5 days |
111 | | double GetTimeInDays() const; |
112 | | |
113 | | /** Get the wall clock time particles for a (date+)time value. |
114 | | |
115 | | Does the necessary rounding and truncating to obtain hour, minute, |
116 | | second and fraction of second from a double time value (time in days, |
117 | | 0.5 == 12h) such that individual values are not rounded up, i.e. |
118 | | x:59:59.999 does not yield x+1:0:0.00 |
119 | | |
120 | | A potential date component (fTimeInDays >= 1.0) is discarded. |
121 | | |
122 | | @param nFractionDecimals |
123 | | If > 0 fFractionOfSecond is truncated to that amount of |
124 | | decimals. |
125 | | Else fFractionOfSecond returns the full remainder of the |
126 | | fractional second. |
127 | | */ |
128 | | static void GetClock( double fTimeInDays, |
129 | | sal_uInt16& nHour, sal_uInt16& nMinute, sal_uInt16& nSecond, |
130 | | double& fFractionOfSecond, int nFractionDecimals ); |
131 | | |
132 | | bool IsEqualIgnoreNanoSec( const tools::Time& rTime ) const; |
133 | | |
134 | 0 | auto operator <=> ( const Time& rTime ) const = default; |
135 | | |
136 | | static Time GetUTCOffset(); |
137 | | |
138 | | /** |
139 | | * Elapsed time in milliseconds (1e-3) since some unspecified starting point |
140 | | * |
141 | | * Convenience function, which just calls GetMonotonicTicks() / 1000. |
142 | | */ |
143 | | static sal_uInt64 GetSystemTicks(); |
144 | | |
145 | | /** |
146 | | * Elapsed time in microseconds (1e-6) since some unspecified starting point |
147 | | * |
148 | | * Uses the high-precision, monotonic time sources provided by the OS, if |
149 | | * available. Don't try to relate it to the system time, and also it's long |
150 | | * time accuracy is not the best. |
151 | | * |
152 | | * Currently used to measure the runtime of OpenCL shaders and to set a |
153 | | * message creation timestamp to allow filtering of invalid timer messages. |
154 | | * |
155 | | * @return current system ticks in microseconds (1e-6s) |
156 | | */ |
157 | | static sal_uInt64 GetMonotonicTicks(); |
158 | | |
159 | | tools::Time& operator =( const tools::Time& rTime ) = default; |
160 | | Time operator -() const |
161 | 0 | { return Time( -nTime ); } |
162 | | tools::Time& operator +=( const tools::Time& rTime ); |
163 | | tools::Time& operator -=( const tools::Time& rTime ); |
164 | | TOOLS_DLLPUBLIC friend Time operator +( const tools::Time& rTime1, const tools::Time& rTime2 ); |
165 | | TOOLS_DLLPUBLIC friend Time operator -( const tools::Time& rTime1, const tools::Time& rTime2 ); |
166 | | }; |
167 | | |
168 | | } /* namespace tools */ |
169 | | |
170 | | #endif |
171 | | |
172 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |