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

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

Issue 669083002: Add logging support for base::Time* types. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add missing <string> include. Created 6 years, 2 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_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 if it was a Time object.
rvargas (doing something else) 2014/10/23 02:36:38 Shouldn't we log TimeTicks as a TimeDelta instead?
Adam Rice 2014/10/23 03:42:02 TimeDeltas are normally logged to 4 digits of prec
rvargas (doing something else) 2014/10/23 19:30:43 But TimeTicks expressed as a random date doesn't s
Adam Rice 2014/10/24 04:05:39 In the "rounding error" and "monotonic clock faile
rvargas (doing something else) 2014/10/24 23:24:18 The second one sounds like a TimeTicks unit test n
Adam Rice 2014/10/28 05:43:12 Okay, I changed it to "bogo-microseconds". I see
279 const Time as_time =
280 Time::UnixEpoch() + (time_ticks - TimeTicks::UnixEpoch());
281 return os << as_time;
282 }
283
256 // Time::Exploded ------------------------------------------------------------- 284 // Time::Exploded -------------------------------------------------------------
257 285
258 inline bool is_in_range(int value, int lo, int hi) { 286 inline bool is_in_range(int value, int lo, int hi) {
259 return lo <= value && value <= hi; 287 return lo <= value && value <= hi;
260 } 288 }
261 289
262 bool Time::Exploded::HasValidValues() const { 290 bool Time::Exploded::HasValidValues() const {
263 return is_in_range(month, 1, 12) && 291 return is_in_range(month, 1, 12) &&
264 is_in_range(day_of_week, 0, 6) && 292 is_in_range(day_of_week, 0, 6) &&
265 is_in_range(day_of_month, 1, 31) && 293 is_in_range(day_of_month, 1, 31) &&
266 is_in_range(hour, 0, 23) && 294 is_in_range(hour, 0, 23) &&
267 is_in_range(minute, 0, 59) && 295 is_in_range(minute, 0, 59) &&
268 is_in_range(second, 0, 60) && 296 is_in_range(second, 0, 60) &&
269 is_in_range(millisecond, 0, 999); 297 is_in_range(millisecond, 0, 999);
270 } 298 }
271 299
272 } // namespace base 300 } // namespace base
OLDNEW
« no previous file with comments | « base/time/time.h ('k') | base/time/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698