root / Community Core Vision / Windows / libs / poco / include / Poco / Timestamp.h @ 8

View | Annotate | Download (6.4 KB)

1
//
2
// Timestamp.h
3
//
4
// $Id: //poco/1.3/Foundation/include/Poco/Timestamp.h#2 $
5
//
6
// Library: Foundation
7
// Package: DateTime
8
// Module:  Timestamp
9
//
10
// Definition of the Timestamp class.
11
//
12
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// and Contributors.
14
//
15
// Permission is hereby granted, free of charge, to any person or organization
16
// obtaining a copy of the software and accompanying documentation covered by
17
// this license (the "Software") to use, reproduce, display, distribute,
18
// execute, and transmit the Software, and to prepare derivative works of the
19
// Software, and to permit third-parties to whom the Software is furnished to
20
// do so, all subject to the following:
21
// 
22
// The copyright notices in the Software and this entire statement, including
23
// the above license grant, this restriction and the following disclaimer,
24
// must be included in all copies of the Software, in whole or in part, and
25
// all derivative works of the Software, unless such copies or derivative
26
// works are solely in the form of machine-executable object code generated by
27
// a source language processor.
28
// 
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35
// DEALINGS IN THE SOFTWARE.
36
//
37
38
39
#ifndef Foundation_Timestamp_INCLUDED
40
#define Foundation_Timestamp_INCLUDED
41
42
43
#include "Poco/Foundation.h"
44
#include <ctime>
45
46
47
namespace Poco {
48
49
50
class Foundation_API Timestamp
51
        /// A Timestamp stores a monotonic time value
52
        /// with (theoretical) microseconds resolution.
53
        /// Timestamps can be compared with each other
54
        /// and simple arithmetics are supported.
55
        /// Timestamps are UTC (Coordinated Universal Time)
56
        /// based and thus independent of the timezone
57
        /// in effect on the system.
58
{
59
public:
60
        typedef Int64 TimeVal;    /// monotonic UTC time value in microsecond resolution
61
        typedef Int64 UtcTimeVal; /// monotonic UTC time value in 100 nanosecond resolution
62
        typedef Int64 TimeDiff;   /// difference between two timestamps in microseconds
63
64
        Timestamp();
65
                /// Creates a timestamp with the current time.
66
                
67
        Timestamp(TimeVal tv);
68
                /// Creates a timestamp from the given time value.
69
                
70
        Timestamp(const Timestamp& other);
71
                /// Copy constructor.
72
                
73
        ~Timestamp();
74
                /// Destroys the timestamp
75
                
76
        Timestamp& operator = (const Timestamp& other);
77
        Timestamp& operator = (TimeVal tv);
78
        
79
        void swap(Timestamp& timestamp);
80
                /// Swaps the Timestamp with another one.
81
        
82
        void update();
83
                /// Updates the Timestamp with the current time.
84
85
        bool operator == (const Timestamp& ts) const;
86
        bool operator != (const Timestamp& ts) const;
87
        bool operator >  (const Timestamp& ts) const;
88
        bool operator >= (const Timestamp& ts) const;
89
        bool operator <  (const Timestamp& ts) const;
90
        bool operator <= (const Timestamp& ts) const;
91
        
92
        Timestamp  operator +  (TimeDiff d) const;
93
        Timestamp  operator -  (TimeDiff d) const;
94
        TimeDiff   operator -  (const Timestamp& ts) const;
95
        Timestamp& operator += (TimeDiff d);
96
        Timestamp& operator -= (TimeDiff d);
97
        
98
        std::time_t epochTime() const;
99
                /// Returns the timestamp expressed in time_t.
100
                /// time_t base time is midnight, January 1, 1970.
101
                /// Resolution is one second.
102
                
103
        UtcTimeVal utcTime() const;
104
                /// Returns the timestamp expressed in UTC-based
105
                /// time. UTC base time is midnight, October 15, 1582.
106
                /// Resolution is 100 nanoseconds.
107
        
108
        TimeVal epochMicroseconds() const;
109
                /// Returns the timestamp expressed in microseconds
110
                /// since the Unix epoch, midnight, January 1, 1970.
111
        
112
        TimeDiff elapsed() const;
113
                /// Returns the time elapsed since the time denoted by
114
                /// the timestamp. Equivalent to Timestamp() - *this.
115
        
116
        bool isElapsed(TimeDiff interval) const;
117
                /// Returns true iff the given interval has passed
118
                /// since the time denoted by the timestamp.
119
        
120
        static Timestamp fromEpochTime(std::time_t t);
121
                /// Creates a timestamp from a std::time_t.
122
                
123
        static Timestamp fromUtcTime(UtcTimeVal val);
124
                /// Creates a timestamp from a UTC time value.
125
                
126
        static TimeVal resolution();
127
                /// Returns the resolution in units per second.
128
                /// Since the timestamp has microsecond resolution,
129
                /// the returned value is always 1000000.
130
131
#if defined(_WIN32)
132
        static Timestamp fromFileTimeNP(UInt32 fileTimeLow, UInt32 fileTimeHigh);
133
        void toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const;
134
#endif
135
136
private:
137
        TimeVal _ts;
138
};
139
140
141
//
142
// inlines
143
//
144
inline bool Timestamp::operator == (const Timestamp& ts) const
145
{
146
        return _ts == ts._ts;
147
}
148
149
150
inline bool Timestamp::operator != (const Timestamp& ts) const
151
{
152
        return _ts != ts._ts;
153
}
154
155
156
inline bool Timestamp::operator >  (const Timestamp& ts) const
157
{
158
        return _ts > ts._ts;
159
}
160
161
162
inline bool Timestamp::operator >= (const Timestamp& ts) const
163
{
164
        return _ts >= ts._ts;
165
}
166
167
168
inline bool Timestamp::operator <  (const Timestamp& ts) const
169
{
170
        return _ts < ts._ts;
171
}
172
173
174
inline bool Timestamp::operator <= (const Timestamp& ts) const
175
{
176
        return _ts <= ts._ts;
177
}
178
179
180
inline Timestamp Timestamp::operator + (Timestamp::TimeDiff d) const
181
{
182
        return Timestamp(_ts + d);
183
}
184
185
186
inline Timestamp Timestamp::operator - (Timestamp::TimeDiff d) const
187
{
188
        return Timestamp(_ts - d);
189
}
190
191
192
inline Timestamp::TimeDiff Timestamp::operator - (const Timestamp& ts) const
193
{
194
        return _ts - ts._ts;
195
}
196
197
198
inline Timestamp& Timestamp::operator += (Timestamp::TimeDiff d)
199
{
200
        _ts += d;
201
        return *this;
202
}
203
204
205
inline Timestamp& Timestamp::operator -= (Timestamp::TimeDiff d)
206
{
207
        _ts -= d;
208
        return *this;
209
}
210
211
212
inline std::time_t Timestamp::epochTime() const
213
{
214
        return std::time_t(_ts/resolution());
215
}
216
217
218
inline Timestamp::UtcTimeVal Timestamp::utcTime() const
219
{
220
        return _ts*10 + (TimeDiff(0x01b21dd2) << 32) + 0x13814000;
221
}
222
223
224
inline Timestamp::TimeVal Timestamp::epochMicroseconds() const
225
{
226
        return _ts;
227
}
228
229
230
inline Timestamp::TimeDiff Timestamp::elapsed() const
231
{
232
        Timestamp now;
233
        return now - *this;
234
}
235
236
237
inline bool Timestamp::isElapsed(Timestamp::TimeDiff interval) const
238
{
239
        Timestamp now;
240
        Timestamp::TimeDiff diff = now - *this;
241
        return diff >= interval;
242
}
243
244
245
inline Timestamp::TimeVal Timestamp::resolution()
246
{
247
        return 1000000;
248
}
249
250
251
inline void swap(Timestamp& s1, Timestamp& s2)
252
{
253
        s1.swap(s2);
254
}
255
256
257
} // namespace Poco
258
259
260
#endif // Foundation_Timestamp_INCLUDED