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"
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"
20 // TimeDelta ------------------------------------------------------------------
23 TimeDelta
TimeDelta::Max() {
24 return TimeDelta(std::numeric_limits
<int64
>::max());
27 int TimeDelta::InDays() const {
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 {
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 {
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 {
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 {
61 // Preserve max to prevent overflow.
62 return std::numeric_limits
<int64
>::max();
64 return delta_
/ Time::kMicrosecondsPerSecond
;
67 double TimeDelta::InMillisecondsF() const {
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 {
77 // Preserve max to prevent overflow.
78 return std::numeric_limits
<int64
>::max();
80 return delta_
/ Time::kMicrosecondsPerMillisecond
;
83 int64
TimeDelta::InMillisecondsRoundedUp() const {
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 {
94 // Preserve max to prevent overflow.
95 return std::numeric_limits
<int64
>::max();
100 namespace time_internal
{
102 int64
SaturatedAdd(TimeDelta delta
, int64 value
) {
103 CheckedNumeric
<int64
> rv(delta
.delta_
);
105 return FromCheckedNumeric(rv
);
108 int64
SaturatedSub(TimeDelta delta
, int64 value
) {
109 CheckedNumeric
<int64
> rv(delta
.delta_
);
111 return FromCheckedNumeric(rv
);
114 int64
FromCheckedNumeric(const CheckedNumeric
<int64
> value
) {
116 return value
.ValueUnsafe();
118 // We could return max/min but we don't really expose what the maximum delta
119 // is. Instead, return max/(-max), which is something that clients can reason
121 // TODO(rvargas) crbug.com/332611: don't use internal values.
122 int64 limit
= std::numeric_limits
<int64
>::max();
123 if (value
.validity() == internal::RANGE_UNDERFLOW
)
125 return value
.ValueOrDefault(limit
);
128 } // namespace time_internal
130 std::ostream
& operator<<(std::ostream
& os
, TimeDelta time_delta
) {
131 return os
<< time_delta
.InSecondsF() << "s";
134 // Time -----------------------------------------------------------------------
138 return Time(std::numeric_limits
<int64
>::max());
142 Time
Time::FromTimeT(time_t tt
) {
144 return Time(); // Preserve 0 so we can tell it doesn't exist.
145 if (tt
== std::numeric_limits
<time_t>::max())
147 return Time(kTimeTToMicrosecondsOffset
) + TimeDelta::FromSeconds(tt
);
150 time_t Time::ToTimeT() const {
152 return 0; // Preserve 0 so we can tell it doesn't exist.
154 // Preserve max without offset to prevent overflow.
155 return std::numeric_limits
<time_t>::max();
157 if (std::numeric_limits
<int64
>::max() - kTimeTToMicrosecondsOffset
<= us_
) {
158 DLOG(WARNING
) << "Overflow when converting base::Time with internal " <<
159 "value " << us_
<< " to time_t.";
160 return std::numeric_limits
<time_t>::max();
162 return (us_
- kTimeTToMicrosecondsOffset
) / kMicrosecondsPerSecond
;
166 Time
Time::FromDoubleT(double dt
) {
167 if (dt
== 0 || std::isnan(dt
))
168 return Time(); // Preserve 0 so we can tell it doesn't exist.
169 return Time(kTimeTToMicrosecondsOffset
) + TimeDelta::FromSecondsD(dt
);
172 double Time::ToDoubleT() const {
174 return 0; // Preserve 0 so we can tell it doesn't exist.
176 // Preserve max without offset to prevent overflow.
177 return std::numeric_limits
<double>::infinity();
179 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
180 static_cast<double>(kMicrosecondsPerSecond
));
183 #if defined(OS_POSIX)
185 Time
Time::FromTimeSpec(const timespec
& ts
) {
186 return FromDoubleT(ts
.tv_sec
+
187 static_cast<double>(ts
.tv_nsec
) /
188 base::Time::kNanosecondsPerSecond
);
193 Time
Time::FromJsTime(double ms_since_epoch
) {
194 // The epoch is a valid time, so this constructor doesn't interpret
195 // 0 as the null time.
196 return Time(kTimeTToMicrosecondsOffset
) +
197 TimeDelta::FromMillisecondsD(ms_since_epoch
);
200 double Time::ToJsTime() const {
202 // Preserve 0 so the invalid result doesn't depend on the platform.
206 // Preserve max without offset to prevent overflow.
207 return std::numeric_limits
<double>::infinity();
209 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
210 kMicrosecondsPerMillisecond
);
213 int64
Time::ToJavaTime() const {
215 // Preserve 0 so the invalid result doesn't depend on the platform.
219 // Preserve max without offset to prevent overflow.
220 return std::numeric_limits
<int64
>::max();
222 return ((us_
- kTimeTToMicrosecondsOffset
) /
223 kMicrosecondsPerMillisecond
);
227 Time
Time::UnixEpoch() {
229 time
.us_
= kTimeTToMicrosecondsOffset
;
233 Time
Time::LocalMidnight() const {
235 LocalExplode(&exploded
);
239 exploded
.millisecond
= 0;
240 return FromLocalExploded(exploded
);
244 bool Time::FromStringInternal(const char* time_string
,
247 DCHECK((time_string
!= NULL
) && (parsed_time
!= NULL
));
249 if (time_string
[0] == '\0')
252 PRTime result_time
= 0;
253 PRStatus result
= PR_ParseTimeString(time_string
,
254 is_local
? PR_FALSE
: PR_TRUE
,
256 if (PR_SUCCESS
!= result
)
259 result_time
+= kTimeTToMicrosecondsOffset
;
260 *parsed_time
= Time(result_time
);
264 std::ostream
& operator<<(std::ostream
& os
, Time time
) {
265 Time::Exploded exploded
;
266 time
.UTCExplode(&exploded
);
267 // Use StringPrintf because iostreams formatting is painful.
268 return os
<< StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d UTC",
271 exploded
.day_of_month
,
275 exploded
.millisecond
);
278 // Local helper class to hold the conversion from Time to TickTime at the
279 // time of the Unix epoch.
280 class UnixEpochSingleton
{
283 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {}
285 TimeTicks
unix_epoch() const { return unix_epoch_
; }
288 const TimeTicks unix_epoch_
;
290 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton
);
293 static LazyInstance
<UnixEpochSingleton
>::Leaky
294 leaky_unix_epoch_singleton_instance
= LAZY_INSTANCE_INITIALIZER
;
297 TimeTicks
TimeTicks::UnixEpoch() {
298 return leaky_unix_epoch_singleton_instance
.Get().unix_epoch();
301 TimeTicks
TimeTicks::SnappedToNextTick(TimeTicks tick_phase
,
302 TimeDelta tick_interval
) const {
303 // |interval_offset| is the offset from |this| to the next multiple of
304 // |tick_interval| after |tick_phase|, possibly negative if in the past.
305 TimeDelta interval_offset
= (tick_phase
- *this) % tick_interval
;
306 // If |this| is exactly on the interval (i.e. offset==0), don't adjust.
307 // Otherwise, if |tick_phase| was in the past, adjust forward to the next
308 // tick after |this|.
309 if (!interval_offset
.is_zero() && tick_phase
< *this)
310 interval_offset
+= tick_interval
;
311 return *this + interval_offset
;
314 std::ostream
& operator<<(std::ostream
& os
, TimeTicks time_ticks
) {
315 // This function formats a TimeTicks object as "bogo-microseconds".
316 // The origin and granularity of the count are platform-specific, and may very
317 // from run to run. Although bogo-microseconds usually roughly correspond to
318 // real microseconds, the only real guarantee is that the number never goes
319 // down during a single run.
320 const TimeDelta as_time_delta
= time_ticks
- TimeTicks();
321 return os
<< as_time_delta
.InMicroseconds() << " bogo-microseconds";
324 std::ostream
& operator<<(std::ostream
& os
, ThreadTicks thread_ticks
) {
325 const TimeDelta as_time_delta
= thread_ticks
- ThreadTicks();
326 return os
<< as_time_delta
.InMicroseconds() << " bogo-thread-microseconds";
329 std::ostream
& operator<<(std::ostream
& os
, TraceTicks trace_ticks
) {
330 const TimeDelta as_time_delta
= trace_ticks
- TraceTicks();
331 return os
<< as_time_delta
.InMicroseconds() << " bogo-trace-microseconds";
334 // Time::Exploded -------------------------------------------------------------
336 inline bool is_in_range(int value
, int lo
, int hi
) {
337 return lo
<= value
&& value
<= hi
;
340 bool Time::Exploded::HasValidValues() const {
341 return is_in_range(month
, 1, 12) &&
342 is_in_range(day_of_week
, 0, 6) &&
343 is_in_range(day_of_month
, 1, 31) &&
344 is_in_range(hour
, 0, 23) &&
345 is_in_range(minute
, 0, 59) &&
346 is_in_range(second
, 0, 60) &&
347 is_in_range(millisecond
, 0, 999);