Fix Win8 metro startup crash from window switcher button
[chromium-blink-merge.git] / base / time.cc
blob26fd500403e961b0497cfed93b21aee97e6e9174
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.
5 #include "base/time.h"
7 #include <math.h>
8 #if defined(OS_WIN)
9 #include <float.h>
10 #endif
12 #include <limits>
14 #include "base/strings/sys_string_conversions.h"
15 #include "base/third_party/nspr/prtime.h"
17 #include "base/logging.h"
19 namespace base {
21 namespace {
22 #if defined(OS_WIN)
23 inline bool isnan(double num) { return !!_isnan(num); }
24 #endif
27 // TimeDelta ------------------------------------------------------------------
29 int TimeDelta::InDays() const {
30 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
33 int TimeDelta::InHours() const {
34 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
37 int TimeDelta::InMinutes() const {
38 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
41 double TimeDelta::InSecondsF() const {
42 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
45 int64 TimeDelta::InSeconds() const {
46 return delta_ / Time::kMicrosecondsPerSecond;
49 double TimeDelta::InMillisecondsF() const {
50 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
53 int64 TimeDelta::InMilliseconds() const {
54 return delta_ / Time::kMicrosecondsPerMillisecond;
57 int64 TimeDelta::InMillisecondsRoundedUp() const {
58 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
59 Time::kMicrosecondsPerMillisecond;
62 int64 TimeDelta::InMicroseconds() const {
63 return delta_;
66 // Time -----------------------------------------------------------------------
68 // static
69 Time Time::Max() {
70 return Time(std::numeric_limits<int64>::max());
73 // static
74 Time Time::FromTimeT(time_t tt) {
75 if (tt == 0)
76 return Time(); // Preserve 0 so we can tell it doesn't exist.
77 if (tt == std::numeric_limits<time_t>::max())
78 return Max();
79 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
82 time_t Time::ToTimeT() const {
83 if (is_null())
84 return 0; // Preserve 0 so we can tell it doesn't exist.
85 if (is_max()) {
86 // Preserve max without offset to prevent overflow.
87 return std::numeric_limits<time_t>::max();
89 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) {
90 DLOG(WARNING) << "Overflow when converting base::Time with internal " <<
91 "value " << us_ << " to time_t.";
92 return std::numeric_limits<time_t>::max();
94 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
97 // static
98 Time Time::FromDoubleT(double dt) {
99 if (dt == 0 || isnan(dt))
100 return Time(); // Preserve 0 so we can tell it doesn't exist.
101 if (dt == std::numeric_limits<double>::max())
102 return Max();
103 return Time(static_cast<int64>((dt *
104 static_cast<double>(kMicrosecondsPerSecond)) +
105 kTimeTToMicrosecondsOffset));
108 double Time::ToDoubleT() const {
109 if (is_null())
110 return 0; // Preserve 0 so we can tell it doesn't exist.
111 if (is_max()) {
112 // Preserve max without offset to prevent overflow.
113 return std::numeric_limits<double>::max();
115 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
116 static_cast<double>(kMicrosecondsPerSecond));
119 // static
120 Time Time::FromJsTime(double ms_since_epoch) {
121 // The epoch is a valid time, so this constructor doesn't interpret
122 // 0 as the null time.
123 if (ms_since_epoch == std::numeric_limits<double>::max())
124 return Max();
125 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
126 kTimeTToMicrosecondsOffset);
129 double Time::ToJsTime() const {
130 if (is_null()) {
131 // Preserve 0 so the invalid result doesn't depend on the platform.
132 return 0;
134 if (is_max()) {
135 // Preserve max without offset to prevent overflow.
136 return std::numeric_limits<double>::max();
138 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
139 kMicrosecondsPerMillisecond);
142 // static
143 Time Time::UnixEpoch() {
144 Time time;
145 time.us_ = kTimeTToMicrosecondsOffset;
146 return time;
149 Time Time::LocalMidnight() const {
150 Exploded exploded;
151 LocalExplode(&exploded);
152 exploded.hour = 0;
153 exploded.minute = 0;
154 exploded.second = 0;
155 exploded.millisecond = 0;
156 return FromLocalExploded(exploded);
159 // static
160 bool Time::FromStringInternal(const char* time_string,
161 bool is_local,
162 Time* parsed_time) {
163 DCHECK((time_string != NULL) && (parsed_time != NULL));
165 if (time_string[0] == '\0')
166 return false;
168 PRTime result_time = 0;
169 PRStatus result = PR_ParseTimeString(time_string,
170 is_local ? PR_FALSE : PR_TRUE,
171 &result_time);
172 if (PR_SUCCESS != result)
173 return false;
175 result_time += kTimeTToMicrosecondsOffset;
176 *parsed_time = Time(result_time);
177 return true;
180 // Time::Exploded -------------------------------------------------------------
182 inline bool is_in_range(int value, int lo, int hi) {
183 return lo <= value && value <= hi;
186 bool Time::Exploded::HasValidValues() const {
187 return is_in_range(month, 1, 12) &&
188 is_in_range(day_of_week, 0, 6) &&
189 is_in_range(day_of_month, 1, 31) &&
190 is_in_range(hour, 0, 23) &&
191 is_in_range(minute, 0, 59) &&
192 is_in_range(second, 0, 60) &&
193 is_in_range(millisecond, 0, 999);
196 } // namespace base