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