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 INCLUDED_TOOLS_TIME_HXX
20 : #define INCLUDED_TOOLS_TIME_HXX
21 :
22 : #include <tools/toolsdllapi.h>
23 : #include <tools/solar.h>
24 : #include <com/sun/star/util/Time.hpp>
25 :
26 : class ResId;
27 :
28 : /**
29 : @WARNING: This class can serve both as wall clock time and time duration, and
30 : the mixing of these concepts leads to problems such as there being
31 : 25 hours or 10 minus 20 seconds being (non-negative) 10 seconds.
32 : */
33 :
34 : namespace tools {
35 :
36 : class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Time
37 : {
38 : private:
39 : sal_Int64 nTime;
40 : void init( sal_uInt32 nHour, sal_uInt32 nMin,
41 : sal_uInt32 nSec, sal_uInt64 nNanoSec);
42 :
43 : public:
44 : enum TimeInitSystem
45 : {
46 : SYSTEM
47 : };
48 :
49 : // temporary until all uses are inspected and resolved
50 : enum TimeInitEmpty
51 : {
52 : EMPTY
53 : };
54 : static const sal_Int64 hourPerDay = 24;
55 : static const sal_Int64 minutePerHour = 60;
56 : static const sal_Int64 secondPerMinute = 60;
57 : static const sal_Int64 nanoSecPerSec = 1000000000;
58 : static const sal_Int64 nanoSecPerMinute = nanoSecPerSec * secondPerMinute;
59 : static const sal_Int64 nanoSecPerHour = nanoSecPerSec * secondPerMinute * minutePerHour;
60 : static const sal_Int64 nanoSecPerDay = nanoSecPerSec * secondPerMinute * minutePerHour * hourPerDay;
61 : static const sal_Int64 secondPerHour = secondPerMinute * minutePerHour;
62 : static const sal_Int64 secondPerDay = secondPerMinute * minutePerHour * hourPerDay;
63 : static const sal_Int64 minutePerDay = minutePerHour * hourPerDay;
64 : static const sal_Int64 nanoPerMicro = 1000;
65 : static const sal_Int64 nanoPerMilli = 1000000;
66 : static const sal_Int64 nanoPerCenti = 10000000;
67 :
68 83396 : Time( TimeInitEmpty )
69 83396 : { nTime = 0; }
70 : Time( TimeInitSystem );
71 : Time( const ResId & rResId );
72 4400 : Time( sal_Int64 _nTime ) { Time::nTime = _nTime; }
73 : Time( const tools::Time& rTime );
74 : Time( const ::com::sun::star::util::Time& rTime );
75 : Time( sal_uInt32 nHour, sal_uInt32 nMin,
76 : sal_uInt32 nSec = 0, sal_uInt64 nNanoSec = 0 );
77 :
78 1062 : void SetTime( sal_Int64 nNewTime ) { nTime = nNewTime; }
79 5788 : sal_Int64 GetTime() const { return nTime; }
80 36 : ::com::sun::star::util::Time GetUNOTime() const { return ::com::sun::star::util::Time(GetNanoSec(),GetSec(),GetMin(),GetHour(),false); }
81 :
82 : void SetHour( sal_uInt16 nNewHour );
83 : void SetMin( sal_uInt16 nNewMin );
84 : void SetSec( sal_uInt16 nNewSec );
85 : void SetNanoSec( sal_uInt32 nNewNanoSec );
86 24206 : sal_uInt16 GetHour() const
87 24206 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
88 24206 : return static_cast<sal_uInt16>(nTempTime / SAL_CONST_UINT64(10000000000000)); }
89 24006 : sal_uInt16 GetMin() const
90 24006 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
91 24006 : return static_cast<sal_uInt16>((nTempTime / SAL_CONST_UINT64(100000000000)) % 100); }
92 18332 : sal_uInt16 GetSec() const
93 18332 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
94 18332 : return static_cast<sal_uInt16>((nTempTime / SAL_CONST_UINT64(1000000000)) % 100); }
95 17864 : sal_uInt32 GetNanoSec() const
96 17864 : { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
97 17864 : return static_cast<sal_uInt32>( nTempTime % SAL_CONST_UINT64(1000000000)); }
98 :
99 : // TODO: consider removing GetMSFromTime and MakeTimeFromMS?
100 : sal_Int32 GetMSFromTime() const;
101 : void MakeTimeFromMS( sal_Int32 nMS );
102 : sal_Int64 GetNSFromTime() const;
103 : void MakeTimeFromNS( sal_Int64 nNS );
104 :
105 : /// 12 hours == 0.5 days
106 : double GetTimeInDays() const;
107 :
108 : bool IsBetween( const tools::Time& rFrom, const tools::Time& rTo ) const
109 : { return ((nTime >= rFrom.nTime) && (nTime <= rTo.nTime)); }
110 :
111 : bool IsEqualIgnoreNanoSec( const tools::Time& rTime ) const;
112 :
113 3247 : bool operator ==( const tools::Time& rTime ) const
114 3247 : { return (nTime == rTime.nTime); }
115 212 : bool operator !=( const tools::Time& rTime ) const
116 212 : { return (nTime != rTime.nTime); }
117 498 : bool operator >( const tools::Time& rTime ) const
118 498 : { return (nTime > rTime.nTime); }
119 566 : bool operator <( const tools::Time& rTime ) const
120 566 : { return (nTime < rTime.nTime); }
121 44 : bool operator >=( const tools::Time& rTime ) const
122 44 : { return (nTime >= rTime.nTime); }
123 0 : bool operator <=( const tools::Time& rTime ) const
124 0 : { return (nTime <= rTime.nTime); }
125 :
126 : static Time GetUTCOffset();
127 : static sal_uIntPtr GetSystemTicks(); // Elapsed time
128 :
129 : void ConvertToUTC() { *this -= Time::GetUTCOffset(); }
130 : void ConvertToLocalTime() { *this += Time::GetUTCOffset(); }
131 :
132 : tools::Time& operator =( const tools::Time& rTime );
133 4 : Time operator -() const
134 4 : { return Time( -nTime ); }
135 : tools::Time& operator +=( const tools::Time& rTime );
136 : tools::Time& operator -=( const tools::Time& rTime );
137 : TOOLS_DLLPUBLIC friend Time operator +( const tools::Time& rTime1, const tools::Time& rTime2 );
138 : TOOLS_DLLPUBLIC friend Time operator -( const tools::Time& rTime1, const tools::Time& rTime2 );
139 : };
140 :
141 408 : } /* namespace tools */
142 :
143 : #endif
144 :
145 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|