| 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 <limits> | 8 #include <limits> |
| 8 #include <ostream> | 9 #include <ostream> |
| 10 #include <sstream> |
| 9 | 11 |
| 10 #include "base/float_util.h" | 12 #include "base/float_util.h" |
| 11 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/strings/stringprintf.h" |
| 13 #include "base/third_party/nspr/prtime.h" | 16 #include "base/third_party/nspr/prtime.h" |
| 14 | 17 |
| 15 namespace base { | 18 namespace base { |
| 16 | 19 |
| 17 // TimeDelta ------------------------------------------------------------------ | 20 // TimeDelta ------------------------------------------------------------------ |
| 18 | 21 |
| 19 // static | 22 // static |
| 20 TimeDelta TimeDelta::Max() { | 23 TimeDelta TimeDelta::Max() { |
| 21 return TimeDelta(std::numeric_limits<int64>::max()); | 24 return TimeDelta(std::numeric_limits<int64>::max()); |
| 22 } | 25 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 } | 90 } |
| 88 | 91 |
| 89 int64 TimeDelta::InMicroseconds() const { | 92 int64 TimeDelta::InMicroseconds() const { |
| 90 if (is_max()) { | 93 if (is_max()) { |
| 91 // Preserve max to prevent overflow. | 94 // Preserve max to prevent overflow. |
| 92 return std::numeric_limits<int64>::max(); | 95 return std::numeric_limits<int64>::max(); |
| 93 } | 96 } |
| 94 return delta_; | 97 return delta_; |
| 95 } | 98 } |
| 96 | 99 |
| 100 std::ostream& operator<<(std::ostream& os, TimeDelta time_delta) { |
| 101 return os << time_delta.InSecondsF() << "s"; |
| 102 } |
| 103 |
| 97 // Time ----------------------------------------------------------------------- | 104 // Time ----------------------------------------------------------------------- |
| 98 | 105 |
| 99 // static | 106 // static |
| 100 Time Time::Max() { | 107 Time Time::Max() { |
| 101 return Time(std::numeric_limits<int64>::max()); | 108 return Time(std::numeric_limits<int64>::max()); |
| 102 } | 109 } |
| 103 | 110 |
| 104 // static | 111 // static |
| 105 Time Time::FromTimeT(time_t tt) { | 112 Time Time::FromTimeT(time_t tt) { |
| 106 if (tt == 0) | 113 if (tt == 0) |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 is_local ? PR_FALSE : PR_TRUE, | 230 is_local ? PR_FALSE : PR_TRUE, |
| 224 &result_time); | 231 &result_time); |
| 225 if (PR_SUCCESS != result) | 232 if (PR_SUCCESS != result) |
| 226 return false; | 233 return false; |
| 227 | 234 |
| 228 result_time += kTimeTToMicrosecondsOffset; | 235 result_time += kTimeTToMicrosecondsOffset; |
| 229 *parsed_time = Time(result_time); | 236 *parsed_time = Time(result_time); |
| 230 return true; | 237 return true; |
| 231 } | 238 } |
| 232 | 239 |
| 240 std::ostream& operator<<(std::ostream& os, Time time) { |
| 241 Time::Exploded exploded; |
| 242 time.UTCExplode(&exploded); |
| 243 // Use StringPrintf because iostreams formatting is painful. |
| 244 return os << StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d UTC", |
| 245 exploded.year, |
| 246 exploded.month, |
| 247 exploded.day_of_month, |
| 248 exploded.hour, |
| 249 exploded.minute, |
| 250 exploded.second, |
| 251 exploded.millisecond); |
| 252 } |
| 253 |
| 233 // Local helper class to hold the conversion from Time to TickTime at the | 254 // Local helper class to hold the conversion from Time to TickTime at the |
| 234 // time of the Unix epoch. | 255 // time of the Unix epoch. |
| 235 class UnixEpochSingleton { | 256 class UnixEpochSingleton { |
| 236 public: | 257 public: |
| 237 UnixEpochSingleton() | 258 UnixEpochSingleton() |
| 238 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {} | 259 : unix_epoch_(TimeTicks::Now() - (Time::Now() - Time::UnixEpoch())) {} |
| 239 | 260 |
| 240 TimeTicks unix_epoch() const { return unix_epoch_; } | 261 TimeTicks unix_epoch() const { return unix_epoch_; } |
| 241 | 262 |
| 242 private: | 263 private: |
| 243 const TimeTicks unix_epoch_; | 264 const TimeTicks unix_epoch_; |
| 244 | 265 |
| 245 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton); | 266 DISALLOW_COPY_AND_ASSIGN(UnixEpochSingleton); |
| 246 }; | 267 }; |
| 247 | 268 |
| 248 static LazyInstance<UnixEpochSingleton>::Leaky | 269 static LazyInstance<UnixEpochSingleton>::Leaky |
| 249 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER; | 270 leaky_unix_epoch_singleton_instance = LAZY_INSTANCE_INITIALIZER; |
| 250 | 271 |
| 251 // Static | 272 // Static |
| 252 TimeTicks TimeTicks::UnixEpoch() { | 273 TimeTicks TimeTicks::UnixEpoch() { |
| 253 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); | 274 return leaky_unix_epoch_singleton_instance.Get().unix_epoch(); |
| 254 } | 275 } |
| 255 | 276 |
| 277 std::ostream& operator<<(std::ostream& os, TimeTicks time_ticks) { |
| 278 // This function formats a TimeTicks object as "bogo-microseconds". |
| 279 // 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 |
| 281 // real microseconds, the only real guarantee is that the number never goes |
| 282 // down during a single run. |
| 283 const TimeDelta as_time_delta = time_ticks - TimeTicks(); |
| 284 return os << as_time_delta.InMicroseconds() << " bogo-microseconds"; |
| 285 } |
| 286 |
| 256 // Time::Exploded ------------------------------------------------------------- | 287 // Time::Exploded ------------------------------------------------------------- |
| 257 | 288 |
| 258 inline bool is_in_range(int value, int lo, int hi) { | 289 inline bool is_in_range(int value, int lo, int hi) { |
| 259 return lo <= value && value <= hi; | 290 return lo <= value && value <= hi; |
| 260 } | 291 } |
| 261 | 292 |
| 262 bool Time::Exploded::HasValidValues() const { | 293 bool Time::Exploded::HasValidValues() const { |
| 263 return is_in_range(month, 1, 12) && | 294 return is_in_range(month, 1, 12) && |
| 264 is_in_range(day_of_week, 0, 6) && | 295 is_in_range(day_of_week, 0, 6) && |
| 265 is_in_range(day_of_month, 1, 31) && | 296 is_in_range(day_of_month, 1, 31) && |
| 266 is_in_range(hour, 0, 23) && | 297 is_in_range(hour, 0, 23) && |
| 267 is_in_range(minute, 0, 59) && | 298 is_in_range(minute, 0, 59) && |
| 268 is_in_range(second, 0, 60) && | 299 is_in_range(second, 0, 60) && |
| 269 is_in_range(millisecond, 0, 999); | 300 is_in_range(millisecond, 0, 999); |
| 270 } | 301 } |
| 271 | 302 |
| 272 } // namespace base | 303 } // namespace base |
| OLD | NEW |