Properly attach InfoBarContainer when it is swapped to a new WebContents
[chromium-blink-merge.git] / base / time / time.cc
blob321323b7cdbdb62640f7b23ad6e72073cd4eb948
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 <ios>
8 #include <limits>
9 #include <ostream>
10 #include <sstream>
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"
18 namespace base {
20 // TimeDelta ------------------------------------------------------------------
22 // static
23 TimeDelta TimeDelta::Max() {
24 return TimeDelta(std::numeric_limits<int64>::max());
27 int TimeDelta::InDays() const {
28 if (is_max()) {
29 // Preserve max to prevent overflow.
30 return std::numeric_limits<int>::max();
32 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
35 int TimeDelta::InHours() const {
36 if (is_max()) {
37 // Preserve max to prevent overflow.
38 return std::numeric_limits<int>::max();
40 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
43 int TimeDelta::InMinutes() const {
44 if (is_max()) {
45 // Preserve max to prevent overflow.
46 return std::numeric_limits<int>::max();
48 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
51 double TimeDelta::InSecondsF() const {
52 if (is_max()) {
53 // Preserve max to prevent overflow.
54 return std::numeric_limits<double>::infinity();
56 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
59 int64 TimeDelta::InSeconds() const {
60 if (is_max()) {
61 // Preserve max to prevent overflow.
62 return std::numeric_limits<int64>::max();
64 return delta_ / Time::kMicrosecondsPerSecond;
67 double TimeDelta::InMillisecondsF() const {
68 if (is_max()) {
69 // Preserve max to prevent overflow.
70 return std::numeric_limits<double>::infinity();
72 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
75 int64 TimeDelta::InMilliseconds() const {
76 if (is_max()) {
77 // Preserve max to prevent overflow.
78 return std::numeric_limits<int64>::max();
80 return delta_ / Time::kMicrosecondsPerMillisecond;
83 int64 TimeDelta::InMillisecondsRoundedUp() const {
84 if (is_max()) {
85 // Preserve max to prevent overflow.
86 return std::numeric_limits<int64>::max();
88 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
89 Time::kMicrosecondsPerMillisecond;
92 int64 TimeDelta::InMicroseconds() const {
93 if (is_max()) {
94 // Preserve max to prevent overflow.
95 return std::numeric_limits<int64>::max();
97 return delta_;
100 int64 TimeDelta::SaturatedAdd(int64 value) const {
101 CheckedNumeric<int64> rv(delta_);
102 rv += value;
103 return FromCheckedNumeric(rv);
106 int64 TimeDelta::SaturatedSub(int64 value) const {
107 CheckedNumeric<int64> rv(delta_);
108 rv -= value;
109 return FromCheckedNumeric(rv);
112 // static
113 int64 TimeDelta::FromCheckedNumeric(const CheckedNumeric<int64> value) {
114 if (value.IsValid())
115 return value.ValueUnsafe();
117 // We could return max/min but we don't really expose what the maximum delta
118 // is. Instead, return max/(-max), which is something that clients can reason
119 // about.
120 // TODO(rvargas) crbug.com/332611: don't use internal values.
121 int64 limit = std::numeric_limits<int64>::max();
122 if (value.validity() == internal::RANGE_UNDERFLOW)
123 limit = -limit;
124 return value.ValueOrDefault(limit);
127 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) {
128 return os << time_delta.InSecondsF() << "s";
131 // Time -----------------------------------------------------------------------
133 // static
134 Time Time::Max() {
135 return Time(std::numeric_limits<int64>::max());
138 // static
139 Time Time::FromTimeT(time_t tt) {
140 if (tt == 0)
141 return Time(); // Preserve 0 so we can tell it doesn't exist.
142 if (tt == std::numeric_limits<time_t>::max())
143 return Max();
144 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
147 time_t Time::ToTimeT() const {
148 if (is_null())
149 return 0; // Preserve 0 so we can tell it doesn't exist.
150 if (is_max()) {
151 // Preserve max without offset to prevent overflow.
152 return std::numeric_limits<time_t>::max();
154 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) {
155 DLOG(WARNING) << "Overflow when converting base::Time with internal " <<
156 "value " << us_ << " to time_t.";
157 return std::numeric_limits<time_t>::max();
159 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
162 // static
163 Time Time::FromDoubleT(double dt) {
164 if (dt == 0 || IsNaN(dt))
165 return Time(); // Preserve 0 so we can tell it doesn't exist.
166 if (dt == std::numeric_limits<double>::infinity())
167 return Max();
168 return Time(static_cast<int64>((dt *
169 static_cast<double>(kMicrosecondsPerSecond)) +
170 kTimeTToMicrosecondsOffset));
173 double Time::ToDoubleT() const {
174 if (is_null())
175 return 0; // Preserve 0 so we can tell it doesn't exist.
176 if (is_max()) {
177 // Preserve max without offset to prevent overflow.
178 return std::numeric_limits<double>::infinity();
180 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
181 static_cast<double>(kMicrosecondsPerSecond));
184 #if defined(OS_POSIX)
185 // static
186 Time Time::FromTimeSpec(const timespec& ts) {
187 return FromDoubleT(ts.tv_sec +
188 static_cast<double>(ts.tv_nsec) /
189 base::Time::kNanosecondsPerSecond);
191 #endif
193 // static
194 Time Time::FromJsTime(double ms_since_epoch) {
195 // The epoch is a valid time, so this constructor doesn't interpret
196 // 0 as the null time.
197 if (ms_since_epoch == std::numeric_limits<double>::infinity())
198 return Max();
199 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
200 kTimeTToMicrosecondsOffset);
203 double Time::ToJsTime() const {
204 if (is_null()) {
205 // Preserve 0 so the invalid result doesn't depend on the platform.
206 return 0;
208 if (is_max()) {
209 // Preserve max without offset to prevent overflow.
210 return std::numeric_limits<double>::infinity();
212 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
213 kMicrosecondsPerMillisecond);
216 int64 Time::ToJavaTime() const {
217 if (is_null()) {
218 // Preserve 0 so the invalid result doesn't depend on the platform.
219 return 0;
221 if (is_max()) {
222 // Preserve max without offset to prevent overflow.
223 return std::numeric_limits<int64>::max();
225 return ((us_ - kTimeTToMicrosecondsOffset) /
226 kMicrosecondsPerMillisecond);
229 // static
230 Time Time::UnixEpoch() {
231 Time time;
232 time.us_ = kTimeTToMicrosecondsOffset;
233 return time;
236 Time Time::LocalMidnight() const {
237 Exploded exploded;
238 LocalExplode(&exploded);
239 exploded.hour = 0;
240 exploded.minute = 0;
241 exploded.second = 0;
242 exploded.millisecond = 0;
243 return FromLocalExploded(exploded);
246 // static
247 bool Time::FromStringInternal(const char* time_string,
248 bool is_local,
249 Time* parsed_time) {
250 DCHECK((time_string != NULL) && (parsed_time != NULL));
252 if (time_string[0] == '\0')
253 return false;
255 PRTime result_time = 0;
256 PRStatus result = PR_ParseTimeString(time_string,
257 is_local ? PR_FALSE : PR_TRUE,
258 &result_time);
259 if (PR_SUCCESS != result)
260 return false;
262 result_time += kTimeTToMicrosecondsOffset;
263 *parsed_time = Time(result_time);
264 return true;
267 std::ostream& operator<<(std::ostream& os, Time time) {
268 Time::Exploded exploded;
269 time.UTCExplode(&exploded);
270 // Use StringPrintf because iostreams formatting is painful.
271 return os << StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d UTC",
272 exploded.year,
273 exploded.month,
274 exploded.day_of_month,
275 exploded.hour,
276 exploded.minute,
277 exploded.second,
278 exploded.millisecond);
281 // Local helper class to hold the conversion from Time to TickTime at the
282 // time of the Unix epoch.
283 class UnixEpochSingleton {
284 public:
285 UnixEpochSingleton()
286 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {}
288 TimeTicks unix_epoch() const { return unix_epoch_; }
290 private:
291 const TimeTicks unix_epoch_;
293 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton);
296 static LazyInstance<UnixEpochSingleton>::Leaky
297 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER;
299 // Static
300 TimeTicks TimeTicks::UnixEpoch() {
301 return leaky_unix_epoch_singleton_instance.Get().unix_epoch();
304 TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase,
305 TimeDelta tick_interval) const {
306 // |interval_offset| is the offset from |this| to the next multiple of
307 // |tick_interval| after |tick_phase|, possibly negative if in the past.
308 TimeDelta interval_offset = TimeDelta::FromInternalValue(
309 (tick_phase - *this).ToInternalValue() % tick_interval.ToInternalValue());
310 // If |this| is exactly on the interval (i.e. offset==0), don't adjust.
311 // Otherwise, if |tick_phase| was in the past, adjust forward to the next
312 // tick after |this|.
313 if (interval_offset.ToInternalValue() != 0 && tick_phase < *this) {
314 interval_offset += tick_interval;
317 return *this + interval_offset;
320 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) {
321 // This function formats a TimeTicks object as "bogo-microseconds".
322 // The origin and granularity of the count are platform-specific, and may very
323 // from run to run. Although bogo-microseconds usually roughly correspond to
324 // real microseconds, the only real guarantee is that the number never goes
325 // down during a single run.
326 const TimeDelta as_time_delta = time_ticks - TimeTicks();
327 return os << as_time_delta.InMicroseconds() << " bogo-microseconds";
330 // Time::Exploded -------------------------------------------------------------
332 inline bool is_in_range(int value, int lo, int hi) {
333 return lo <= value && value <= hi;
336 bool Time::Exploded::HasValidValues() const {
337 return is_in_range(month, 1, 12) &&
338 is_in_range(day_of_week, 0, 6) &&
339 is_in_range(day_of_month, 1, 31) &&
340 is_in_range(hour, 0, 23) &&
341 is_in_range(minute, 0, 59) &&
342 is_in_range(second, 0, 60) &&
343 is_in_range(millisecond, 0, 999);
346 } // namespace base