blob: 26fd500403e961b0497cfed93b21aee97e6e9174 [file] [log] [blame]
[email protected]527c7f32012-06-06 05:04:221// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
5#include "base/time.h"
[email protected]527c7f32012-06-06 05:04:226
7#include <math.h>
8#if defined(OS_WIN)
9#include <float.h>
10#endif
11
[email protected]c462caf2012-08-27 18:44:0312#include <limits>
13
[email protected]9fe1a5b2013-02-07 19:18:0314#include "base/strings/sys_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3815#include "base/third_party/nspr/prtime.h"
16
17#include "base/logging.h"
18
[email protected]e1acf6f2008-10-27 20:43:3319namespace base {
20
[email protected]527c7f32012-06-06 05:04:2221namespace {
22#if defined(OS_WIN)
23inline bool isnan(double num) { return !!_isnan(num); }
24#endif
25}
26
initial.commitd7cae122008-07-26 21:49:3827// TimeDelta ------------------------------------------------------------------
28
initial.commitd7cae122008-07-26 21:49:3829int TimeDelta::InDays() const {
30 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
31}
32
33int TimeDelta::InHours() const {
34 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
35}
36
[email protected]9f251302008-08-19 09:16:4937int TimeDelta::InMinutes() const {
38 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
39}
40
initial.commitd7cae122008-07-26 21:49:3841double TimeDelta::InSecondsF() const {
42 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
43}
44
45int64 TimeDelta::InSeconds() const {
46 return delta_ / Time::kMicrosecondsPerSecond;
47}
48
49double TimeDelta::InMillisecondsF() const {
50 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
51}
52
53int64 TimeDelta::InMilliseconds() const {
54 return delta_ / Time::kMicrosecondsPerMillisecond;
55}
56
[email protected]6b175382009-10-13 06:47:4757int64 TimeDelta::InMillisecondsRoundedUp() const {
58 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
59 Time::kMicrosecondsPerMillisecond;
60}
61
initial.commitd7cae122008-07-26 21:49:3862int64 TimeDelta::InMicroseconds() const {
63 return delta_;
64}
65
66// Time -----------------------------------------------------------------------
67
initial.commitd7cae122008-07-26 21:49:3868// static
[email protected]c462caf2012-08-27 18:44:0369Time Time::Max() {
70 return Time(std::numeric_limits<int64>::max());
71}
72
73// static
initial.commitd7cae122008-07-26 21:49:3874Time Time::FromTimeT(time_t tt) {
75 if (tt == 0)
76 return Time(); // Preserve 0 so we can tell it doesn't exist.
[email protected]a9e5f0442012-09-08 17:50:0777 if (tt == std::numeric_limits<time_t>::max())
78 return Max();
[email protected]b842d4c62009-09-14 18:49:0379 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
initial.commitd7cae122008-07-26 21:49:3880}
81
82time_t Time::ToTimeT() const {
[email protected]a9e5f0442012-09-08 17:50:0783 if (is_null())
initial.commitd7cae122008-07-26 21:49:3884 return 0; // Preserve 0 so we can tell it doesn't exist.
[email protected]a9e5f0442012-09-08 17:50:0785 if (is_max()) {
86 // Preserve max without offset to prevent overflow.
87 return std::numeric_limits<time_t>::max();
88 }
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();
93 }
initial.commitd7cae122008-07-26 21:49:3894 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
95}
96
[email protected]c20210e62009-04-03 21:39:2697// static
98Time Time::FromDoubleT(double dt) {
[email protected]527c7f32012-06-06 05:04:2299 if (dt == 0 || isnan(dt))
[email protected]c925ad12010-09-09 17:17:55100 return Time(); // Preserve 0 so we can tell it doesn't exist.
[email protected]a9e5f0442012-09-08 17:50:07101 if (dt == std::numeric_limits<double>::max())
102 return Max();
[email protected]b842d4c62009-09-14 18:49:03103 return Time(static_cast<int64>((dt *
104 static_cast<double>(kMicrosecondsPerSecond)) +
105 kTimeTToMicrosecondsOffset));
[email protected]c20210e62009-04-03 21:39:26106}
107
initial.commitd7cae122008-07-26 21:49:38108double Time::ToDoubleT() const {
[email protected]a9e5f0442012-09-08 17:50:07109 if (is_null())
initial.commitd7cae122008-07-26 21:49:38110 return 0; // Preserve 0 so we can tell it doesn't exist.
[email protected]a9e5f0442012-09-08 17:50:07111 if (is_max()) {
112 // Preserve max without offset to prevent overflow.
113 return std::numeric_limits<double>::max();
114 }
initial.commitd7cae122008-07-26 21:49:38115 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
116 static_cast<double>(kMicrosecondsPerSecond));
117}
118
[email protected]c925ad12010-09-09 17:17:55119// static
[email protected]42132be2012-06-08 22:59:33120Time 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.
[email protected]a9e5f0442012-09-08 17:50:07123 if (ms_since_epoch == std::numeric_limits<double>::max())
124 return Max();
[email protected]42132be2012-06-08 22:59:33125 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
126 kTimeTToMicrosecondsOffset);
127}
128
129double Time::ToJsTime() const {
[email protected]a9e5f0442012-09-08 17:50:07130 if (is_null()) {
[email protected]42132be2012-06-08 22:59:33131 // Preserve 0 so the invalid result doesn't depend on the platform.
132 return 0;
133 }
[email protected]a9e5f0442012-09-08 17:50:07134 if (is_max()) {
135 // Preserve max without offset to prevent overflow.
136 return std::numeric_limits<double>::max();
137 }
[email protected]42132be2012-06-08 22:59:33138 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
139 kMicrosecondsPerMillisecond);
140}
141
142// static
[email protected]c925ad12010-09-09 17:17:55143Time Time::UnixEpoch() {
144 Time time;
145 time.us_ = kTimeTToMicrosecondsOffset;
146 return time;
147}
148
initial.commitd7cae122008-07-26 21:49:38149Time Time::LocalMidnight() const {
150 Exploded exploded;
151 LocalExplode(&exploded);
152 exploded.hour = 0;
153 exploded.minute = 0;
154 exploded.second = 0;
155 exploded.millisecond = 0;
156 return FromLocalExploded(exploded);
157}
158
159// static
[email protected]6e6acc102012-10-29 22:54:58160bool Time::FromStringInternal(const char* time_string,
161 bool is_local,
162 Time* parsed_time) {
initial.commitd7cae122008-07-26 21:49:38163 DCHECK((time_string != NULL) && (parsed_time != NULL));
[email protected]46470aa2011-08-03 05:28:10164
165 if (time_string[0] == '\0')
initial.commitd7cae122008-07-26 21:49:38166 return false;
[email protected]46470aa2011-08-03 05:28:10167
initial.commitd7cae122008-07-26 21:49:38168 PRTime result_time = 0;
[email protected]6e6acc102012-10-29 22:54:58169 PRStatus result = PR_ParseTimeString(time_string,
170 is_local ? PR_FALSE : PR_TRUE,
initial.commitd7cae122008-07-26 21:49:38171 &result_time);
172 if (PR_SUCCESS != result)
173 return false;
[email protected]46470aa2011-08-03 05:28:10174
initial.commitd7cae122008-07-26 21:49:38175 result_time += kTimeTToMicrosecondsOffset;
176 *parsed_time = Time(result_time);
177 return true;
178}
license.botbf09a502008-08-24 00:55:55179
[email protected]e2419082010-07-27 02:43:20180// Time::Exploded -------------------------------------------------------------
181
182inline bool is_in_range(int value, int lo, int hi) {
183 return lo <= value && value <= hi;
184}
185
186bool 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);
194}
195
[email protected]e1acf6f2008-10-27 20:43:33196} // namespace base