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.
14 #include "base/strings/sys_string_conversions.h"
15 #include "base/third_party/nspr/prtime.h"
17 #include "base/logging.h"
23 inline bool isnan(double num
) { return !!_isnan(num
); }
27 // TimeDelta ------------------------------------------------------------------
29 int TimeDelta::InDays() const {
30 return static_cast<int>(delta_
/ Time::kMicrosecondsPerDay
);
33 int TimeDelta::InHours() const {
34 return static_cast<int>(delta_
/ Time::kMicrosecondsPerHour
);
37 int TimeDelta::InMinutes() const {
38 return static_cast<int>(delta_
/ Time::kMicrosecondsPerMinute
);
41 double TimeDelta::InSecondsF() const {
42 return static_cast<double>(delta_
) / Time::kMicrosecondsPerSecond
;
45 int64
TimeDelta::InSeconds() const {
46 return delta_
/ Time::kMicrosecondsPerSecond
;
49 double TimeDelta::InMillisecondsF() const {
50 return static_cast<double>(delta_
) / Time::kMicrosecondsPerMillisecond
;
53 int64
TimeDelta::InMilliseconds() const {
54 return delta_
/ Time::kMicrosecondsPerMillisecond
;
57 int64
TimeDelta::InMillisecondsRoundedUp() const {
58 return (delta_
+ Time::kMicrosecondsPerMillisecond
- 1) /
59 Time::kMicrosecondsPerMillisecond
;
62 int64
TimeDelta::InMicroseconds() const {
66 // Time -----------------------------------------------------------------------
70 return Time(std::numeric_limits
<int64
>::max());
74 Time
Time::FromTimeT(time_t tt
) {
76 return Time(); // Preserve 0 so we can tell it doesn't exist.
77 if (tt
== std::numeric_limits
<time_t>::max())
79 return Time((tt
* kMicrosecondsPerSecond
) + kTimeTToMicrosecondsOffset
);
82 time_t Time::ToTimeT() const {
84 return 0; // Preserve 0 so we can tell it doesn't exist.
86 // Preserve max without offset to prevent overflow.
87 return std::numeric_limits
<time_t>::max();
89 if (std::numeric_limits
<int64
>::max() - kTimeTToMicrosecondsOffset
<= us_
) {
90 DLOG(WARNING
) << "Overflow when converting base::Time with internal " <<
91 "value " << us_
<< " to time_t.";
92 return std::numeric_limits
<time_t>::max();
94 return (us_
- kTimeTToMicrosecondsOffset
) / kMicrosecondsPerSecond
;
98 Time
Time::FromDoubleT(double dt
) {
99 if (dt
== 0 || isnan(dt
))
100 return Time(); // Preserve 0 so we can tell it doesn't exist.
101 if (dt
== std::numeric_limits
<double>::max())
103 return Time(static_cast<int64
>((dt
*
104 static_cast<double>(kMicrosecondsPerSecond
)) +
105 kTimeTToMicrosecondsOffset
));
108 double Time::ToDoubleT() const {
110 return 0; // Preserve 0 so we can tell it doesn't exist.
112 // Preserve max without offset to prevent overflow.
113 return std::numeric_limits
<double>::max();
115 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
116 static_cast<double>(kMicrosecondsPerSecond
));
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>::max())
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>::max();
138 return (static_cast<double>(us_
- kTimeTToMicrosecondsOffset
) /
139 kMicrosecondsPerMillisecond
);
143 Time
Time::UnixEpoch() {
145 time
.us_
= kTimeTToMicrosecondsOffset
;
149 Time
Time::LocalMidnight() const {
151 LocalExplode(&exploded
);
155 exploded
.millisecond
= 0;
156 return FromLocalExploded(exploded
);
160 bool Time::FromStringInternal(const char* time_string
,
163 DCHECK((time_string
!= NULL
) && (parsed_time
!= NULL
));
165 if (time_string
[0] == '\0')
168 PRTime result_time
= 0;
169 PRStatus result
= PR_ParseTimeString(time_string
,
170 is_local
? PR_FALSE
: PR_TRUE
,
172 if (PR_SUCCESS
!= result
)
175 result_time
+= kTimeTToMicrosecondsOffset
;
176 *parsed_time
= Time(result_time
);
180 // Time::Exploded -------------------------------------------------------------
182 inline bool is_in_range(int value
, int lo
, int hi
) {
183 return lo
<= value
&& value
<= hi
;
186 bool Time::Exploded::HasValidValues() const {
187 return is_in_range(month
, 1, 12) &&
188 is_in_range(day_of_week
, 0, 6) &&
189 is_in_range(day_of_month
, 1, 31) &&
190 is_in_range(hour
, 0, 23) &&
191 is_in_range(minute
, 0, 59) &&
192 is_in_range(second
, 0, 60) &&
193 is_in_range(millisecond
, 0, 999);