Add -E option for setting environment variables
[nativeclient.git] / service_runtime / time.cc
blob9af87afd805640033a4d877b3908d7af9ee6b67d
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"
13 namespace {
15 // Time between resampling the un-granular clock for this API. 60 seconds.
16 const int kMaxMillisecondsToAvoidDrift = 60 * Time::kMillisecondsPerSecond;
18 } // namespace
20 // TimeDelta ------------------------------------------------------------------
22 // static
23 TimeDelta TimeDelta::FromDays(int64 days) {
24 return TimeDelta(days * Time::kMicrosecondsPerDay);
27 // static
28 TimeDelta TimeDelta::FromHours(int64 hours) {
29 return TimeDelta(hours * Time::kMicrosecondsPerHour);
32 // static
33 TimeDelta TimeDelta::FromMinutes(int64 minutes) {
34 return TimeDelta(minutes * Time::kMicrosecondsPerMinute);
37 // static
38 TimeDelta TimeDelta::FromSeconds(int64 secs) {
39 return TimeDelta(secs * Time::kMicrosecondsPerSecond);
42 // static
43 TimeDelta TimeDelta::FromMilliseconds(int64 ms) {
44 return TimeDelta(ms * Time::kMicrosecondsPerMillisecond);
47 // static
48 TimeDelta TimeDelta::FromMicroseconds(int64 us) {
49 return TimeDelta(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 {
73 return delta_;
76 // Time -----------------------------------------------------------------------
78 int64 Time::initial_time_ = 0;
79 TimeTicks Time::initial_ticks_;
81 // static
82 void Time::InitializeClock() {
83 initial_ticks_ = TimeTicks::Now();
84 initial_time_ = CurrentWallclockMicroseconds();
87 // static
88 Time Time::Now() {
89 if (initial_time_ == 0)
90 InitializeClock();
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
101 // clock.
102 while (true) {
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) {
110 InitializeClock();
111 continue;
114 return elapsed + initial_time_;
118 // static
119 Time Time::FromTimeT(time_t tt) {
120 if (tt == 0)
121 return Time(); // Preserve 0 so we can tell it doesn't exist.
122 return (tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset;
125 time_t Time::ToTimeT() const {
126 if (us_ == 0)
127 return 0; // Preserve 0 so we can tell it doesn't exist.
128 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
131 double Time::ToDoubleT() const {
132 if (us_ == 0)
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 {
139 Exploded exploded;
140 LocalExplode(&exploded);
141 exploded.hour = 0;
142 exploded.minute = 0;
143 exploded.second = 0;
144 exploded.millisecond = 0;
145 return FromLocalExploded(exploded);