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"
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"
17 // TimeDelta ------------------------------------------------------------------
20 TimeDelta
TimeDelta::Max() {
21 return TimeDelta(std::numeric_limits
<int64
>::max());
24 int TimeDelta::InDays() const {
26 // Preserve max to prevent overflow.
27 return std::numeric_limits
<int>::max();
29 return static_cast<int>(delta_
/ Time::kMicrosecondsPerDay
);
32 int TimeDelta::InHours() const {
34 // Preserve max to prevent overflow.
35 return std::numeric_limits
<int>::max();
37 return static_cast<int>(delta_
/ Time::kMicrosecondsPerHour
);
40 int TimeDelta::InMinutes() const {
42 // Preserve max to prevent overflow.
43 return std::numeric_limits
<int>::max();
45 return static_cast<int>(delta_
/ Time::kMicrosecondsPerMinute
);
48 double TimeDelta::InSecondsF() const {
50 // Preserve max to prevent overflow.
51 return std::numeric_limits
<double>::infinity();
53 return static_cast<double>(delta_
) / Time::kMicrosecondsPerSecond
;
56 int64
TimeDelta::InSeconds() const {
58 // Preserve max to prevent overflow.
59 return std::numeric_limits
<int64
>::max();
61 return delta_
/ Time::kMicrosecondsPerSecond
;
64 double TimeDelta::InMillisecondsF() const {
66 // Preserve max to prevent overflow.
67 return std::numeric_limits
<double>::infinity();
69 return static_cast<double>(delta_
) / Time::kMicrosecondsPerMillisecond
;
72 int64
TimeDelta::InMilliseconds() const {
74 // Preserve max to prevent overflow.
75 return std::numeric_limits
<int64
>::max();
77 return delta_
/ Time::kMicrosecondsPerMillisecond
;
80 int64
TimeDelta::InMillisecondsRoundedUp() const {
82 // Preserve max to prevent overflow.
83 return std::numeric_limits
<int64
>::max();
85 return (delta_
+ Time::kMicrosecondsPerMillisecond
- 1) /
86 Time::kMicrosecondsPerMillisecond
;
89 int64
TimeDelta::InMicroseconds() const {
91 // Preserve max to prevent overflow.
92 return std::numeric_limits
<int64
>::max();
97 // Time -----------------------------------------------------------------------
101 return Time(std::numeric_limits
<int64
>::max());
105 Time
Time::FromTimeT(time_t tt
) {
107 return Time(); // Preserve 0 so we can tell it doesn't exist.
108 if (tt
== std::numeric_limits
<time_t>::max())
110 return Time((tt
* kMicrosecondsPerSecond
) + kTimeTToMicrosecondsOffset
);
113 time_t Time::ToTimeT() const {
115 return 0; // Preserve 0 so we can tell it doesn't exist.
117 // Preserve max without offset to prevent overflow.
118 return std::numeric_limits
<time_t>::max();
120 if (std::numeric_limits
<int64
>::max() - kTimeTToMicrosecondsOffset
<= us_
) {
121 DLOG(WARNING
) << "Overflow when converting base::Time with internal " <<
122 "value " << us_
<< " to time_t.";
123 return std::numeric_limits
<time_t>::max();
125 return (us_
- kTimeTToMicrosecondsOffset
) / kMicrosecondsPerSecond
;
129 Time
Time::FromDoubleT(double dt
) {
130 if (dt
== 0 || IsNaN(dt
))
131 return Time(); // Preserve 0 so we can tell it doesn't exist.
132 if (dt
== std::numeric_limits
<double>::infinity())
134 return Time(static_cast<int64
>((dt
*
135 static_cast<double>(kMicrosecondsPerSecond
)) +
136 kTimeTToMicrosecondsOffset
));
139 double Time::ToDoubleT() const {
141 return 0; // Preserve 0 so we can tell it doesn't exist.
143 // Preserve max without offset to prevent overflow.
144 return std::numeric_limits
<double>::infinity();
146 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
147 static_cast<double>(kMicrosecondsPerSecond
));
150 #if defined(OS_POSIX)
152 Time
Time::FromTimeSpec(const timespec
& ts
) {
153 return FromDoubleT(ts
.tv_sec
+
154 static_cast<double>(ts
.tv_nsec
) /
155 base::Time::kNanosecondsPerSecond
);
160 Time
Time::FromJsTime(double ms_since_epoch
) {
161 // The epoch is a valid time, so this constructor doesn't interpret
162 // 0 as the null time.
163 if (ms_since_epoch
== std::numeric_limits
<double>::infinity())
165 return Time(static_cast<int64
>(ms_since_epoch
* kMicrosecondsPerMillisecond
) +
166 kTimeTToMicrosecondsOffset
);
169 double Time::ToJsTime() const {
171 // Preserve 0 so the invalid result doesn't depend on the platform.
175 // Preserve max without offset to prevent overflow.
176 return std::numeric_limits
<double>::infinity();
178 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
179 kMicrosecondsPerMillisecond
);
182 int64
Time::ToJavaTime() const {
184 // Preserve 0 so the invalid result doesn't depend on the platform.
188 // Preserve max without offset to prevent overflow.
189 return std::numeric_limits
<int64
>::max();
191 return ((us_
- kTimeTToMicrosecondsOffset
) /
192 kMicrosecondsPerMillisecond
);
196 Time
Time::UnixEpoch() {
198 time
.us_
= kTimeTToMicrosecondsOffset
;
202 Time
Time::LocalMidnight() const {
204 LocalExplode(&exploded
);
208 exploded
.millisecond
= 0;
209 return FromLocalExploded(exploded
);
213 bool Time::FromStringInternal(const char* time_string
,
216 DCHECK((time_string
!= NULL
) && (parsed_time
!= NULL
));
218 if (time_string
[0] == '\0')
221 PRTime result_time
= 0;
222 PRStatus result
= PR_ParseTimeString(time_string
,
223 is_local
? PR_FALSE
: PR_TRUE
,
225 if (PR_SUCCESS
!= result
)
228 result_time
+= kTimeTToMicrosecondsOffset
;
229 *parsed_time
= Time(result_time
);
233 // Local helper class to hold the conversion from Time to TickTime at the
234 // time of the Unix epoch.
235 class UnixEpochSingleton
{
238 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {}
240 TimeTicks
unix_epoch() const { return unix_epoch_
; }
243 const TimeTicks unix_epoch_
;
245 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton
);
248 static LazyInstance
<UnixEpochSingleton
>::Leaky
249 leaky_unix_epoch_singleton_instance
= LAZY_INSTANCE_INITIALIZER
;
252 TimeTicks
TimeTicks::UnixEpoch() {
253 return leaky_unix_epoch_singleton_instance
.Get().unix_epoch();
256 // Time::Exploded -------------------------------------------------------------
258 inline bool is_in_range(int value
, int lo
, int hi
) {
259 return lo
<= value
&& value
<= hi
;
262 bool Time::Exploded::HasValidValues() const {
263 return is_in_range(month
, 1, 12) &&
264 is_in_range(day_of_week
, 0, 6) &&
265 is_in_range(day_of_month
, 1, 31) &&
266 is_in_range(hour
, 0, 23) &&
267 is_in_range(minute
, 0, 59) &&
268 is_in_range(second
, 0, 60) &&
269 is_in_range(millisecond
, 0, 999);