/src/brpc/src/butil/time/time.h
Line | Count | Source |
1 | | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 | | // Use of this source code is governed by a BSD-style license that can be |
3 | | // found in the LICENSE file. |
4 | | |
5 | | // Time represents an absolute point in coordinated universal time (UTC), |
6 | | // internally represented as microseconds (s/1,000,000) since the Windows epoch |
7 | | // (1601-01-01 00:00:00 UTC) (See http://crbug.com/14734). System-dependent |
8 | | // clock interface routines are defined in time_PLATFORM.cc. |
9 | | // |
10 | | // TimeDelta represents a duration of time, internally represented in |
11 | | // microseconds. |
12 | | // |
13 | | // TimeTicks represents an abstract time that is most of the time incrementing |
14 | | // for use in measuring time durations. It is internally represented in |
15 | | // microseconds. It can not be converted to a human-readable time, but is |
16 | | // guaranteed not to decrease (if the user changes the computer clock, |
17 | | // Time::Now() may actually decrease or jump). But note that TimeTicks may |
18 | | // "stand still", for example if the computer suspended. |
19 | | // |
20 | | // These classes are represented as only a 64-bit value, so they can be |
21 | | // efficiently passed by value. |
22 | | |
23 | | #ifndef BUTIL_TIME_TIME_H_ |
24 | | #define BUTIL_TIME_TIME_H_ |
25 | | |
26 | | #include <time.h> |
27 | | |
28 | | #include "butil/base_export.h" |
29 | | #include "butil/basictypes.h" |
30 | | #include "butil/build_config.h" |
31 | | |
32 | | #if defined(OS_MACOSX) |
33 | | #include <CoreFoundation/CoreFoundation.h> |
34 | | // Avoid Mac system header macro leak. |
35 | | #undef TYPE_BOOL |
36 | | #endif |
37 | | |
38 | | #if defined(OS_POSIX) |
39 | | #include <unistd.h> |
40 | | #include <sys/time.h> |
41 | | #endif |
42 | | |
43 | | #if defined(OS_WIN) |
44 | | // For FILETIME in FromFileTime, until it moves to a new converter class. |
45 | | // See TODO(iyengar) below. |
46 | | #include <windows.h> |
47 | | #endif |
48 | | |
49 | | #include <limits> |
50 | | |
51 | | #ifdef BAIDU_INTERNAL |
52 | | // gejun: Force inclusion of ul_def.h and undef conflict macros |
53 | | #include <ul_def.h> |
54 | | #undef Uchar |
55 | | #undef Ushort |
56 | | #undef Uint |
57 | | #undef Max |
58 | | #undef Min |
59 | | #undef Exchange |
60 | | #endif // BAIDU_INTERNAL |
61 | | |
62 | | namespace butil { |
63 | | |
64 | | class Time; |
65 | | class TimeTicks; |
66 | | |
67 | | // TimeDelta ------------------------------------------------------------------ |
68 | | |
69 | | class BUTIL_EXPORT TimeDelta { |
70 | | public: |
71 | 0 | TimeDelta() : delta_(0) { |
72 | 0 | } |
73 | | |
74 | | // Required by clang++ 11 on MacOS catalina |
75 | 0 | TimeDelta(const TimeDelta& other) : delta_(other.delta_) {} |
76 | | |
77 | | // Converts units of time to TimeDeltas. |
78 | | static TimeDelta FromDays(int days); |
79 | | static TimeDelta FromHours(int hours); |
80 | | static TimeDelta FromMinutes(int minutes); |
81 | | static TimeDelta FromSeconds(int64_t secs); |
82 | | static TimeDelta FromMilliseconds(int64_t ms); |
83 | | static TimeDelta FromMicroseconds(int64_t us); |
84 | | static TimeDelta FromSecondsD(double secs); |
85 | | static TimeDelta FromMillisecondsD(double ms); |
86 | | static TimeDelta FromMicrosecondsD(double us); |
87 | | |
88 | | #if defined(OS_WIN) |
89 | | static TimeDelta FromQPCValue(LONGLONG qpc_value); |
90 | | #endif |
91 | | |
92 | | // Converts an integer value representing TimeDelta to a class. This is used |
93 | | // when deserializing a |TimeDelta| structure, using a value known to be |
94 | | // compatible. It is not provided as a constructor because the integer type |
95 | | // may be unclear from the perspective of a caller. |
96 | 0 | static TimeDelta FromInternalValue(int64_t delta) { |
97 | 0 | return TimeDelta(delta); |
98 | 0 | } |
99 | | |
100 | | // Returns the maximum time delta, which should be greater than any reasonable |
101 | | // time delta we might compare it to. Adding or subtracting the maximum time |
102 | | // delta to a time or another time delta has an undefined result. |
103 | | static TimeDelta Max(); |
104 | | |
105 | | // Returns the internal numeric value of the TimeDelta object. Please don't |
106 | | // use this and do arithmetic on it, as it is more error prone than using the |
107 | | // provided operators. |
108 | | // For serializing, use FromInternalValue to reconstitute. |
109 | 0 | int64_t ToInternalValue() const { |
110 | 0 | return delta_; |
111 | 0 | } |
112 | | |
113 | | // Returns true if the time delta is the maximum time delta. |
114 | 0 | bool is_max() const { |
115 | 0 | return delta_ == std::numeric_limits<int64_t>::max(); |
116 | 0 | } |
117 | | |
118 | | #if defined(OS_POSIX) |
119 | | struct timespec ToTimeSpec() const; |
120 | | #endif |
121 | | |
122 | | // Returns the time delta in some unit. The F versions return a floating |
123 | | // point value, the "regular" versions return a rounded-down value. |
124 | | // |
125 | | // InMillisecondsRoundedUp() instead returns an integer that is rounded up |
126 | | // to the next full millisecond. |
127 | | int InDays() const; |
128 | | int InHours() const; |
129 | | int InMinutes() const; |
130 | | double InSecondsF() const; |
131 | | int64_t InSeconds() const; |
132 | | double InMillisecondsF() const; |
133 | | int64_t InMilliseconds() const; |
134 | | int64_t InMillisecondsRoundedUp() const; |
135 | | int64_t InMicroseconds() const; |
136 | | |
137 | 0 | TimeDelta& operator=(TimeDelta other) { |
138 | 0 | delta_ = other.delta_; |
139 | 0 | return *this; |
140 | 0 | } |
141 | | |
142 | | // Computations with other deltas. |
143 | 0 | TimeDelta operator+(TimeDelta other) const { |
144 | 0 | return TimeDelta(delta_ + other.delta_); |
145 | 0 | } |
146 | 0 | TimeDelta operator-(TimeDelta other) const { |
147 | 0 | return TimeDelta(delta_ - other.delta_); |
148 | 0 | } |
149 | | |
150 | 0 | TimeDelta& operator+=(TimeDelta other) { |
151 | 0 | delta_ += other.delta_; |
152 | 0 | return *this; |
153 | 0 | } |
154 | 0 | TimeDelta& operator-=(TimeDelta other) { |
155 | 0 | delta_ -= other.delta_; |
156 | 0 | return *this; |
157 | 0 | } |
158 | 0 | TimeDelta operator-() const { |
159 | 0 | return TimeDelta(-delta_); |
160 | 0 | } |
161 | | |
162 | | // Computations with ints, note that we only allow multiplicative operations |
163 | | // with ints, and additive operations with other deltas. |
164 | 0 | TimeDelta operator*(int64_t a) const { |
165 | 0 | return TimeDelta(delta_ * a); |
166 | 0 | } |
167 | 0 | TimeDelta operator/(int64_t a) const { |
168 | 0 | return TimeDelta(delta_ / a); |
169 | 0 | } |
170 | 0 | TimeDelta& operator*=(int64_t a) { |
171 | 0 | delta_ *= a; |
172 | 0 | return *this; |
173 | 0 | } |
174 | 0 | TimeDelta& operator/=(int64_t a) { |
175 | 0 | delta_ /= a; |
176 | 0 | return *this; |
177 | 0 | } |
178 | 0 | int64_t operator/(TimeDelta a) const { |
179 | 0 | return delta_ / a.delta_; |
180 | 0 | } |
181 | | |
182 | | // Defined below because it depends on the definition of the other classes. |
183 | | Time operator+(Time t) const; |
184 | | TimeTicks operator+(TimeTicks t) const; |
185 | | |
186 | | // Comparison operators. |
187 | 0 | bool operator==(TimeDelta other) const { |
188 | 0 | return delta_ == other.delta_; |
189 | 0 | } |
190 | 0 | bool operator!=(TimeDelta other) const { |
191 | 0 | return delta_ != other.delta_; |
192 | 0 | } |
193 | 0 | bool operator<(TimeDelta other) const { |
194 | 0 | return delta_ < other.delta_; |
195 | 0 | } |
196 | 0 | bool operator<=(TimeDelta other) const { |
197 | 0 | return delta_ <= other.delta_; |
198 | 0 | } |
199 | 0 | bool operator>(TimeDelta other) const { |
200 | 0 | return delta_ > other.delta_; |
201 | 0 | } |
202 | 0 | bool operator>=(TimeDelta other) const { |
203 | 0 | return delta_ >= other.delta_; |
204 | 0 | } |
205 | | |
206 | | private: |
207 | | friend class Time; |
208 | | friend class TimeTicks; |
209 | | friend TimeDelta operator*(int64_t a, TimeDelta td); |
210 | | |
211 | | // Constructs a delta given the duration in microseconds. This is private |
212 | | // to avoid confusion by callers with an integer constructor. Use |
213 | | // FromSeconds, FromMilliseconds, etc. instead. |
214 | 0 | explicit TimeDelta(int64_t delta_us) : delta_(delta_us) { |
215 | 0 | } |
216 | | |
217 | | // Delta in microseconds. |
218 | | int64_t delta_; |
219 | | }; |
220 | | |
221 | 0 | inline TimeDelta operator*(int64_t a, TimeDelta td) { |
222 | 0 | return TimeDelta(a * td.delta_); |
223 | 0 | } |
224 | | |
225 | | // Time ----------------------------------------------------------------------- |
226 | | |
227 | | // Represents a wall clock time in UTC. |
228 | | class BUTIL_EXPORT Time { |
229 | | public: |
230 | | static const int64_t kMillisecondsPerSecond = 1000; |
231 | | static const int64_t kMicrosecondsPerMillisecond = 1000; |
232 | | static const int64_t kMicrosecondsPerSecond = kMicrosecondsPerMillisecond * |
233 | | kMillisecondsPerSecond; |
234 | | static const int64_t kMicrosecondsPerMinute = kMicrosecondsPerSecond * 60; |
235 | | static const int64_t kMicrosecondsPerHour = kMicrosecondsPerMinute * 60; |
236 | | static const int64_t kMicrosecondsPerDay = kMicrosecondsPerHour * 24; |
237 | | static const int64_t kMicrosecondsPerWeek = kMicrosecondsPerDay * 7; |
238 | | static const int64_t kNanosecondsPerMicrosecond = 1000; |
239 | | static const int64_t kNanosecondsPerSecond = kNanosecondsPerMicrosecond * |
240 | | kMicrosecondsPerSecond; |
241 | | |
242 | | #if !defined(OS_WIN) |
243 | | // On Mac & Linux, this value is the delta from the Windows epoch of 1601 to |
244 | | // the Posix delta of 1970. This is used for migrating between the old |
245 | | // 1970-based epochs to the new 1601-based ones. It should be removed from |
246 | | // this global header and put in the platform-specific ones when we remove the |
247 | | // migration code. |
248 | | static const int64_t kWindowsEpochDeltaMicroseconds; |
249 | | #endif |
250 | | |
251 | | // Represents an exploded time that can be formatted nicely. This is kind of |
252 | | // like the Win32 SYSTEMTIME structure or the Unix "struct tm" with a few |
253 | | // additions and changes to prevent errors. |
254 | | struct BUTIL_EXPORT Exploded { |
255 | | int year; // Four digit year "2007" |
256 | | int month; // 1-based month (values 1 = January, etc.) |
257 | | int day_of_week; // 0-based day of week (0 = Sunday, etc.) |
258 | | int day_of_month; // 1-based day of month (1-31) |
259 | | int hour; // Hour within the current day (0-23) |
260 | | int minute; // Minute within the current hour (0-59) |
261 | | int second; // Second within the current minute (0-59 plus leap |
262 | | // seconds which may take it up to 60). |
263 | | int millisecond; // Milliseconds within the current second (0-999) |
264 | | |
265 | | // A cursory test for whether the data members are within their |
266 | | // respective ranges. A 'true' return value does not guarantee the |
267 | | // Exploded value can be successfully converted to a Time value. |
268 | | bool HasValidValues() const; |
269 | | }; |
270 | | |
271 | | // Contains the NULL time. Use Time::Now() to get the current time. |
272 | 0 | Time() : us_(0) { |
273 | 0 | } |
274 | | |
275 | | // Required by clang++ 11 on MacOS catalina |
276 | 0 | Time(const Time& other) : us_(other.us_) {} |
277 | | |
278 | | // Returns true if the time object has not been initialized. |
279 | 0 | bool is_null() const { |
280 | 0 | return us_ == 0; |
281 | 0 | } |
282 | | |
283 | | // Returns true if the time object is the maximum time. |
284 | 0 | bool is_max() const { |
285 | 0 | return us_ == std::numeric_limits<int64_t>::max(); |
286 | 0 | } |
287 | | |
288 | | // Returns the time for epoch in Unix-like system (Jan 1, 1970). |
289 | | static Time UnixEpoch(); |
290 | | |
291 | | // Returns the current time. Watch out, the system might adjust its clock |
292 | | // in which case time will actually go backwards. We don't guarantee that |
293 | | // times are increasing, or that two calls to Now() won't be the same. |
294 | | static Time Now(); |
295 | | |
296 | | // Returns the maximum time, which should be greater than any reasonable time |
297 | | // with which we might compare it. |
298 | | static Time Max(); |
299 | | |
300 | | // Returns the current time. Same as Now() except that this function always |
301 | | // uses system time so that there are no discrepancies between the returned |
302 | | // time and system time even on virtual environments including our test bot. |
303 | | // For timing sensitive unittests, this function should be used. |
304 | | static Time NowFromSystemTime(); |
305 | | |
306 | | // Converts to/from time_t in UTC and a Time class. |
307 | | // TODO(brettw) this should be removed once everybody starts using the |Time| |
308 | | // class. |
309 | | static Time FromTimeT(time_t tt); |
310 | | time_t ToTimeT() const; |
311 | | |
312 | | // Converts time to/from a double which is the number of seconds since epoch |
313 | | // (Jan 1, 1970). Webkit uses this format to represent time. |
314 | | // Because WebKit initializes double time value to 0 to indicate "not |
315 | | // initialized", we map it to empty Time object that also means "not |
316 | | // initialized". |
317 | | static Time FromDoubleT(double dt); |
318 | | double ToDoubleT() const; |
319 | | |
320 | | #if defined(OS_POSIX) |
321 | | // Converts the timespec structure to time. MacOS X 10.8.3 (and tentatively, |
322 | | // earlier versions) will have the |ts|'s tv_nsec component zeroed out, |
323 | | // having a 1 second resolution, which agrees with |
324 | | // https://developer.apple.com/legacy/library/#technotes/tn/tn1150.html#HFSPlusDates. |
325 | | static Time FromTimeSpec(const timespec& ts); |
326 | | #endif |
327 | | |
328 | | // Converts to/from the Javascript convention for times, a number of |
329 | | // milliseconds since the epoch: |
330 | | // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getTime. |
331 | | static Time FromJsTime(double ms_since_epoch); |
332 | | double ToJsTime() const; |
333 | | |
334 | | // Converts to Java convention for times, a number of |
335 | | // milliseconds since the epoch. |
336 | | int64_t ToJavaTime() const; |
337 | | |
338 | | #if defined(OS_POSIX) |
339 | | static Time FromTimeVal(struct timeval t); |
340 | | struct timeval ToTimeVal() const; |
341 | | #endif |
342 | | |
343 | | #if defined(OS_MACOSX) |
344 | | static Time FromCFAbsoluteTime(CFAbsoluteTime t); |
345 | | CFAbsoluteTime ToCFAbsoluteTime() const; |
346 | | #endif |
347 | | |
348 | | #if defined(OS_WIN) |
349 | | static Time FromFileTime(FILETIME ft); |
350 | | FILETIME ToFileTime() const; |
351 | | |
352 | | // The minimum time of a low resolution timer. This is basically a windows |
353 | | // constant of ~15.6ms. While it does vary on some older OS versions, we'll |
354 | | // treat it as static across all windows versions. |
355 | | static const int kMinLowResolutionThresholdMs = 16; |
356 | | |
357 | | // Enable or disable Windows high resolution timer. If the high resolution |
358 | | // timer is not enabled, calls to ActivateHighResolutionTimer will fail. |
359 | | // When disabling the high resolution timer, this function will not cause |
360 | | // the high resolution timer to be deactivated, but will prevent future |
361 | | // activations. |
362 | | // Must be called from the main thread. |
363 | | // For more details see comments in time_win.cc. |
364 | | static void EnableHighResolutionTimer(bool enable); |
365 | | |
366 | | // Activates or deactivates the high resolution timer based on the |activate| |
367 | | // flag. If the HighResolutionTimer is not Enabled (see |
368 | | // EnableHighResolutionTimer), this function will return false. Otherwise |
369 | | // returns true. Each successful activate call must be paired with a |
370 | | // subsequent deactivate call. |
371 | | // All callers to activate the high resolution timer must eventually call |
372 | | // this function to deactivate the high resolution timer. |
373 | | static bool ActivateHighResolutionTimer(bool activate); |
374 | | |
375 | | // Returns true if the high resolution timer is both enabled and activated. |
376 | | // This is provided for testing only, and is not tracked in a thread-safe |
377 | | // way. |
378 | | static bool IsHighResolutionTimerInUse(); |
379 | | #endif |
380 | | |
381 | | // Converts an exploded structure representing either the local time or UTC |
382 | | // into a Time class. |
383 | 0 | static Time FromUTCExploded(const Exploded& exploded) { |
384 | 0 | return FromExploded(false, exploded); |
385 | 0 | } |
386 | 0 | static Time FromLocalExploded(const Exploded& exploded) { |
387 | 0 | return FromExploded(true, exploded); |
388 | 0 | } |
389 | | |
390 | | // Converts an integer value representing Time to a class. This is used |
391 | | // when deserializing a |Time| structure, using a value known to be |
392 | | // compatible. It is not provided as a constructor because the integer type |
393 | | // may be unclear from the perspective of a caller. |
394 | 0 | static Time FromInternalValue(int64_t us) { |
395 | 0 | return Time(us); |
396 | 0 | } |
397 | | |
398 | | // Converts a string representation of time to a Time object. |
399 | | // An example of a time string which is converted is as below:- |
400 | | // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
401 | | // in the input string, FromString assumes local time and FromUTCString |
402 | | // assumes UTC. A timezone that cannot be parsed (e.g. "UTC" which is not |
403 | | // specified in RFC822) is treated as if the timezone is not specified. |
404 | | // TODO(iyengar) Move the FromString/FromTimeT/ToTimeT/FromFileTime to |
405 | | // a new time converter class. |
406 | 0 | static bool FromString(const char* time_string, Time* parsed_time) { |
407 | 0 | return FromStringInternal(time_string, true, parsed_time); |
408 | 0 | } |
409 | 0 | static bool FromUTCString(const char* time_string, Time* parsed_time) { |
410 | 0 | return FromStringInternal(time_string, false, parsed_time); |
411 | 0 | } |
412 | | |
413 | | // For serializing, use FromInternalValue to reconstitute. Please don't use |
414 | | // this and do arithmetic on it, as it is more error prone than using the |
415 | | // provided operators. |
416 | 0 | int64_t ToInternalValue() const { |
417 | 0 | return us_; |
418 | 0 | } |
419 | | |
420 | | // Fills the given exploded structure with either the local time or UTC from |
421 | | // this time structure (containing UTC). |
422 | 0 | void UTCExplode(Exploded* exploded) const { |
423 | 0 | return Explode(false, exploded); |
424 | 0 | } |
425 | 0 | void LocalExplode(Exploded* exploded) const { |
426 | 0 | return Explode(true, exploded); |
427 | 0 | } |
428 | | |
429 | | // Rounds this time down to the nearest day in local time. It will represent |
430 | | // midnight on that day. |
431 | | Time LocalMidnight() const; |
432 | | |
433 | 0 | Time& operator=(Time other) { |
434 | 0 | us_ = other.us_; |
435 | 0 | return *this; |
436 | 0 | } |
437 | | |
438 | | // Compute the difference between two times. |
439 | 0 | TimeDelta operator-(Time other) const { |
440 | 0 | return TimeDelta(us_ - other.us_); |
441 | 0 | } |
442 | | |
443 | | // Modify by some time delta. |
444 | 0 | Time& operator+=(TimeDelta delta) { |
445 | 0 | us_ += delta.delta_; |
446 | 0 | return *this; |
447 | 0 | } |
448 | 0 | Time& operator-=(TimeDelta delta) { |
449 | 0 | us_ -= delta.delta_; |
450 | 0 | return *this; |
451 | 0 | } |
452 | | |
453 | | // Return a new time modified by some delta. |
454 | 0 | Time operator+(TimeDelta delta) const { |
455 | 0 | return Time(us_ + delta.delta_); |
456 | 0 | } |
457 | 0 | Time operator-(TimeDelta delta) const { |
458 | 0 | return Time(us_ - delta.delta_); |
459 | 0 | } |
460 | | |
461 | | // Comparison operators |
462 | 0 | bool operator==(Time other) const { |
463 | 0 | return us_ == other.us_; |
464 | 0 | } |
465 | 0 | bool operator!=(Time other) const { |
466 | 0 | return us_ != other.us_; |
467 | 0 | } |
468 | 0 | bool operator<(Time other) const { |
469 | 0 | return us_ < other.us_; |
470 | 0 | } |
471 | 0 | bool operator<=(Time other) const { |
472 | 0 | return us_ <= other.us_; |
473 | 0 | } |
474 | 0 | bool operator>(Time other) const { |
475 | 0 | return us_ > other.us_; |
476 | 0 | } |
477 | 0 | bool operator>=(Time other) const { |
478 | 0 | return us_ >= other.us_; |
479 | 0 | } |
480 | | |
481 | | private: |
482 | | friend class TimeDelta; |
483 | | |
484 | 0 | explicit Time(int64_t us) : us_(us) { |
485 | 0 | } |
486 | | |
487 | | // Explodes the given time to either local time |is_local = true| or UTC |
488 | | // |is_local = false|. |
489 | | void Explode(bool is_local, Exploded* exploded) const; |
490 | | |
491 | | // Unexplodes a given time assuming the source is either local time |
492 | | // |is_local = true| or UTC |is_local = false|. |
493 | | static Time FromExploded(bool is_local, const Exploded& exploded); |
494 | | |
495 | | // Converts a string representation of time to a Time object. |
496 | | // An example of a time string which is converted is as below:- |
497 | | // "Tue, 15 Nov 1994 12:45:26 GMT". If the timezone is not specified |
498 | | // in the input string, local time |is_local = true| or |
499 | | // UTC |is_local = false| is assumed. A timezone that cannot be parsed |
500 | | // (e.g. "UTC" which is not specified in RFC822) is treated as if the |
501 | | // timezone is not specified. |
502 | | static bool FromStringInternal(const char* time_string, |
503 | | bool is_local, |
504 | | Time* parsed_time); |
505 | | |
506 | | // The representation of Jan 1, 1970 UTC in microseconds since the |
507 | | // platform-dependent epoch. |
508 | | static const int64_t kTimeTToMicrosecondsOffset; |
509 | | |
510 | | #if defined(OS_WIN) |
511 | | // Indicates whether fast timers are usable right now. For instance, |
512 | | // when using battery power, we might elect to prevent high speed timers |
513 | | // which would draw more power. |
514 | | static bool high_resolution_timer_enabled_; |
515 | | // Count of activations on the high resolution timer. Only use in tests |
516 | | // which are single threaded. |
517 | | static int high_resolution_timer_activated_; |
518 | | #endif |
519 | | |
520 | | // Time in microseconds in UTC. |
521 | | int64_t us_; |
522 | | }; |
523 | | |
524 | | // Inline the TimeDelta factory methods, for fast TimeDelta construction. |
525 | | |
526 | | // static |
527 | 0 | inline TimeDelta TimeDelta::FromDays(int days) { |
528 | 0 | // Preserve max to prevent overflow. |
529 | 0 | if (days == std::numeric_limits<int>::max()) |
530 | 0 | return Max(); |
531 | 0 | return TimeDelta(days * Time::kMicrosecondsPerDay); |
532 | 0 | } |
533 | | |
534 | | // static |
535 | 0 | inline TimeDelta TimeDelta::FromHours(int hours) { |
536 | 0 | // Preserve max to prevent overflow. |
537 | 0 | if (hours == std::numeric_limits<int>::max()) |
538 | 0 | return Max(); |
539 | 0 | return TimeDelta(hours * Time::kMicrosecondsPerHour); |
540 | 0 | } |
541 | | |
542 | | // static |
543 | 0 | inline TimeDelta TimeDelta::FromMinutes(int minutes) { |
544 | 0 | // Preserve max to prevent overflow. |
545 | 0 | if (minutes == std::numeric_limits<int>::max()) |
546 | 0 | return Max(); |
547 | 0 | return TimeDelta(minutes * Time::kMicrosecondsPerMinute); |
548 | 0 | } |
549 | | |
550 | | // static |
551 | 0 | inline TimeDelta TimeDelta::FromSeconds(int64_t secs) { |
552 | | // Preserve max to prevent overflow. |
553 | 0 | if (secs == std::numeric_limits<int64_t>::max()) |
554 | 0 | return Max(); |
555 | 0 | return TimeDelta(secs * Time::kMicrosecondsPerSecond); |
556 | 0 | } |
557 | | |
558 | | // static |
559 | 0 | inline TimeDelta TimeDelta::FromMilliseconds(int64_t ms) { |
560 | 0 | // Preserve max to prevent overflow. |
561 | 0 | if (ms == std::numeric_limits<int64_t>::max()) |
562 | 0 | return Max(); |
563 | 0 | return TimeDelta(ms * Time::kMicrosecondsPerMillisecond); |
564 | 0 | } |
565 | | |
566 | | // static |
567 | 0 | inline TimeDelta TimeDelta::FromSecondsD(double secs) { |
568 | 0 | // Preserve max to prevent overflow. |
569 | 0 | if (secs == std::numeric_limits<double>::infinity()) |
570 | 0 | return Max(); |
571 | 0 | return TimeDelta((int64_t)(secs * Time::kMicrosecondsPerSecond)); |
572 | 0 | } |
573 | | |
574 | | // static |
575 | 0 | inline TimeDelta TimeDelta::FromMillisecondsD(double ms) { |
576 | 0 | // Preserve max to prevent overflow. |
577 | 0 | if (ms == std::numeric_limits<double>::infinity()) |
578 | 0 | return Max(); |
579 | 0 | return TimeDelta((int64_t)(ms * Time::kMicrosecondsPerMillisecond)); |
580 | 0 | } |
581 | | |
582 | | // static |
583 | 0 | inline TimeDelta TimeDelta::FromMicroseconds(int64_t us) { |
584 | | // Preserve max to prevent overflow. |
585 | 0 | if (us == std::numeric_limits<int64_t>::max()) |
586 | 0 | return Max(); |
587 | 0 | return TimeDelta(us); |
588 | 0 | } |
589 | | |
590 | | // static |
591 | 0 | inline TimeDelta TimeDelta::FromMicrosecondsD(double us) { |
592 | 0 | // Preserve max to prevent overflow. |
593 | 0 | if (us == std::numeric_limits<double>::infinity()) |
594 | 0 | return Max(); |
595 | 0 | return TimeDelta((int64_t)us); |
596 | 0 | } |
597 | | |
598 | 0 | inline Time TimeDelta::operator+(Time t) const { |
599 | 0 | return Time(t.us_ + delta_); |
600 | 0 | } |
601 | | |
602 | | // TimeTicks ------------------------------------------------------------------ |
603 | | |
604 | | class BUTIL_EXPORT TimeTicks { |
605 | | public: |
606 | | // We define this even without OS_CHROMEOS for seccomp sandbox testing. |
607 | | #if defined(OS_LINUX) |
608 | | // Force definition of the system trace clock; it is a chromeos-only api |
609 | | // at the moment and surfacing it in the right place requires mucking |
610 | | // with glibc et al. |
611 | | static const clockid_t kClockSystemTrace = 11; |
612 | | #endif |
613 | | |
614 | 0 | TimeTicks() : ticks_(0) { |
615 | 0 | } |
616 | | |
617 | | // Required by clang++ 11 on MacOS catalina |
618 | 0 | TimeTicks(const TimeTicks& other) : ticks_(other.ticks_) {} |
619 | | |
620 | | // Platform-dependent tick count representing "right now." |
621 | | // The resolution of this clock is ~1-15ms. Resolution varies depending |
622 | | // on hardware/operating system configuration. |
623 | | static TimeTicks Now(); |
624 | | |
625 | | // Returns a platform-dependent high-resolution tick count. Implementation |
626 | | // is hardware dependent and may or may not return sub-millisecond |
627 | | // resolution. THIS CALL IS GENERALLY MUCH MORE EXPENSIVE THAN Now() AND |
628 | | // SHOULD ONLY BE USED WHEN IT IS REALLY NEEDED. |
629 | | static TimeTicks HighResNow(); |
630 | | |
631 | | static bool IsHighResNowFastAndReliable(); |
632 | | |
633 | | // FIXME(gejun): CLOCK_THREAD_CPUTIME_ID behaves differently in different |
634 | | // glibc. To avoid potential bug, disable ThreadNow() always. |
635 | | // Returns true if ThreadNow() is supported on this system. |
636 | 0 | static bool IsThreadNowSupported() { |
637 | 0 | return false; |
638 | 0 | } |
639 | | |
640 | | // Returns thread-specific CPU-time on systems that support this feature. |
641 | | // Needs to be guarded with a call to IsThreadNowSupported(). Use this timer |
642 | | // to (approximately) measure how much time the calling thread spent doing |
643 | | // actual work vs. being de-scheduled. May return bogus results if the thread |
644 | | // migrates to another CPU between two calls. |
645 | | static TimeTicks ThreadNow(); |
646 | | |
647 | | // Returns the current system trace time or, if none is defined, the current |
648 | | // high-res time (i.e. HighResNow()). On systems where a global trace clock |
649 | | // is defined, timestamping TraceEvents's with this value guarantees |
650 | | // synchronization between events collected inside chrome and events |
651 | | // collected outside (e.g. kernel, X server). |
652 | | static TimeTicks NowFromSystemTraceTime(); |
653 | | |
654 | | #if defined(OS_WIN) |
655 | | // Get the absolute value of QPC time drift. For testing. |
656 | | static int64_t GetQPCDriftMicroseconds(); |
657 | | |
658 | | static TimeTicks FromQPCValue(LONGLONG qpc_value); |
659 | | |
660 | | // Returns true if the high resolution clock is working on this system. |
661 | | // This is only for testing. |
662 | | static bool IsHighResClockWorking(); |
663 | | |
664 | | // Enable high resolution time for TimeTicks::Now(). This function will |
665 | | // test for the availability of a working implementation of |
666 | | // QueryPerformanceCounter(). If one is not available, this function does |
667 | | // nothing and the resolution of Now() remains 1ms. Otherwise, all future |
668 | | // calls to TimeTicks::Now() will have the higher resolution provided by QPC. |
669 | | // Returns true if high resolution time was successfully enabled. |
670 | | static bool SetNowIsHighResNowIfSupported(); |
671 | | |
672 | | // Returns a time value that is NOT rollover protected. |
673 | | static TimeTicks UnprotectedNow(); |
674 | | #endif |
675 | | |
676 | | // Returns true if this object has not been initialized. |
677 | 0 | bool is_null() const { |
678 | 0 | return ticks_ == 0; |
679 | 0 | } |
680 | | |
681 | | // Converts an integer value representing TimeTicks to a class. This is used |
682 | | // when deserializing a |TimeTicks| structure, using a value known to be |
683 | | // compatible. It is not provided as a constructor because the integer type |
684 | | // may be unclear from the perspective of a caller. |
685 | 0 | static TimeTicks FromInternalValue(int64_t ticks) { |
686 | 0 | return TimeTicks(ticks); |
687 | 0 | } |
688 | | |
689 | | // Get the TimeTick value at the time of the UnixEpoch. This is useful when |
690 | | // you need to relate the value of TimeTicks to a real time and date. |
691 | | // Note: Upon first invocation, this function takes a snapshot of the realtime |
692 | | // clock to establish a reference point. This function will return the same |
693 | | // value for the duration of the application, but will be different in future |
694 | | // application runs. |
695 | | static TimeTicks UnixEpoch(); |
696 | | |
697 | | // Returns the internal numeric value of the TimeTicks object. |
698 | | // For serializing, use FromInternalValue to reconstitute. |
699 | 0 | int64_t ToInternalValue() const { |
700 | 0 | return ticks_; |
701 | 0 | } |
702 | | |
703 | 0 | TimeTicks& operator=(TimeTicks other) { |
704 | 0 | ticks_ = other.ticks_; |
705 | 0 | return *this; |
706 | 0 | } |
707 | | |
708 | | // Compute the difference between two times. |
709 | 0 | TimeDelta operator-(TimeTicks other) const { |
710 | 0 | return TimeDelta(ticks_ - other.ticks_); |
711 | 0 | } |
712 | | |
713 | | // Modify by some time delta. |
714 | 0 | TimeTicks& operator+=(TimeDelta delta) { |
715 | 0 | ticks_ += delta.delta_; |
716 | 0 | return *this; |
717 | 0 | } |
718 | 0 | TimeTicks& operator-=(TimeDelta delta) { |
719 | 0 | ticks_ -= delta.delta_; |
720 | 0 | return *this; |
721 | 0 | } |
722 | | |
723 | | // Return a new TimeTicks modified by some delta. |
724 | 0 | TimeTicks operator+(TimeDelta delta) const { |
725 | 0 | return TimeTicks(ticks_ + delta.delta_); |
726 | 0 | } |
727 | 0 | TimeTicks operator-(TimeDelta delta) const { |
728 | 0 | return TimeTicks(ticks_ - delta.delta_); |
729 | 0 | } |
730 | | |
731 | | // Comparison operators |
732 | 0 | bool operator==(TimeTicks other) const { |
733 | 0 | return ticks_ == other.ticks_; |
734 | 0 | } |
735 | 0 | bool operator!=(TimeTicks other) const { |
736 | 0 | return ticks_ != other.ticks_; |
737 | 0 | } |
738 | 0 | bool operator<(TimeTicks other) const { |
739 | 0 | return ticks_ < other.ticks_; |
740 | 0 | } |
741 | 0 | bool operator<=(TimeTicks other) const { |
742 | 0 | return ticks_ <= other.ticks_; |
743 | 0 | } |
744 | 0 | bool operator>(TimeTicks other) const { |
745 | 0 | return ticks_ > other.ticks_; |
746 | 0 | } |
747 | 0 | bool operator>=(TimeTicks other) const { |
748 | 0 | return ticks_ >= other.ticks_; |
749 | 0 | } |
750 | | |
751 | | protected: |
752 | | friend class TimeDelta; |
753 | | |
754 | | // Please use Now() to create a new object. This is for internal use |
755 | | // and testing. Ticks is in microseconds. |
756 | 0 | explicit TimeTicks(int64_t ticks) : ticks_(ticks) { |
757 | 0 | } |
758 | | |
759 | | // Tick count in microseconds. |
760 | | int64_t ticks_; |
761 | | |
762 | | #if defined(OS_WIN) |
763 | | typedef DWORD (*TickFunctionType)(void); |
764 | | static TickFunctionType SetMockTickFunction(TickFunctionType ticker); |
765 | | #endif |
766 | | }; |
767 | | |
768 | 0 | inline TimeTicks TimeDelta::operator+(TimeTicks t) const { |
769 | 0 | return TimeTicks(t.ticks_ + delta_); |
770 | 0 | } |
771 | | |
772 | | } // namespace butil |
773 | | |
774 | | #endif // BUTIL_TIME_TIME_H_ |