OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "base/time.h" | |
6 | |
7 #include <math.h> | |
8 #if defined(OS_WIN) | |
9 #include <float.h> | |
10 #endif | |
11 | |
12 #include <limits> | |
13 | |
14 #include "base/strings/sys_string_conversions.h" | |
15 #include "base/third_party/nspr/prtime.h" | |
16 | |
17 #include "base/logging.h" | |
18 | |
19 namespace base { | |
20 | |
21 namespace { | |
22 #if defined(OS_WIN) | |
23 inline bool isnan(double num) { return !!_isnan(num); } | |
24 #endif | |
25 } | |
26 | |
27 // TimeDelta ------------------------------------------------------------------ | |
28 | |
29 int TimeDelta::InDays() const { | |
30 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay); | |
31 } | |
32 | |
33 int TimeDelta::InHours() const { | |
34 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour); | |
35 } | |
36 | |
37 int TimeDelta::InMinutes() const { | |
38 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute); | |
39 } | |
40 | |
41 double TimeDelta::InSecondsF() const { | |
42 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond; | |
43 } | |
44 | |
45 int64 TimeDelta::InSeconds() const { | |
46 return delta_ / Time::kMicrosecondsPerSecond; | |
47 } | |
48 | |
49 double TimeDelta::InMillisecondsF() const { | |
50 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond; | |
51 } | |
52 | |
53 int64 TimeDelta::InMilliseconds() const { | |
54 return delta_ / Time::kMicrosecondsPerMillisecond; | |
55 } | |
56 | |
57 int64 TimeDelta::InMillisecondsRoundedUp() const { | |
58 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) / | |
59 Time::kMicrosecondsPerMillisecond; | |
60 } | |
61 | |
62 int64 TimeDelta::InMicroseconds() const { | |
63 return delta_; | |
64 } | |
65 | |
66 // Time ----------------------------------------------------------------------- | |
67 | |
68 // static | |
69 Time Time::Max() { | |
70 return Time(std::numeric_limits<int64>::max()); | |
71 } | |
72 | |
73 // static | |
74 Time Time::FromTimeT(time_t tt) { | |
75 if (tt == 0) | |
76 return Time(); // Preserve 0 so we can tell it doesn't exist. | |
77 if (tt == std::numeric_limits<time_t>::max()) | |
78 return Max(); | |
79 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset); | |
80 } | |
81 | |
82 time_t Time::ToTimeT() const { | |
83 if (is_null()) | |
84 return 0; // Preserve 0 so we can tell it doesn't exist. | |
85 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 } | |
94 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond; | |
95 } | |
96 | |
97 // static | |
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()) | |
102 return Max(); | |
103 return Time(static_cast<int64>((dt * | |
104 static_cast<double>(kMicrosecondsPerSecond)) + | |
105 kTimeTToMicrosecondsOffset)); | |
106 } | |
107 | |
108 double Time::ToDoubleT() const { | |
109 if (is_null()) | |
110 return 0; // Preserve 0 so we can tell it doesn't exist. | |
111 if (is_max()) { | |
112 // Preserve max without offset to prevent overflow. | |
113 return std::numeric_limits<double>::max(); | |
114 } | |
115 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / | |
116 static_cast<double>(kMicrosecondsPerSecond)); | |
117 } | |
118 | |
119 #if defined(OS_POSIX) | |
120 // static | |
121 Time Time::FromTimeSpec(const timespec& ts) { | |
122 return FromDoubleT(ts.tv_sec + | |
123 static_cast<double>(ts.tv_nsec) / | |
124 base::Time::kNanosecondsPerSecond); | |
125 } | |
126 #endif | |
127 | |
128 // static | |
129 Time Time::FromJsTime(double ms_since_epoch) { | |
130 // The epoch is a valid time, so this constructor doesn't interpret | |
131 // 0 as the null time. | |
132 if (ms_since_epoch == std::numeric_limits<double>::max()) | |
133 return Max(); | |
134 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) + | |
135 kTimeTToMicrosecondsOffset); | |
136 } | |
137 | |
138 double Time::ToJsTime() const { | |
139 if (is_null()) { | |
140 // Preserve 0 so the invalid result doesn't depend on the platform. | |
141 return 0; | |
142 } | |
143 if (is_max()) { | |
144 // Preserve max without offset to prevent overflow. | |
145 return std::numeric_limits<double>::max(); | |
146 } | |
147 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) / | |
148 kMicrosecondsPerMillisecond); | |
149 } | |
150 | |
151 // static | |
152 Time Time::UnixEpoch() { | |
153 Time time; | |
154 time.us_ = kTimeTToMicrosecondsOffset; | |
155 return time; | |
156 } | |
157 | |
158 Time Time::LocalMidnight() const { | |
159 Exploded exploded; | |
160 LocalExplode(&exploded); | |
161 exploded.hour = 0; | |
162 exploded.minute = 0; | |
163 exploded.second = 0; | |
164 exploded.millisecond = 0; | |
165 return FromLocalExploded(exploded); | |
166 } | |
167 | |
168 // static | |
169 bool Time::FromStringInternal(const char* time_string, | |
170 bool is_local, | |
171 Time* parsed_time) { | |
172 DCHECK((time_string != NULL) && (parsed_time != NULL)); | |
173 | |
174 if (time_string[0] == '\0') | |
175 return false; | |
176 | |
177 PRTime result_time = 0; | |
178 PRStatus result = PR_ParseTimeString(time_string, | |
179 is_local ? PR_FALSE : PR_TRUE, | |
180 &result_time); | |
181 if (PR_SUCCESS != result) | |
182 return false; | |
183 | |
184 result_time += kTimeTToMicrosecondsOffset; | |
185 *parsed_time = Time(result_time); | |
186 return true; | |
187 } | |
188 | |
189 // Time::Exploded ------------------------------------------------------------- | |
190 | |
191 inline bool is_in_range(int value, int lo, int hi) { | |
192 return lo <= value && value <= hi; | |
193 } | |
194 | |
195 bool Time::Exploded::HasValidValues() const { | |
196 return is_in_range(month, 1, 12) && | |
197 is_in_range(day_of_week, 0, 6) && | |
198 is_in_range(day_of_month, 1, 31) && | |
199 is_in_range(hour, 0, 23) && | |
200 is_in_range(minute, 0, 59) && | |
201 is_in_range(second, 0, 60) && | |
202 is_in_range(millisecond, 0, 999); | |
203 } | |
204 | |
205 } // namespace base | |
OLD | NEW |