1 // @REWRITE(insert c-copyright)
2 // @REWRITE(delete-start)
3 // Copyright 2007 Google Inc. All Rights Reserved.
4 // @REWRITE(delete-end)
7 #include "native_client/include/portability.h"
8 #include "native_client/include/base/basictypes.h"
10 #include "native_client/service_runtime/time.h"
15 // Time between resampling the un-granular clock for this API. 60 seconds.
16 const int kMaxMillisecondsToAvoidDrift
= 60 * Time::kMillisecondsPerSecond
;
20 // TimeDelta ------------------------------------------------------------------
23 TimeDelta
TimeDelta::FromDays(int64 days
) {
24 return TimeDelta(days
* Time::kMicrosecondsPerDay
);
28 TimeDelta
TimeDelta::FromHours(int64 hours
) {
29 return TimeDelta(hours
* Time::kMicrosecondsPerHour
);
33 TimeDelta
TimeDelta::FromMinutes(int64 minutes
) {
34 return TimeDelta(minutes
* Time::kMicrosecondsPerMinute
);
38 TimeDelta
TimeDelta::FromSeconds(int64 secs
) {
39 return TimeDelta(secs
* Time::kMicrosecondsPerSecond
);
43 TimeDelta
TimeDelta::FromMilliseconds(int64 ms
) {
44 return TimeDelta(ms
* Time::kMicrosecondsPerMillisecond
);
48 TimeDelta
TimeDelta::FromMicroseconds(int64 us
) {
52 int TimeDelta::InDays() const {
53 return static_cast<int>(delta_
/ Time::kMicrosecondsPerDay
);
56 double TimeDelta::InSecondsF() const {
57 return static_cast<double>(delta_
) / Time::kMicrosecondsPerSecond
;
60 int64
TimeDelta::InSeconds() const {
61 return delta_
/ Time::kMicrosecondsPerSecond
;
64 double TimeDelta::InMillisecondsF() const {
65 return static_cast<double>(delta_
) / Time::kMicrosecondsPerMillisecond
;
68 int64
TimeDelta::InMilliseconds() const {
69 return delta_
/ Time::kMicrosecondsPerMillisecond
;
72 int64
TimeDelta::InMicroseconds() const {
76 // Time -----------------------------------------------------------------------
78 int64
Time::initial_time_
= 0;
79 TimeTicks
Time::initial_ticks_
;
82 void Time::InitializeClock() {
83 initial_ticks_
= TimeTicks::Now();
84 initial_time_
= CurrentWallclockMicroseconds();
89 if (initial_time_
== 0)
92 // We implement time using the high-resolution timers so that we can get
93 // timeouts which are smaller than 10-15ms. If we just used
94 // CurrentWallclockMicroseconds(), we'd have the less-granular timer.
96 // To make this work, we initialize the clock (initial_time) and the
97 // counter (initial_ctr). To compute the initial time, we can check
98 // the number of ticks that have elapsed, and compute the delta.
100 // To avoid any drift, we periodically resync the counters to the system
103 TimeTicks ticks
= TimeTicks::Now();
105 // Calculate the time elapsed since we started our timer
106 TimeDelta elapsed
= ticks
- initial_ticks_
;
108 // Check if enough time has elapsed that we need to resync the clock.
109 if (elapsed
.InMilliseconds() > kMaxMillisecondsToAvoidDrift
) {
114 return elapsed
+ initial_time_
;
119 Time
Time::FromTimeT(time_t tt
) {
121 return Time(); // Preserve 0 so we can tell it doesn't exist.
122 return (tt
* kMicrosecondsPerSecond
) + kTimeTToMicrosecondsOffset
;
125 time_t Time::ToTimeT() const {
127 return 0; // Preserve 0 so we can tell it doesn't exist.
128 return (us_
- kTimeTToMicrosecondsOffset
) / kMicrosecondsPerSecond
;
131 double Time::ToDoubleT() const {
133 return 0; // Preserve 0 so we can tell it doesn't exist.
134 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
135 static_cast<double>(kMicrosecondsPerSecond
));
138 Time
Time::LocalMidnight() const {
140 LocalExplode(&exploded
);
144 exploded
.millisecond
= 0;
145 return FromLocalExploded(exploded
);