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"
14 #include "base/third_party/nspr/prtypes.h"
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 {
57 // Time -----------------------------------------------------------------------
61 return Time(std::numeric_limits
<int64
>::max());
65 Time
Time::FromTimeT(time_t tt
) {
67 return Time(); // Preserve 0 so we can tell it doesn't exist.
68 if (tt
== std::numeric_limits
<time_t>::max())
70 return Time((tt
* kMicrosecondsPerSecond
) + kTimeTToMicrosecondsOffset
);
73 time_t Time::ToTimeT() const {
75 return 0; // Preserve 0 so we can tell it doesn't exist.
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
;
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())
94 return Time(static_cast<int64
>((dt
*
95 static_cast<double>(kMicrosecondsPerSecond
)) +
96 kTimeTToMicrosecondsOffset
));
99 double Time::ToDoubleT() const {
101 return 0; // Preserve 0 so we can tell it doesn't exist.
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)
112 Time
Time::FromTimeSpec(const timespec
& ts
) {
113 return FromDoubleT(ts
.tv_sec
+
114 static_cast<double>(ts
.tv_nsec
) /
115 base::Time::kNanosecondsPerSecond
);
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())
125 return Time(static_cast<int64
>(ms_since_epoch
* kMicrosecondsPerMillisecond
) +
126 kTimeTToMicrosecondsOffset
);
129 double Time::ToJsTime() const {
131 // Preserve 0 so the invalid result doesn't depend on the platform.
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 {
144 // Preserve 0 so the invalid result doesn't depend on the platform.
148 // Preserve max without offset to prevent overflow.
149 return std::numeric_limits
<int64
>::max();
151 return ((us_
- kTimeTToMicrosecondsOffset
) /
152 kMicrosecondsPerMillisecond
);
156 Time
Time::UnixEpoch() {
158 time
.us_
= kTimeTToMicrosecondsOffset
;
162 Time
Time::LocalMidnight() const {
164 LocalExplode(&exploded
);
168 exploded
.millisecond
= 0;
169 return FromLocalExploded(exploded
);
173 bool Time::FromStringInternal(const char* time_string
,
176 DCHECK((time_string
!= NULL
) && (parsed_time
!= NULL
));
178 if (time_string
[0] == '\0')
181 PRTime result_time
= 0;
182 PRStatus result
= PR_ParseTimeString(time_string
,
183 is_local
? PR_FALSE
: PR_TRUE
,
185 if (PR_SUCCESS
!= result
)
188 result_time
+= kTimeTToMicrosecondsOffset
;
189 *parsed_time
= Time(result_time
);
193 // Local helper class to hold the conversion from Time to TickTime at the
194 // time of the Unix epoch.
195 class UnixEpochSingleton
{
198 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {}
200 TimeTicks
unix_epoch() const { return unix_epoch_
; }
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
;
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);