|
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 |
|
3 // found in the LICENSE file. |
|
4 |
|
5 #include "base/time.h" |
|
6 |
|
7 #include <CoreFoundation/CFDate.h> |
|
8 #include <CoreFoundation/CFTimeZone.h> |
|
9 #include <mach/mach_time.h> |
|
10 #include <sys/time.h> |
|
11 #include <time.h> |
|
12 |
|
13 #include "base/basictypes.h" |
|
14 #include "base/logging.h" |
|
15 #include "base/scoped_cftyperef.h" |
|
16 |
|
17 namespace base { |
|
18 |
|
19 // The Time routines in this file use Mach and CoreFoundation APIs, since the |
|
20 // POSIX definition of time_t in Mac OS X wraps around after 2038--and |
|
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 |
|
23 // for TimeTicks gives us nice high-resolution interval timing. |
|
24 |
|
25 // Time ----------------------------------------------------------------------- |
|
26 |
|
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 |
|
29 // since 2001-01-01 00:00:00 UTC. |
|
30 |
|
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. |
|
33 // static |
|
34 const int64_t Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); |
|
35 |
|
36 // static |
|
37 Time Time::Now() { |
|
38 CFAbsoluteTime now = |
|
39 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; |
|
40 return Time(static_cast<int64_t>(now * kMicrosecondsPerSecond)); |
|
41 } |
|
42 |
|
43 // static |
|
44 Time Time::NowFromSystemTime() { |
|
45 // Just use Now() because Now() returns the system time. |
|
46 return Now(); |
|
47 } |
|
48 |
|
49 // static |
|
50 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
|
51 CFGregorianDate date; |
|
52 date.second = exploded.second + |
|
53 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); |
|
54 date.minute = exploded.minute; |
|
55 date.hour = exploded.hour; |
|
56 date.day = exploded.day_of_month; |
|
57 date.month = exploded.month; |
|
58 date.year = exploded.year; |
|
59 |
|
60 scoped_cftyperef<CFTimeZoneRef> |
|
61 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
|
62 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + |
|
63 kCFAbsoluteTimeIntervalSince1970; |
|
64 return Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond)); |
|
65 } |
|
66 |
|
67 void Time::Explode(bool is_local, Exploded* exploded) const { |
|
68 CFAbsoluteTime seconds = |
|
69 (static_cast<double>(us_) / kMicrosecondsPerSecond) - |
|
70 kCFAbsoluteTimeIntervalSince1970; |
|
71 |
|
72 scoped_cftyperef<CFTimeZoneRef> |
|
73 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
|
74 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); |
|
75 |
|
76 exploded->year = date.year; |
|
77 exploded->month = date.month; |
|
78 exploded->day_of_month = date.day; |
|
79 exploded->hour = date.hour; |
|
80 exploded->minute = date.minute; |
|
81 exploded->second = date.second; |
|
82 exploded->millisecond = |
|
83 static_cast<int>(date.second * kMillisecondsPerSecond) % |
|
84 kMillisecondsPerSecond; |
|
85 } |
|
86 |
|
87 // TimeTicks ------------------------------------------------------------------ |
|
88 |
|
89 // static |
|
90 TimeTicks TimeTicks::Now() { |
|
91 uint64_t absolute_micro; |
|
92 |
|
93 static mach_timebase_info_data_t timebase_info; |
|
94 if (timebase_info.denom == 0) { |
|
95 // Zero-initialization of statics guarantees that denom will be 0 before |
|
96 // calling mach_timebase_info. mach_timebase_info will never set denom to |
|
97 // 0 as that would be invalid, so the zero-check can be used to determine |
|
98 // whether mach_timebase_info has already been called. This is |
|
99 // recommended by Apple's QA1398. |
|
100 kern_return_t kr = mach_timebase_info(&timebase_info); |
|
101 DCHECK(kr == KERN_SUCCESS); |
|
102 } |
|
103 |
|
104 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls |
|
105 // with less precision (such as TickCount) just call through to |
|
106 // mach_absolute_time. |
|
107 |
|
108 // timebase_info converts absolute time tick units into nanoseconds. Convert |
|
109 // to microseconds up front to stave off overflows. |
|
110 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * |
|
111 timebase_info.numer / timebase_info.denom; |
|
112 |
|
113 // Don't bother with the rollover handling that the Windows version does. |
|
114 // With numer and denom = 1 (the expected case), the 64-bit absolute time |
|
115 // reported in nanoseconds is enough to last nearly 585 years. |
|
116 |
|
117 return TimeTicks(absolute_micro); |
|
118 } |
|
119 |
|
120 // static |
|
121 TimeTicks TimeTicks::HighResNow() { |
|
122 return Now(); |
|
123 } |
|
124 |
|
125 } // namespace base |