Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / base / time / time.cc
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 #include "base/time/time.h"
6
7 #include <ios>
8 #include <limits>
9 #include <ostream>
10 #include <sstream>
11
12 #include "base/float_util.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/third_party/nspr/prtime.h"
17
18 namespace base {
19
20 // TimeDelta ------------------------------------------------------------------
21
22 // static
23 TimeDelta TimeDelta::Max() {
24   return TimeDelta(std::numeric_limits<int64>::max());
25 }
26
27 int TimeDelta::InDays() const {
28   if (is_max()) {
29     // Preserve max to prevent overflow.
30     return std::numeric_limits<int>::max();
31   }
32   return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
33 }
34
35 int TimeDelta::InHours() const {
36   if (is_max()) {
37     // Preserve max to prevent overflow.
38     return std::numeric_limits<int>::max();
39   }
40   return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
41 }
42
43 int TimeDelta::InMinutes() const {
44   if (is_max()) {
45     // Preserve max to prevent overflow.
46     return std::numeric_limits<int>::max();
47   }
48   return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
49 }
50
51 double TimeDelta::InSecondsF() const {
52   if (is_max()) {
53     // Preserve max to prevent overflow.
54     return std::numeric_limits<double>::infinity();
55   }
56   return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
57 }
58
59 int64 TimeDelta::InSeconds() const {
60   if (is_max()) {
61     // Preserve max to prevent overflow.
62     return std::numeric_limits<int64>::max();
63   }
64   return delta_ / Time::kMicrosecondsPerSecond;
65 }
66
67 double TimeDelta::InMillisecondsF() const {
68   if (is_max()) {
69     // Preserve max to prevent overflow.
70     return std::numeric_limits<double>::infinity();
71   }
72   return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
73 }
74
75 int64 TimeDelta::InMilliseconds() const {
76   if (is_max()) {
77     // Preserve max to prevent overflow.
78     return std::numeric_limits<int64>::max();
79   }
80   return delta_ / Time::kMicrosecondsPerMillisecond;
81 }
82
83 int64 TimeDelta::InMillisecondsRoundedUp() const {
84   if (is_max()) {
85     // Preserve max to prevent overflow.
86     return std::numeric_limits<int64>::max();
87   }
88   return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
89       Time::kMicrosecondsPerMillisecond;
90 }
91
92 int64 TimeDelta::InMicroseconds() const {
93   if (is_max()) {
94     // Preserve max to prevent overflow.
95     return std::numeric_limits<int64>::max();
96   }
97   return delta_;
98 }
99
100 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) {
101   return os << time_delta.InSecondsF() << "s";
102 }
103
104 // Time -----------------------------------------------------------------------
105
106 // static
107 Time Time::Max() {
108   return Time(std::numeric_limits<int64>::max());
109 }
110
111 // static
112 Time Time::FromTimeT(time_t tt) {
113   if (tt == 0)
114     return Time();  // Preserve 0 so we can tell it doesn't exist.
115   if (tt == std::numeric_limits<time_t>::max())
116     return Max();
117   return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
118 }
119
120 time_t Time::ToTimeT() const {
121   if (is_null())
122     return 0;  // Preserve 0 so we can tell it doesn't exist.
123   if (is_max()) {
124     // Preserve max without offset to prevent overflow.
125     return std::numeric_limits<time_t>::max();
126   }
127   if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) {
128     DLOG(WARNING) << "Overflow when converting base::Time with internal " <<
129                      "value " << us_ << " to time_t.";
130     return std::numeric_limits<time_t>::max();
131   }
132   return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
133 }
134
135 // static
136 Time Time::FromDoubleT(double dt) {
137   if (dt == 0 || IsNaN(dt))
138     return Time();  // Preserve 0 so we can tell it doesn't exist.
139   if (dt == std::numeric_limits<double>::infinity())
140     return Max();
141   return Time(static_cast<int64>((dt *
142                                   static_cast<double>(kMicrosecondsPerSecond)) +
143                                  kTimeTToMicrosecondsOffset));
144 }
145
146 double Time::ToDoubleT() const {
147   if (is_null())
148     return 0;  // Preserve 0 so we can tell it doesn't exist.
149   if (is_max()) {
150     // Preserve max without offset to prevent overflow.
151     return std::numeric_limits<double>::infinity();
152   }
153   return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
154           static_cast<double>(kMicrosecondsPerSecond));
155 }
156
157 #if defined(OS_POSIX)
158 // static
159 Time Time::FromTimeSpec(const timespec& ts) {
160   return FromDoubleT(ts.tv_sec +
161                      static_cast<double>(ts.tv_nsec) /
162                          base::Time::kNanosecondsPerSecond);
163 }
164 #endif
165
166 // static
167 Time Time::FromJsTime(double ms_since_epoch) {
168   // The epoch is a valid time, so this constructor doesn't interpret
169   // 0 as the null time.
170   if (ms_since_epoch == std::numeric_limits<double>::infinity())
171     return Max();
172   return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
173               kTimeTToMicrosecondsOffset);
174 }
175
176 double Time::ToJsTime() const {
177   if (is_null()) {
178     // Preserve 0 so the invalid result doesn't depend on the platform.
179     return 0;
180   }
181   if (is_max()) {
182     // Preserve max without offset to prevent overflow.
183     return std::numeric_limits<double>::infinity();
184   }
185   return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
186           kMicrosecondsPerMillisecond);
187 }
188
189 int64 Time::ToJavaTime() const {
190   if (is_null()) {
191     // Preserve 0 so the invalid result doesn't depend on the platform.
192     return 0;
193   }
194   if (is_max()) {
195     // Preserve max without offset to prevent overflow.
196     return std::numeric_limits<int64>::max();
197   }
198   return ((us_ - kTimeTToMicrosecondsOffset) /
199           kMicrosecondsPerMillisecond);
200 }
201
202 // static
203 Time Time::UnixEpoch() {
204   Time time;
205   time.us_ = kTimeTToMicrosecondsOffset;
206   return time;
207 }
208
209 Time Time::LocalMidnight() const {
210   Exploded exploded;
211   LocalExplode(&exploded);
212   exploded.hour = 0;
213   exploded.minute = 0;
214   exploded.second = 0;
215   exploded.millisecond = 0;
216   return FromLocalExploded(exploded);
217 }
218
219 // static
220 bool Time::FromStringInternal(const char* time_string,
221                               bool is_local,
222                               Time* parsed_time) {
223   DCHECK((time_string != NULL) && (parsed_time != NULL));
224
225   if (time_string[0] == '\0')
226     return false;
227
228   PRTime result_time = 0;
229   PRStatus result = PR_ParseTimeString(time_string,
230                                        is_local ? PR_FALSE : PR_TRUE,
231                                        &result_time);
232   if (PR_SUCCESS != result)
233     return false;
234
235   result_time += kTimeTToMicrosecondsOffset;
236   *parsed_time = Time(result_time);
237   return true;
238 }
239
240 std::ostream& operator<<(std::ostream& os, Time time) {
241   Time::Exploded exploded;
242   time.UTCExplode(&exploded);
243   // Use StringPrintf because iostreams formatting is painful.
244   return os << StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d UTC",
245                             exploded.year,
246                             exploded.month,
247                             exploded.day_of_month,
248                             exploded.hour,
249                             exploded.minute,
250                             exploded.second,
251                             exploded.millisecond);
252 }
253
254 // Local helper class to hold the conversion from Time to TickTime at the
255 // time of the Unix epoch.
256 class UnixEpochSingleton {
257  public:
258   UnixEpochSingleton()
259       : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {}
260
261   TimeTicks unix_epoch() const { return unix_epoch_; }
262
263  private:
264   const TimeTicks unix_epoch_;
265
266   DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton);
267 };
268
269 static LazyInstance<UnixEpochSingleton>::Leaky
270     leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER;
271
272 // Static
273 TimeTicks TimeTicks::UnixEpoch() {
274   return leaky_unix_epoch_singleton_instance.Get().unix_epoch();
275 }
276
277 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) {
278   // This function formats a TimeTicks object as "bogo-microseconds".
279   // The origin and granularity of the count are platform-specific, and may very
280   // from run to run. Although bogo-microseconds usually roughly correspond to
281   // real microseconds, the only real guarantee is that the number never goes
282   // down during a single run.
283   const TimeDelta as_time_delta = time_ticks - TimeTicks();
284   return os << as_time_delta.InMicroseconds() << " bogo-microseconds";
285 }
286
287 // Time::Exploded -------------------------------------------------------------
288
289 inline bool is_in_range(int value, int lo, int hi) {
290   return lo <= value && value <= hi;
291 }
292
293 bool Time::Exploded::HasValidValues() const {
294   return is_in_range(month, 1, 12) &&
295          is_in_range(day_of_week, 0, 6) &&
296          is_in_range(day_of_month, 1, 31) &&
297          is_in_range(hour, 0, 23) &&
298          is_in_range(minute, 0, 59) &&
299          is_in_range(second, 0, 60) &&
300          is_in_range(millisecond, 0, 999);
301 }
302
303 }  // namespace base