Branch libreoffice-5-0-4
[LibreOffice.git] / include / tools / time.hxx
blobe25b2de73f530b28b8fa2a3bb903e31e7f47b3a6
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 <tools/toolsdllapi.h>
23 #include <tools/solar.h>
24 #include <com/sun/star/util/Time.hpp>
25 #include <com/sun/star/util/DateTime.hpp>
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.
33 namespace tools {
35 class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Time
37 private:
38 sal_Int64 nTime;
39 void init( sal_uInt32 nHour, sal_uInt32 nMin,
40 sal_uInt32 nSec, sal_uInt64 nNanoSec);
42 public:
43 enum TimeInitSystem
45 SYSTEM
48 // temporary until all uses are inspected and resolved
49 enum TimeInitEmpty
51 EMPTY
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;
67 Time( TimeInitEmpty )
68 { nTime = 0; }
69 Time( TimeInitSystem );
70 Time( sal_Int64 _nTime ) { Time::nTime = _nTime; }
71 Time( const tools::Time& rTime );
72 Time( const ::com::sun::star::util::Time& rTime );
73 Time( const ::com::sun::star::util::DateTime& rDateTime );
74 Time( sal_uInt32 nHour, sal_uInt32 nMin,
75 sal_uInt32 nSec = 0, sal_uInt64 nNanoSec = 0 );
77 void SetTime( sal_Int64 nNewTime ) { nTime = nNewTime; }
78 sal_Int64 GetTime() const { return nTime; }
79 ::com::sun::star::util::Time GetUNOTime() const { return ::com::sun::star::util::Time(GetNanoSec(),GetSec(),GetMin(),GetHour(),false); }
81 void SetHour( sal_uInt16 nNewHour );
82 void SetMin( sal_uInt16 nNewMin );
83 void SetSec( sal_uInt16 nNewSec );
84 void SetNanoSec( sal_uInt32 nNewNanoSec );
85 sal_uInt16 GetHour() const
86 { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
87 return static_cast<sal_uInt16>(nTempTime / SAL_CONST_UINT64(10000000000000)); }
88 sal_uInt16 GetMin() const
89 { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
90 return static_cast<sal_uInt16>((nTempTime / SAL_CONST_UINT64(100000000000)) % 100); }
91 sal_uInt16 GetSec() const
92 { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
93 return static_cast<sal_uInt16>((nTempTime / SAL_CONST_UINT64(1000000000)) % 100); }
94 sal_uInt32 GetNanoSec() const
95 { sal_uInt64 nTempTime = (nTime >= 0) ? nTime : -nTime;
96 return static_cast<sal_uInt32>( nTempTime % SAL_CONST_UINT64(1000000000)); }
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 );
104 /// 12 hours == 0.5 days
105 double GetTimeInDays() const;
107 bool IsBetween( const tools::Time& rFrom, const tools::Time& rTo ) const
108 { return ((nTime >= rFrom.nTime) && (nTime <= rTo.nTime)); }
110 bool IsEqualIgnoreNanoSec( const tools::Time& rTime ) const;
112 bool operator ==( const tools::Time& rTime ) const
113 { return (nTime == rTime.nTime); }
114 bool operator !=( const tools::Time& rTime ) const
115 { return (nTime != rTime.nTime); }
116 bool operator >( const tools::Time& rTime ) const
117 { return (nTime > rTime.nTime); }
118 bool operator <( const tools::Time& rTime ) const
119 { return (nTime < rTime.nTime); }
120 bool operator >=( const tools::Time& rTime ) const
121 { return (nTime >= rTime.nTime); }
122 bool operator <=( const tools::Time& rTime ) const
123 { return (nTime <= rTime.nTime); }
125 static Time GetUTCOffset();
127 /// Elapsed time since epoch in milliseconds
128 static sal_uInt64 GetSystemTicks();
130 void ConvertToUTC() { *this -= Time::GetUTCOffset(); }
131 void ConvertToLocalTime() { *this += Time::GetUTCOffset(); }
133 tools::Time& operator =( const tools::Time& rTime );
134 Time operator -() const
135 { return Time( -nTime ); }
136 tools::Time& operator +=( const tools::Time& rTime );
137 tools::Time& operator -=( const tools::Time& rTime );
138 TOOLS_DLLPUBLIC friend Time operator +( const tools::Time& rTime1, const tools::Time& rTime2 );
139 TOOLS_DLLPUBLIC friend Time operator -( const tools::Time& rTime1, const tools::Time& rTime2 );
142 } /* namespace tools */
144 #endif
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */