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/logging.h"
12 #include "base/third_party/nspr/prtime.h"
13 #include "base/third_party/nspr/prtypes.h"
17 // TimeDelta ------------------------------------------------------------------
19 int TimeDelta::InDays() const {
20 return static_cast<int>(delta_
/ Time::kMicrosecondsPerDay
);
23 int TimeDelta::InHours() const {
24 return static_cast<int>(delta_
/ Time::kMicrosecondsPerHour
);
27 int TimeDelta::InMinutes() const {
28 return static_cast<int>(delta_
/ Time::kMicrosecondsPerMinute
);
31 double TimeDelta::InSecondsF() const {
32 return static_cast<double>(delta_
) / Time::kMicrosecondsPerSecond
;
35 int64
TimeDelta::InSeconds() const {
36 return delta_
/ Time::kMicrosecondsPerSecond
;
39 double TimeDelta::InMillisecondsF() const {
40 return static_cast<double>(delta_
) / Time::kMicrosecondsPerMillisecond
;
43 int64
TimeDelta::InMilliseconds() const {
44 return delta_
/ Time::kMicrosecondsPerMillisecond
;
47 int64
TimeDelta::InMillisecondsRoundedUp() const {
48 return (delta_
+ Time::kMicrosecondsPerMillisecond
- 1) /
49 Time::kMicrosecondsPerMillisecond
;
52 int64
TimeDelta::InMicroseconds() const {
56 // Time -----------------------------------------------------------------------
60 return Time(std::numeric_limits
<int64
>::max());
64 Time
Time::FromTimeT(time_t tt
) {
66 return Time(); // Preserve 0 so we can tell it doesn't exist.
67 if (tt
== std::numeric_limits
<time_t>::max())
69 return Time((tt
* kMicrosecondsPerSecond
) + kTimeTToMicrosecondsOffset
);
72 time_t Time::ToTimeT() const {
74 return 0; // Preserve 0 so we can tell it doesn't exist.
76 // Preserve max without offset to prevent overflow.
77 return std::numeric_limits
<time_t>::max();
79 if (std::numeric_limits
<int64
>::max() - kTimeTToMicrosecondsOffset
<= us_
) {
80 DLOG(WARNING
) << "Overflow when converting base::Time with internal " <<
81 "value " << us_
<< " to time_t.";
82 return std::numeric_limits
<time_t>::max();
84 return (us_
- kTimeTToMicrosecondsOffset
) / kMicrosecondsPerSecond
;
88 Time
Time::FromDoubleT(double dt
) {
89 if (dt
== 0 || IsNaN(dt
))
90 return Time(); // Preserve 0 so we can tell it doesn't exist.
91 if (dt
== std::numeric_limits
<double>::max())
93 return Time(static_cast<int64
>((dt
*
94 static_cast<double>(kMicrosecondsPerSecond
)) +
95 kTimeTToMicrosecondsOffset
));
98 double Time::ToDoubleT() const {
100 return 0; // Preserve 0 so we can tell it doesn't exist.
102 // Preserve max without offset to prevent overflow.
103 return std::numeric_limits
<double>::max();
105 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
106 static_cast<double>(kMicrosecondsPerSecond
));
109 #if defined(OS_POSIX)
111 Time
Time::FromTimeSpec(const timespec
& ts
) {
112 return FromDoubleT(ts
.tv_sec
+
113 static_cast<double>(ts
.tv_nsec
) /
114 base::Time::kNanosecondsPerSecond
);
119 Time
Time::FromJsTime(double ms_since_epoch
) {
120 // The epoch is a valid time, so this constructor doesn't interpret
121 // 0 as the null time.
122 if (ms_since_epoch
== std::numeric_limits
<double>::max())
124 return Time(static_cast<int64
>(ms_since_epoch
* kMicrosecondsPerMillisecond
) +
125 kTimeTToMicrosecondsOffset
);
128 double Time::ToJsTime() const {
130 // Preserve 0 so the invalid result doesn't depend on the platform.
134 // Preserve max without offset to prevent overflow.
135 return std::numeric_limits
<double>::max();
137 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
138 kMicrosecondsPerMillisecond
);
142 Time
Time::UnixEpoch() {
144 time
.us_
= kTimeTToMicrosecondsOffset
;
148 Time
Time::LocalMidnight() const {
150 LocalExplode(&exploded
);
154 exploded
.millisecond
= 0;
155 return FromLocalExploded(exploded
);
159 bool Time::FromStringInternal(const char* time_string
,
162 DCHECK((time_string
!= NULL
) && (parsed_time
!= NULL
));
164 if (time_string
[0] == '\0')
167 PRTime result_time
= 0;
168 PRStatus result
= PR_ParseTimeString(time_string
,
169 is_local
? PR_FALSE
: PR_TRUE
,
171 if (PR_SUCCESS
!= result
)
174 result_time
+= kTimeTToMicrosecondsOffset
;
175 *parsed_time
= Time(result_time
);
179 // Time::Exploded -------------------------------------------------------------
181 inline bool is_in_range(int value
, int lo
, int hi
) {
182 return lo
<= value
&& value
<= hi
;
185 bool Time::Exploded::HasValidValues() const {
186 return is_in_range(month
, 1, 12) &&
187 is_in_range(day_of_week
, 0, 6) &&
188 is_in_range(day_of_month
, 1, 31) &&
189 is_in_range(hour
, 0, 23) &&
190 is_in_range(minute
, 0, 59) &&
191 is_in_range(second
, 0, 60) &&
192 is_in_range(millisecond
, 0, 999);