tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / include / tools / time.hxx
blob9b3d15f5ec225cce3f1a58d5decae7e9ee117929
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 #ifndef INCLUDED_TOOLS_TIME_HXX
20 #define INCLUDED_TOOLS_TIME_HXX
22 #include <sal/config.h>
24 #include <cmath>
26 #include <tools/toolsdllapi.h>
27 #include <com/sun/star/util/Time.hpp>
29 namespace com::sun::star::util { struct DateTime; }
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.
37 namespace tools {
39 class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Time
41 private:
42 sal_Int64 nTime;
43 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 short GetSign() const { return (nTime >= 0) ? +1 : -1; }
47 public:
48 enum TimeInitSystem
50 SYSTEM
53 // temporary until all uses are inspected and resolved
54 enum TimeInitEmpty
56 EMPTY
58 static const sal_Int64 hourPerDay = 24;
59 static const sal_Int64 minutePerHour = 60;
60 static const sal_Int64 secondPerMinute = 60;
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;
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;
79 explicit Time( TimeInitEmpty )
80 { 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 );
88 // The argument is not nanoseconds, it's what nTime must contain!
89 static Time fromEncodedTime(sal_Int64 _nTime) { return Time(_nTime); }
91 void SetTime( sal_Int64 nNewTime ) { nTime = nNewTime; }
92 sal_Int64 GetTime() const { return nTime; }
93 css::util::Time GetUNOTime() const { return css::util::Time(GetNanoSec(),GetSec(),GetMin(),GetHour(),false); }
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 sal_uInt16 GetHour() const { return std::abs(nTime) / SAL_CONST_UINT64(10000000000000); }
100 sal_uInt16 GetMin() const { return (std::abs(nTime) / SAL_CONST_UINT64(100000000000)) % 100; }
101 sal_uInt16 GetSec() const { return (std::abs(nTime) / SAL_CONST_UINT64(1000000000)) % 100; }
102 sal_uInt32 GetNanoSec() const { return std::abs(nTime) % SAL_CONST_UINT64(1000000000); }
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 );
110 /// 12 hours == 0.5 days
111 double GetTimeInDays() const;
113 /** Get the wall clock time particles for a (date+)time value.
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
120 A potential date component (fTimeInDays >= 1.0) is discarded.
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.
128 static void GetClock( double fTimeInDays,
129 sal_uInt16& nHour, sal_uInt16& nMinute, sal_uInt16& nSecond,
130 double& fFractionOfSecond, int nFractionDecimals );
132 bool IsEqualIgnoreNanoSec( const tools::Time& rTime ) const;
134 bool operator ==( const tools::Time& rTime ) const
135 { return (nTime == rTime.nTime); }
136 bool operator !=( const tools::Time& rTime ) const
137 { return (nTime != rTime.nTime); }
138 bool operator >( const tools::Time& rTime ) const
139 { return (nTime > rTime.nTime); }
140 bool operator <( const tools::Time& rTime ) const
141 { return (nTime < rTime.nTime); }
142 bool operator >=( const tools::Time& rTime ) const
143 { return (nTime >= rTime.nTime); }
144 bool operator <=( const tools::Time& rTime ) const
145 { return (nTime <= rTime.nTime); }
147 static Time GetUTCOffset();
150 * Elapsed time in milliseconds (1e-3) since some unspecified starting point
152 * Convenience function, which just calls GetMonotonicTicks() / 1000.
154 static sal_uInt64 GetSystemTicks();
157 * Elapsed time in microseconds (1e-6) since some unspecified starting point
159 * Uses the high-precision, monotonic time sources provided by the OS, if
160 * available. Don't try to relate it to the system time, and also it's long
161 * time accuracy is not the best.
163 * Currently used to measure the runtime of OpenCL shaders and to set a
164 * message creation timestamp to allow filtering of invalid timer messages.
166 * @return current system ticks in microseconds (1e-6s)
168 static sal_uInt64 GetMonotonicTicks();
170 tools::Time& operator =( const tools::Time& rTime ) = default;
171 Time operator -() const
172 { return Time( -nTime ); }
173 tools::Time& operator +=( const tools::Time& rTime );
174 tools::Time& operator -=( const tools::Time& rTime );
175 TOOLS_DLLPUBLIC friend Time operator +( const tools::Time& rTime1, const tools::Time& rTime2 );
176 TOOLS_DLLPUBLIC friend Time operator -( const tools::Time& rTime1, const tools::Time& rTime2 );
179 } /* namespace tools */
181 #endif
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */