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
=
17 60 * NaCl::Time::kMillisecondsPerSecond
;
21 // TimeDelta ------------------------------------------------------------------
24 NaCl::TimeDelta
NaCl::TimeDelta::FromDays(int64 days
) {
25 return TimeDelta(days
* Time::kMicrosecondsPerDay
);
29 NaCl::TimeDelta
NaCl::TimeDelta::FromHours(int64 hours
) {
30 return TimeDelta(hours
* Time::kMicrosecondsPerHour
);
34 NaCl::TimeDelta
NaCl::TimeDelta::FromMinutes(int64 minutes
) {
35 return TimeDelta(minutes
* Time::kMicrosecondsPerMinute
);
39 NaCl::TimeDelta
NaCl::TimeDelta::FromSeconds(int64 secs
) {
40 return TimeDelta(secs
* Time::kMicrosecondsPerSecond
);
44 NaCl::TimeDelta
NaCl::TimeDelta::FromMilliseconds(int64 ms
) {
45 return TimeDelta(ms
* Time::kMicrosecondsPerMillisecond
);
49 NaCl::TimeDelta
NaCl::TimeDelta::FromMicroseconds(int64 us
) {
53 int NaCl::TimeDelta::InDays() const {
54 return static_cast<int>(delta_
/ Time::kMicrosecondsPerDay
);
57 double NaCl::TimeDelta::InSecondsF() const {
58 return static_cast<double>(delta_
) / Time::kMicrosecondsPerSecond
;
61 int64
NaCl::TimeDelta::InSeconds() const {
62 return delta_
/ Time::kMicrosecondsPerSecond
;
65 double NaCl::TimeDelta::InMillisecondsF() const {
66 return static_cast<double>(delta_
) / Time::kMicrosecondsPerMillisecond
;
69 int64
NaCl::TimeDelta::InMilliseconds() const {
70 return delta_
/ Time::kMicrosecondsPerMillisecond
;
73 int64
NaCl::TimeDelta::InMicroseconds() const {
77 // Time -----------------------------------------------------------------------
79 int64
NaCl::Time::initial_time_
= 0;
80 NaCl::TimeTicks
NaCl::Time::initial_ticks_
;
83 void NaCl::Time::InitializeClock() {
84 initial_ticks_
= TimeTicks::Now();
85 initial_time_
= CurrentWallclockMicroseconds();
89 NaCl::Time
NaCl::Time::Now() {
90 if (initial_time_
== 0)
93 // We implement time using the high-resolution timers so that we can get
94 // timeouts which are smaller than 10-15ms. If we just used
95 // CurrentWallclockMicroseconds(), we'd have the less-granular timer.
97 // To make this work, we initialize the clock (initial_time) and the
98 // counter (initial_ctr). To compute the initial time, we can check
99 // the number of ticks that have elapsed, and compute the delta.
101 // To avoid any drift, we periodically resync the counters to the system
104 TimeTicks ticks
= TimeTicks::Now();
106 // Calculate the time elapsed since we started our timer
107 TimeDelta elapsed
= ticks
- initial_ticks_
;
109 // Check if enough time has elapsed that we need to resync the clock.
110 if (elapsed
.InMilliseconds() > kMaxMillisecondsToAvoidDrift
) {
115 return elapsed
+ initial_time_
;
120 NaCl::Time
NaCl::Time::FromTimeT(time_t tt
) {
122 return Time(); // Preserve 0 so we can tell it doesn't exist.
123 return (tt
* kMicrosecondsPerSecond
) + kTimeTToMicrosecondsOffset
;
126 time_t NaCl::Time::ToTimeT() const {
128 return 0; // Preserve 0 so we can tell it doesn't exist.
129 return (us_
- kTimeTToMicrosecondsOffset
) / kMicrosecondsPerSecond
;
132 double NaCl::Time::ToDoubleT() const {
134 return 0; // Preserve 0 so we can tell it doesn't exist.
135 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
136 static_cast<double>(kMicrosecondsPerSecond
));
139 NaCl::Time
NaCl::Time::LocalMidnight() const {
141 LocalExplode(&exploded
);
145 exploded
.millisecond
= 0;
146 return FromLocalExploded(exploded
);