rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / base / time / time.cc
blob4605d56ee007dba02029e17d9601618d528e23da
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/time.h"
7 #include <limits>
8 #include <ostream>
10 #include "base/float_util.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/third_party/nspr/prtime.h"
14 #include "base/third_party/nspr/prtypes.h"
16 namespace base {
18 // TimeDelta ------------------------------------------------------------------
20 int TimeDelta::InDays() const {
21 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
24 int TimeDelta::InHours() const {
25 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
28 int TimeDelta::InMinutes() const {
29 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
32 double TimeDelta::InSecondsF() const {
33 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
36 int64 TimeDelta::InSeconds() const {
37 return delta_ / Time::kMicrosecondsPerSecond;
40 double TimeDelta::InMillisecondsF() const {
41 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
44 int64 TimeDelta::InMilliseconds() const {
45 return delta_ / Time::kMicrosecondsPerMillisecond;
48 int64 TimeDelta::InMillisecondsRoundedUp() const {
49 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
50 Time::kMicrosecondsPerMillisecond;
53 int64 TimeDelta::InMicroseconds() const {
54 return delta_;
57 // Time -----------------------------------------------------------------------
59 // static
60 Time Time::Max() {
61 return Time(std::numeric_limits<int64>::max());
64 // static
65 Time Time::FromTimeT(time_t tt) {
66 if (tt == 0)
67 return Time(); // Preserve 0 so we can tell it doesn't exist.
68 if (tt == std::numeric_limits<time_t>::max())
69 return Max();
70 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
73 time_t Time::ToTimeT() const {
74 if (is_null())
75 return 0; // Preserve 0 so we can tell it doesn't exist.
76 if (is_max()) {
77 // Preserve max without offset to prevent overflow.
78 return std::numeric_limits<time_t>::max();
80 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) {
81 DLOG(WARNING) << "Overflow when converting base::Time with internal " <<
82 "value " << us_ << " to time_t.";
83 return std::numeric_limits<time_t>::max();
85 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
88 // static
89 Time Time::FromDoubleT(double dt) {
90 if (dt == 0 || IsNaN(dt))
91 return Time(); // Preserve 0 so we can tell it doesn't exist.
92 if (dt == std::numeric_limits<double>::infinity())
93 return Max();
94 return Time(static_cast<int64>((dt *
95 static_cast<double>(kMicrosecondsPerSecond)) +
96 kTimeTToMicrosecondsOffset));
99 double Time::ToDoubleT() const {
100 if (is_null())
101 return 0; // Preserve 0 so we can tell it doesn't exist.
102 if (is_max()) {
103 // Preserve max without offset to prevent overflow.
104 return std::numeric_limits<double>::infinity();
106 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
107 static_cast<double>(kMicrosecondsPerSecond));
110 #if defined(OS_POSIX)
111 // static
112 Time Time::FromTimeSpec(const timespec& ts) {
113 return FromDoubleT(ts.tv_sec +
114 static_cast<double>(ts.tv_nsec) /
115 base::Time::kNanosecondsPerSecond);
117 #endif
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>::infinity())
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>::infinity();
138 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
139 kMicrosecondsPerMillisecond);
142 int64 Time::ToJavaTime() const {
143 if (is_null()) {
144 // Preserve 0 so the invalid result doesn't depend on the platform.
145 return 0;
147 if (is_max()) {
148 // Preserve max without offset to prevent overflow.
149 return std::numeric_limits<int64>::max();
151 return ((us_ - kTimeTToMicrosecondsOffset) /
152 kMicrosecondsPerMillisecond);
155 // static
156 Time Time::UnixEpoch() {
157 Time time;
158 time.us_ = kTimeTToMicrosecondsOffset;
159 return time;
162 Time Time::LocalMidnight() const {
163 Exploded exploded;
164 LocalExplode(&exploded);
165 exploded.hour = 0;
166 exploded.minute = 0;
167 exploded.second = 0;
168 exploded.millisecond = 0;
169 return FromLocalExploded(exploded);
172 // static
173 bool Time::FromStringInternal(const char* time_string,
174 bool is_local,
175 Time* parsed_time) {
176 DCHECK((time_string != NULL) && (parsed_time != NULL));
178 if (time_string[0] == '\0')
179 return false;
181 PRTime result_time = 0;
182 PRStatus result = PR_ParseTimeString(time_string,
183 is_local ? PR_FALSE : PR_TRUE,
184 &result_time);
185 if (PR_SUCCESS != result)
186 return false;
188 result_time += kTimeTToMicrosecondsOffset;
189 *parsed_time = Time(result_time);
190 return true;
193 // Local helper class to hold the conversion from Time to TickTime at the
194 // time of the Unix epoch.
195 class UnixEpochSingleton {
196 public:
197 UnixEpochSingleton()
198 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {}
200 TimeTicks unix_epoch() const { return unix_epoch_; }
202 private:
203 const TimeTicks unix_epoch_;
205 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton);
208 static LazyInstance<UnixEpochSingleton>::Leaky
209 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER;
211 // Static
212 TimeTicks TimeTicks::UnixEpoch() {
213 return leaky_unix_epoch_singleton_instance.Get().unix_epoch();
216 // Time::Exploded -------------------------------------------------------------
218 inline bool is_in_range(int value, int lo, int hi) {
219 return lo <= value && value <= hi;
222 bool Time::Exploded::HasValidValues() const {
223 return is_in_range(month, 1, 12) &&
224 is_in_range(day_of_week, 0, 6) &&
225 is_in_range(day_of_month, 1, 31) &&
226 is_in_range(hour, 0, 23) &&
227 is_in_range(minute, 0, 59) &&
228 is_in_range(second, 0, 60) &&
229 is_in_range(millisecond, 0, 999);
232 } // namespace base