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

Side by Side Diff: base/time_mac.cc

Issue 9249: Update Mac implemention of Time to prevent problems with... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 12 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « base/base.xcodeproj/project.pbxproj ('k') | base/time_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Name: svn:eol-style
+ LF
OLDNEW
(Empty)
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.c 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 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>(now * kMicrosecondsPerSecond));
41 }
42
43 // static
44 Time Time::FromExploded(bool is_local, const Exploded& exploded) {
45 CFGregorianDate date;
46 date.second = exploded.second +
47 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond);
48 date.minute = exploded.minute;
49 date.hour = exploded.hour;
50 date.day = exploded.day_of_month;
51 date.month = exploded.month;
52 date.year = exploded.year;
53
54 scoped_cftyperef<CFTimeZoneRef> time_zone;
55 if (is_local)
56 time_zone.reset(CFTimeZoneCopySystem());
57 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) +
58 kCFAbsoluteTimeIntervalSince1970;
59 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond));
60 }
61
62 void Time::Explode(bool is_local, Exploded* exploded) const {
63 CFAbsoluteTime seconds =
64 (static_cast<double>(us_) / kMicrosecondsPerSecond) -
65 kCFAbsoluteTimeIntervalSince1970;
66
67 scoped_cftyperef<CFTimeZoneRef> time_zone;
68 if (is_local)
69 time_zone.reset(CFTimeZoneCopySystem());
70 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone);
71
72 exploded->year = date.year;
73 exploded->month = date.month;
74 exploded->day_of_month = date.day;
75 exploded->hour = date.hour;
76 exploded->minute = date.minute;
77 exploded->second = date.second;
78 exploded->millisecond =
79 static_cast<int>(date.second * kMillisecondsPerSecond) %
80 kMillisecondsPerSecond;
81 }
82
83 // TimeTicks ------------------------------------------------------------------
84
85 // static
86 TimeTicks TimeTicks::Now() {
87 uint64_t absolute_micro;
88
89 static mach_timebase_info_data_t timebase_info;
90 if (timebase_info.denom == 0) {
91 // Zero-initialization of statics guarantees that denom will be 0 before
92 // calling mach_timebase_info. mach_timebase_info will never set denom to
93 // 0 as that would be invalid, so the zero-check can be used to determine
94 // whether mach_timebase_info has already been called. This is
95 // recommended by Apple's QA1398.
96 kern_return_t kr = mach_timebase_info(&timebase_info);
97 DCHECK(kr == KERN_SUCCESS);
98 }
99
100 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
101 // with less precision (such as TickCount) just call through to
102 // mach_absolute_time.
103
104 // timebase_info converts absolute time tick units into nanoseconds. Convert
105 // to microseconds up front to stave off overflows.
106 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
107 timebase_info.numer / timebase_info.denom;
108
109 // Don't bother with the rollover handling that the Windows version does.
110 // With numer and denom = 1 (the expected case), the 64-bit absolute time
111 // reported in nanoseconds is enough to last nearly 585 years.
112
113 return TimeTicks(absolute_micro);
114 }
115
116 // static
117 TimeTicks TimeTicks::HighResNow() {
118 return Now();
119 }
120
121 } // namespace base
OLDNEW
« no previous file with comments | « base/base.xcodeproj/project.pbxproj ('k') | base/time_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698