OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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.h" | 5 #include "base/time.h" |
6 | 6 |
7 #include <CoreFoundation/CFDate.h> | 7 #include <CoreFoundation/CFDate.h> |
8 #include <CoreFoundation/CFTimeZone.h> | 8 #include <CoreFoundation/CFTimeZone.h> |
9 #include <mach/mach_time.h> | 9 #include <mach/mach_time.h> |
10 #include <sys/time.h> | 10 #include <sys/time.h> |
(...skipping 10 matching lines...) Expand all Loading... |
21 // there are already cookie expiration dates, etc., past that time out in | 21 // there are already cookie expiration dates, etc., past that time out in |
22 // the field. Using CFDate prevents that problem, and using mach_absolute_time | 22 // the field. Using CFDate prevents that problem, and using mach_absolute_time |
23 // for TimeTicks gives us nice high-resolution interval timing. | 23 // for TimeTicks gives us nice high-resolution interval timing. |
24 | 24 |
25 // Time ----------------------------------------------------------------------- | 25 // Time ----------------------------------------------------------------------- |
26 | 26 |
27 // The internal representation of Time uses a 64-bit microsecond count | 27 // The internal representation of Time uses a 64-bit microsecond count |
28 // from 1970-01-01 00:00:00 UTC. Core Foundation uses a double second count | 28 // from 1970-01-01 00:00:00 UTC. Core Foundation uses a double second count |
29 // since 2001-01-01 00:00:00 UTC. | 29 // since 2001-01-01 00:00:00 UTC. |
30 | 30 |
31 // Some functions in time.c use time_t directly, so we provide a zero offset | 31 // Some functions in time.cc use time_t directly, so we provide a zero offset |
32 // for them. The epoch is 1970-01-01 00:00:00 UTC. | 32 // for them. The epoch is 1970-01-01 00:00:00 UTC. |
33 // static | 33 // static |
34 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); | 34 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); |
35 | 35 |
36 // static | 36 // static |
37 Time Time::Now() { | 37 Time Time::Now() { |
38 CFAbsoluteTime now = | 38 CFAbsoluteTime now = |
39 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; | 39 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; |
40 return Time(static_cast<int64>(now * kMicrosecondsPerSecond)); | 40 return Time(static_cast<int64>(now * kMicrosecondsPerSecond)); |
41 } | 41 } |
42 | 42 |
43 // static | 43 // static |
44 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 44 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
45 CFGregorianDate date; | 45 CFGregorianDate date; |
46 date.second = exploded.second + | 46 date.second = exploded.second + |
47 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); | 47 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); |
48 date.minute = exploded.minute; | 48 date.minute = exploded.minute; |
49 date.hour = exploded.hour; | 49 date.hour = exploded.hour; |
50 date.day = exploded.day_of_month; | 50 date.day = exploded.day_of_month; |
51 date.month = exploded.month; | 51 date.month = exploded.month; |
52 date.year = exploded.year; | 52 date.year = exploded.year; |
53 | 53 |
54 scoped_cftyperef<CFTimeZoneRef> time_zone; | 54 scoped_cftyperef<CFTimeZoneRef> |
55 if (is_local) | 55 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
56 time_zone.reset(CFTimeZoneCopySystem()); | |
57 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + | 56 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + |
58 kCFAbsoluteTimeIntervalSince1970; | 57 kCFAbsoluteTimeIntervalSince1970; |
59 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond)); | 58 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond)); |
60 } | 59 } |
61 | 60 |
62 void Time::Explode(bool is_local, Exploded* exploded) const { | 61 void Time::Explode(bool is_local, Exploded* exploded) const { |
63 CFAbsoluteTime seconds = | 62 CFAbsoluteTime seconds = |
64 (static_cast<double>(us_) / kMicrosecondsPerSecond) - | 63 (static_cast<double>(us_) / kMicrosecondsPerSecond) - |
65 kCFAbsoluteTimeIntervalSince1970; | 64 kCFAbsoluteTimeIntervalSince1970; |
66 | 65 |
67 scoped_cftyperef<CFTimeZoneRef> time_zone; | 66 scoped_cftyperef<CFTimeZoneRef> |
68 if (is_local) | 67 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
69 time_zone.reset(CFTimeZoneCopySystem()); | |
70 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); | 68 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); |
71 | 69 |
72 exploded->year = date.year; | 70 exploded->year = date.year; |
73 exploded->month = date.month; | 71 exploded->month = date.month; |
74 exploded->day_of_month = date.day; | 72 exploded->day_of_month = date.day; |
75 exploded->hour = date.hour; | 73 exploded->hour = date.hour; |
76 exploded->minute = date.minute; | 74 exploded->minute = date.minute; |
77 exploded->second = date.second; | 75 exploded->second = date.second; |
78 exploded->millisecond = | 76 exploded->millisecond = |
79 static_cast<int>(date.second * kMillisecondsPerSecond) % | 77 static_cast<int>(date.second * kMillisecondsPerSecond) % |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 110 |
113 return TimeTicks(absolute_micro); | 111 return TimeTicks(absolute_micro); |
114 } | 112 } |
115 | 113 |
116 // static | 114 // static |
117 TimeTicks TimeTicks::HighResNow() { | 115 TimeTicks TimeTicks::HighResNow() { |
118 return Now(); | 116 return Now(); |
119 } | 117 } |
120 | 118 |
121 } // namespace base | 119 } // namespace base |
OLD | NEW |