Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: base/time/time.cc

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/time/time.h ('k') | base/time/time_mac.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/time.h"
6
7 #include <cmath>
8 #include <ios>
9 #include <limits>
10 #include <ostream>
11 #include <sstream>
12
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/third_party/nspr/prtime.h"
17
18 namespace base {
19
20 // TimeDelta ------------------------------------------------------------------
21
22 // static
23 TimeDelta TimeDelta::Max() {
24 return TimeDelta(std::numeric_limits<int64>::max());
25 }
26
27 int TimeDelta::InDays() const {
28 if (is_max()) {
29 // Preserve max to prevent overflow.
30 return std::numeric_limits<int>::max();
31 }
32 return static_cast<int>(delta_ / Time::kMicrosecondsPerDay);
33 }
34
35 int TimeDelta::InHours() const {
36 if (is_max()) {
37 // Preserve max to prevent overflow.
38 return std::numeric_limits<int>::max();
39 }
40 return static_cast<int>(delta_ / Time::kMicrosecondsPerHour);
41 }
42
43 int TimeDelta::InMinutes() const {
44 if (is_max()) {
45 // Preserve max to prevent overflow.
46 return std::numeric_limits<int>::max();
47 }
48 return static_cast<int>(delta_ / Time::kMicrosecondsPerMinute);
49 }
50
51 double TimeDelta::InSecondsF() const {
52 if (is_max()) {
53 // Preserve max to prevent overflow.
54 return std::numeric_limits<double>::infinity();
55 }
56 return static_cast<double>(delta_) / Time::kMicrosecondsPerSecond;
57 }
58
59 int64 TimeDelta::InSeconds() const {
60 if (is_max()) {
61 // Preserve max to prevent overflow.
62 return std::numeric_limits<int64>::max();
63 }
64 return delta_ / Time::kMicrosecondsPerSecond;
65 }
66
67 double TimeDelta::InMillisecondsF() const {
68 if (is_max()) {
69 // Preserve max to prevent overflow.
70 return std::numeric_limits<double>::infinity();
71 }
72 return static_cast<double>(delta_) / Time::kMicrosecondsPerMillisecond;
73 }
74
75 int64 TimeDelta::InMilliseconds() const {
76 if (is_max()) {
77 // Preserve max to prevent overflow.
78 return std::numeric_limits<int64>::max();
79 }
80 return delta_ / Time::kMicrosecondsPerMillisecond;
81 }
82
83 int64 TimeDelta::InMillisecondsRoundedUp() const {
84 if (is_max()) {
85 // Preserve max to prevent overflow.
86 return std::numeric_limits<int64>::max();
87 }
88 return (delta_ + Time::kMicrosecondsPerMillisecond - 1) /
89 Time::kMicrosecondsPerMillisecond;
90 }
91
92 int64 TimeDelta::InMicroseconds() const {
93 if (is_max()) {
94 // Preserve max to prevent overflow.
95 return std::numeric_limits<int64>::max();
96 }
97 return delta_;
98 }
99
100 namespace time_internal {
101
102 int64 SaturatedAdd(TimeDelta delta, int64 value) {
103 CheckedNumeric<int64> rv(delta.delta_);
104 rv += value;
105 return FromCheckedNumeric(rv);
106 }
107
108 int64 SaturatedSub(TimeDelta delta, int64 value) {
109 CheckedNumeric<int64> rv(delta.delta_);
110 rv -= value;
111 return FromCheckedNumeric(rv);
112 }
113
114 int64 FromCheckedNumeric(const CheckedNumeric<int64> value) {
115 if (value.IsValid())
116 return value.ValueUnsafe();
117
118 // We could return max/min but we don't really expose what the maximum delta
119 // is. Instead, return max/(-max), which is something that clients can reason
120 // about.
121 // TODO(rvargas) crbug.com/332611: don't use internal values.
122 int64 limit = std::numeric_limits<int64>::max();
123 if (value.validity() == internal::RANGE_UNDERFLOW)
124 limit = -limit;
125 return value.ValueOrDefault(limit);
126 }
127
128 } // namespace time_internal
129
130 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) {
131 return os << time_delta.InSecondsF() << "s";
132 }
133
134 // Time -----------------------------------------------------------------------
135
136 // static
137 Time Time::Max() {
138 return Time(std::numeric_limits<int64>::max());
139 }
140
141 // static
142 Time Time::FromTimeT(time_t tt) {
143 if (tt == 0)
144 return Time(); // Preserve 0 so we can tell it doesn't exist.
145 if (tt == std::numeric_limits<time_t>::max())
146 return Max();
147 return Time((tt * kMicrosecondsPerSecond) + kTimeTToMicrosecondsOffset);
148 }
149
150 time_t Time::ToTimeT() const {
151 if (is_null())
152 return 0; // Preserve 0 so we can tell it doesn't exist.
153 if (is_max()) {
154 // Preserve max without offset to prevent overflow.
155 return std::numeric_limits<time_t>::max();
156 }
157 if (std::numeric_limits<int64>::max() - kTimeTToMicrosecondsOffset <= us_) {
158 DLOG(WARNING) << "Overflow when converting base::Time with internal " <<
159 "value " << us_ << " to time_t.";
160 return std::numeric_limits<time_t>::max();
161 }
162 return (us_ - kTimeTToMicrosecondsOffset) / kMicrosecondsPerSecond;
163 }
164
165 // static
166 Time Time::FromDoubleT(double dt) {
167 if (dt == 0 || std::isnan(dt))
168 return Time(); // Preserve 0 so we can tell it doesn't exist.
169 if (dt == std::numeric_limits<double>::infinity())
170 return Max();
171 return Time(static_cast<int64>((dt *
172 static_cast<double>(kMicrosecondsPerSecond)) +
173 kTimeTToMicrosecondsOffset));
174 }
175
176 double Time::ToDoubleT() const {
177 if (is_null())
178 return 0; // Preserve 0 so we can tell it doesn't exist.
179 if (is_max()) {
180 // Preserve max without offset to prevent overflow.
181 return std::numeric_limits<double>::infinity();
182 }
183 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
184 static_cast<double>(kMicrosecondsPerSecond));
185 }
186
187 #if defined(OS_POSIX)
188 // static
189 Time Time::FromTimeSpec(const timespec& ts) {
190 return FromDoubleT(ts.tv_sec +
191 static_cast<double>(ts.tv_nsec) /
192 base::Time::kNanosecondsPerSecond);
193 }
194 #endif
195
196 // static
197 Time Time::FromJsTime(double ms_since_epoch) {
198 // The epoch is a valid time, so this constructor doesn't interpret
199 // 0 as the null time.
200 if (ms_since_epoch == std::numeric_limits<double>::infinity())
201 return Max();
202 return Time(static_cast<int64>(ms_since_epoch * kMicrosecondsPerMillisecond) +
203 kTimeTToMicrosecondsOffset);
204 }
205
206 double Time::ToJsTime() const {
207 if (is_null()) {
208 // Preserve 0 so the invalid result doesn't depend on the platform.
209 return 0;
210 }
211 if (is_max()) {
212 // Preserve max without offset to prevent overflow.
213 return std::numeric_limits<double>::infinity();
214 }
215 return (static_cast<double>(us_ - kTimeTToMicrosecondsOffset) /
216 kMicrosecondsPerMillisecond);
217 }
218
219 int64 Time::ToJavaTime() const {
220 if (is_null()) {
221 // Preserve 0 so the invalid result doesn't depend on the platform.
222 return 0;
223 }
224 if (is_max()) {
225 // Preserve max without offset to prevent overflow.
226 return std::numeric_limits<int64>::max();
227 }
228 return ((us_ - kTimeTToMicrosecondsOffset) /
229 kMicrosecondsPerMillisecond);
230 }
231
232 // static
233 Time Time::UnixEpoch() {
234 Time time;
235 time.us_ = kTimeTToMicrosecondsOffset;
236 return time;
237 }
238
239 Time Time::LocalMidnight() const {
240 Exploded exploded;
241 LocalExplode(&exploded);
242 exploded.hour = 0;
243 exploded.minute = 0;
244 exploded.second = 0;
245 exploded.millisecond = 0;
246 return FromLocalExploded(exploded);
247 }
248
249 // static
250 bool Time::FromStringInternal(const char* time_string,
251 bool is_local,
252 Time* parsed_time) {
253 DCHECK((time_string != NULL) && (parsed_time != NULL));
254
255 if (time_string[0] == '\0')
256 return false;
257
258 PRTime result_time = 0;
259 PRStatus result = PR_ParseTimeString(time_string,
260 is_local ? PR_FALSE : PR_TRUE,
261 &result_time);
262 if (PR_SUCCESS != result)
263 return false;
264
265 result_time += kTimeTToMicrosecondsOffset;
266 *parsed_time = Time(result_time);
267 return true;
268 }
269
270 std::ostream& operator<<(std::ostream& os, Time time) {
271 Time::Exploded exploded;
272 time.UTCExplode(&exploded);
273 // Use StringPrintf because iostreams formatting is painful.
274 return os << StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d UTC",
275 exploded.year,
276 exploded.month,
277 exploded.day_of_month,
278 exploded.hour,
279 exploded.minute,
280 exploded.second,
281 exploded.millisecond);
282 }
283
284 // Local helper class to hold the conversion from Time to TickTime at the
285 // time of the Unix epoch.
286 class UnixEpochSingleton {
287 public:
288 UnixEpochSingleton()
289 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {}
290
291 TimeTicks unix_epoch() const { return unix_epoch_; }
292
293 private:
294 const TimeTicks unix_epoch_;
295
296 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton);
297 };
298
299 static LazyInstance<UnixEpochSingleton>::Leaky
300 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER;
301
302 // Static
303 TimeTicks TimeTicks::UnixEpoch() {
304 return leaky_unix_epoch_singleton_instance.Get().unix_epoch();
305 }
306
307 TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase,
308 TimeDelta tick_interval) const {
309 // |interval_offset| is the offset from |this| to the next multiple of
310 // |tick_interval| after |tick_phase|, possibly negative if in the past.
311 TimeDelta interval_offset = (tick_phase - *this) % tick_interval;
312 // If |this| is exactly on the interval (i.e. offset==0), don't adjust.
313 // Otherwise, if |tick_phase| was in the past, adjust forward to the next
314 // tick after |this|.
315 if (!interval_offset.is_zero() && tick_phase < *this)
316 interval_offset += tick_interval;
317 return *this + interval_offset;
318 }
319
320 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) {
321 // This function formats a TimeTicks object as "bogo-microseconds".
322 // The origin and granularity of the count are platform-specific, and may very
323 // from run to run. Although bogo-microseconds usually roughly correspond to
324 // real microseconds, the only real guarantee is that the number never goes
325 // down during a single run.
326 const TimeDelta as_time_delta = time_ticks - TimeTicks();
327 return os << as_time_delta.InMicroseconds() << " bogo-microseconds";
328 }
329
330 std::ostream& operator<<(std::ostream& os, ThreadTicks thread_ticks) {
331 const TimeDelta as_time_delta = thread_ticks - ThreadTicks();
332 return os << as_time_delta.InMicroseconds() << " bogo-thread-microseconds";
333 }
334
335 std::ostream& operator<<(std::ostream& os, TraceTicks trace_ticks) {
336 const TimeDelta as_time_delta = trace_ticks - TraceTicks();
337 return os << as_time_delta.InMicroseconds() << " bogo-trace-microseconds";
338 }
339
340 // Time::Exploded -------------------------------------------------------------
341
342 inline bool is_in_range(int value, int lo, int hi) {
343 return lo <= value && value <= hi;
344 }
345
346 bool Time::Exploded::HasValidValues() const {
347 return is_in_range(month, 1, 12) &&
348 is_in_range(day_of_week, 0, 6) &&
349 is_in_range(day_of_month, 1, 31) &&
350 is_in_range(hour, 0, 23) &&
351 is_in_range(minute, 0, 59) &&
352 is_in_range(second, 0, 60) &&
353 is_in_range(millisecond, 0, 999);
354 }
355
356 } // namespace base
OLDNEW
« no previous file with comments | « base/time/time.h ('k') | base/time/time_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698