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 : #ifndef _TOOLS_TIME_HXX
20 : #define _TOOLS_TIME_HXX
21 :
22 : #include "tools/toolsdllapi.h"
23 : #include <tools/solar.h>
24 :
25 : class ResId;
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 : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Time
34 : {
35 : private:
36 : sal_Int64 nTime;
37 :
38 : public:
39 : enum TimeInitSystem
40 : {
41 : SYSTEM
42 : };
43 :
44 : // temporary until all uses are inspected and resolved
45 : enum TimeInitEmpty
46 : {
47 : EMPTY
48 : };
49 : static const sal_Int64 hourPerDay = 24;
50 : static const sal_Int64 minutePerHour = 60;
51 : static const sal_Int64 secondPerMinute = 60;
52 : static const sal_Int64 nanoSecPerSec = 1000000000;
53 : static const sal_Int64 nanoSecPerMinute = nanoSecPerSec * secondPerMinute;
54 : static const sal_Int64 nanoSecPerHour = nanoSecPerSec * secondPerMinute * minutePerHour;
55 : static const sal_Int64 nanoSecPerDay = nanoSecPerSec * secondPerMinute * minutePerHour * hourPerDay;
56 : static const sal_Int64 secondPerHour = secondPerMinute * minutePerHour;
57 : static const sal_Int64 secondPerDay = secondPerMinute * minutePerHour * hourPerDay;
58 : static const sal_Int64 minutePerDay = minutePerHour * hourPerDay;
59 : static const sal_Int64 nanoPerMicro = 1000;
60 : static const sal_Int64 nanoPerMilli = 1000000;
61 : static const sal_Int64 nanoPerCenti = 10000000;
62 :
63 22482 : Time( TimeInitEmpty )
64 22482 : { nTime = 0; }
65 : Time( TimeInitSystem );
66 : Time( const ResId & rResId );
67 889 : Time( sal_Int64 _nTime ) { Time::nTime = _nTime; }
68 : Time( const Time& rTime );
69 : Time( sal_uInt32 nHour, sal_uInt32 nMin,
70 : sal_uInt32 nSec = 0, sal_uInt64 nNanoSec = 0 );
71 :
72 255 : void SetTime( sal_Int64 nNewTime ) { nTime = nNewTime; }
73 1396 : sal_Int64 GetTime() const { return nTime; }
74 :
75 : void SetHour( sal_uInt16 nNewHour );
76 : void SetMin( sal_uInt16 nNewMin );
77 : void SetSec( sal_uInt16 nNewSec );
78 : void SetNanoSec( sal_uInt32 nNewNanoSec );
79 4875 : sal_uInt16 GetHour() const
80 4875 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
81 4875 : return static_cast<sal_uInt16>(nTempTime / SAL_CONST_UINT64(10000000000000)); }
82 4775 : sal_uInt16 GetMin() const
83 4775 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
84 4775 : return static_cast<sal_uInt16>((nTempTime / SAL_CONST_UINT64(100000000000)) % 100); }
85 3757 : sal_uInt16 GetSec() const
86 3757 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
87 3757 : return static_cast<sal_uInt16>((nTempTime / SAL_CONST_UINT64(1000000000)) % 100); }
88 3653 : sal_uInt32 GetNanoSec() const
89 3653 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
90 3653 : return static_cast<sal_uInt32>( nTempTime % SAL_CONST_UINT64(1000000000)); }
91 :
92 : // TODO: consider removing GetMSFromTime and MakeTimeFromMS?
93 : sal_Int32 GetMSFromTime() const;
94 : void MakeTimeFromMS( sal_Int32 nMS );
95 : sal_Int64 GetNSFromTime() const;
96 : void MakeTimeFromNS( sal_Int64 nNS );
97 :
98 : /// 12 hours == 0.5 days
99 : double GetTimeInDays() const;
100 :
101 : bool IsBetween( const Time& rFrom, const Time& rTo ) const
102 : { return ((nTime >= rFrom.nTime) && (nTime <= rTo.nTime)); }
103 :
104 : bool IsEqualIgnoreNanoSec( const Time& rTime ) const;
105 :
106 169 : bool operator ==( const Time& rTime ) const
107 169 : { return (nTime == rTime.nTime); }
108 26 : bool operator !=( const Time& rTime ) const
109 26 : { return (nTime != rTime.nTime); }
110 242 : bool operator >( const Time& rTime ) const
111 242 : { return (nTime > rTime.nTime); }
112 178 : bool operator <( const Time& rTime ) const
113 178 : { return (nTime < rTime.nTime); }
114 4 : bool operator >=( const Time& rTime ) const
115 4 : { return (nTime >= rTime.nTime); }
116 0 : bool operator <=( const Time& rTime ) const
117 0 : { return (nTime <= rTime.nTime); }
118 :
119 : static Time GetUTCOffset();
120 : static sal_uIntPtr GetSystemTicks(); // Elapsed time
121 :
122 : void ConvertToUTC() { *this -= Time::GetUTCOffset(); }
123 : void ConvertToLocalTime() { *this += Time::GetUTCOffset(); }
124 :
125 : Time& operator =( const Time& rTime );
126 2 : Time operator -() const
127 2 : { return Time( -nTime ); }
128 : Time& operator +=( const Time& rTime );
129 : Time& operator -=( const Time& rTime );
130 : TOOLS_DLLPUBLIC friend Time operator +( const Time& rTime1, const Time& rTime2 );
131 : TOOLS_DLLPUBLIC friend Time operator -( const Time& rTime1, const Time& rTime2 );
132 141 : };
133 :
134 : #endif
135 :
136 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|