OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <ios> | 7 #include <ios> |
8 #include <limits> | 8 #include <limits> |
9 #include <ostream> | 9 #include <ostream> |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 }; | 267 }; |
268 | 268 |
269 static LazyInstance<UnixEpochSingleton>::Leaky | 269 static LazyInstance<UnixEpochSingleton>::Leaky |
270 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER; | 270 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER; |
271 | 271 |
272 // Static | 272 // Static |
273 TimeTicks TimeTicks::UnixEpoch() { | 273 TimeTicks TimeTicks::UnixEpoch() { |
274 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); | 274 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); |
275 } | 275 } |
276 | 276 |
| 277 TimeTicks TimeTicks::SnappedToNextTick(TimeTicks tick_phase, |
| 278 TimeDelta tick_interval) const { |
| 279 // |interval_offset| is the offset from |this| to the next multiple of |
| 280 // |tick_interval| after |tick_phase|, possibly negative if in the past. |
| 281 TimeDelta interval_offset = TimeDelta::FromInternalValue( |
| 282 (tick_phase - *this).ToInternalValue() % tick_interval.ToInternalValue()); |
| 283 // If |this| is exactly on the interval (i.e. offset==0), don't adjust. |
| 284 // Otherwise, if |tick_phase| was in the past, adjust forward to the next |
| 285 // tick after |this|. |
| 286 if (interval_offset.ToInternalValue() != 0 && tick_phase < *this) { |
| 287 interval_offset += tick_interval; |
| 288 } |
| 289 |
| 290 return *this + interval_offset; |
| 291 } |
| 292 |
277 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) { | 293 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) { |
278 // This function formats a TimeTicks object as "bogo-microseconds". | 294 // This function formats a TimeTicks object as "bogo-microseconds". |
279 // The origin and granularity of the count are platform-specific, and may very | 295 // The origin and granularity of the count are platform-specific, and may very |
280 // from run to run. Although bogo-microseconds usually roughly correspond to | 296 // from run to run. Although bogo-microseconds usually roughly correspond to |
281 // real microseconds, the only real guarantee is that the number never goes | 297 // real microseconds, the only real guarantee is that the number never goes |
282 // down during a single run. | 298 // down during a single run. |
283 const TimeDelta as_time_delta = time_ticks - TimeTicks(); | 299 const TimeDelta as_time_delta = time_ticks - TimeTicks(); |
284 return os << as_time_delta.InMicroseconds() << " bogo-microseconds"; | 300 return os << as_time_delta.InMicroseconds() << " bogo-microseconds"; |
285 } | 301 } |
286 | 302 |
287 // Time::Exploded ------------------------------------------------------------- | 303 // Time::Exploded ------------------------------------------------------------- |
288 | 304 |
289 inline bool is_in_range(int value, int lo, int hi) { | 305 inline bool is_in_range(int value, int lo, int hi) { |
290 return lo <= value && value <= hi; | 306 return lo <= value && value <= hi; |
291 } | 307 } |
292 | 308 |
293 bool Time::Exploded::HasValidValues() const { | 309 bool Time::Exploded::HasValidValues() const { |
294 return is_in_range(month, 1, 12) && | 310 return is_in_range(month, 1, 12) && |
295 is_in_range(day_of_week, 0, 6) && | 311 is_in_range(day_of_week, 0, 6) && |
296 is_in_range(day_of_month, 1, 31) && | 312 is_in_range(day_of_month, 1, 31) && |
297 is_in_range(hour, 0, 23) && | 313 is_in_range(hour, 0, 23) && |
298 is_in_range(minute, 0, 59) && | 314 is_in_range(minute, 0, 59) && |
299 is_in_range(second, 0, 60) && | 315 is_in_range(second, 0, 60) && |
300 is_in_range(millisecond, 0, 999); | 316 is_in_range(millisecond, 0, 999); |
301 } | 317 } |
302 | 318 |
303 } // namespace base | 319 } // namespace base |
OLD | NEW |