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

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

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 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.cc ('k') | base/time/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')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/time/time.h"
6
7 #include <CoreFoundation/CFDate.h>
8 #include <CoreFoundation/CFTimeZone.h>
9 #include <mach/mach.h>
10 #include <mach/mach_time.h>
11 #include <stdint.h>
12 #include <sys/sysctl.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <time.h>
16
17 #include "base/basictypes.h"
18 #include "base/logging.h"
19 #include "base/mac/mach_logging.h"
20 #include "base/mac/scoped_cftyperef.h"
21 #include "base/mac/scoped_mach_port.h"
22 #include "base/numerics/safe_conversions.h"
23
24 namespace {
25
26 int64_t ComputeCurrentTicks() {
27 #if defined(OS_IOS)
28 // On iOS mach_absolute_time stops while the device is sleeping. Instead use
29 // now - KERN_BOOTTIME to get a time difference that is not impacted by clock
30 // changes. KERN_BOOTTIME will be updated by the system whenever the system
31 // clock change.
32 struct timeval boottime;
33 int mib[2] = {CTL_KERN, KERN_BOOTTIME};
34 size_t size = sizeof(boottime);
35 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0);
36 DCHECK_EQ(KERN_SUCCESS, kr);
37 base::TimeDelta time_difference = base::Time::Now() -
38 (base::Time::FromTimeT(boottime.tv_sec) +
39 base::TimeDelta::FromMicroseconds(boottime.tv_usec));
40 return time_difference.InMicroseconds();
41 #else
42 static mach_timebase_info_data_t timebase_info;
43 if (timebase_info.denom == 0) {
44 // Zero-initialization of statics guarantees that denom will be 0 before
45 // calling mach_timebase_info. mach_timebase_info will never set denom to
46 // 0 as that would be invalid, so the zero-check can be used to determine
47 // whether mach_timebase_info has already been called. This is
48 // recommended by Apple's QA1398.
49 kern_return_t kr = mach_timebase_info(&timebase_info);
50 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info";
51 }
52
53 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
54 // with less precision (such as TickCount) just call through to
55 // mach_absolute_time.
56
57 // timebase_info converts absolute time tick units into nanoseconds. Convert
58 // to microseconds up front to stave off overflows.
59 base::CheckedNumeric<uint64_t> result(
60 mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond);
61 result *= timebase_info.numer;
62 result /= timebase_info.denom;
63
64 // Don't bother with the rollover handling that the Windows version does.
65 // With numer and denom = 1 (the expected case), the 64-bit absolute time
66 // reported in nanoseconds is enough to last nearly 585 years.
67 return base::checked_cast<int64_t>(result.ValueOrDie());
68 #endif // defined(OS_IOS)
69 }
70
71 int64_t ComputeThreadTicks() {
72 base::mac::ScopedMachSendRight thread(mach_thread_self());
73 mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT;
74 thread_basic_info_data_t thread_info_data;
75
76 if (thread.get() == MACH_PORT_NULL) {
77 DLOG(ERROR) << "Failed to get mach_thread_self()";
78 return 0;
79 }
80
81 kern_return_t kr = thread_info(
82 thread,
83 THREAD_BASIC_INFO,
84 reinterpret_cast<thread_info_t>(&thread_info_data),
85 &thread_info_count);
86 MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info";
87
88 base::CheckedNumeric<int64_t> absolute_micros(
89 thread_info_data.user_time.seconds);
90 absolute_micros *= base::Time::kMicrosecondsPerSecond;
91 absolute_micros += thread_info_data.user_time.microseconds;
92 return absolute_micros.ValueOrDie();
93 }
94
95 } // namespace
96
97 namespace base {
98
99 // The Time routines in this file use Mach and CoreFoundation APIs, since the
100 // POSIX definition of time_t in Mac OS X wraps around after 2038--and
101 // there are already cookie expiration dates, etc., past that time out in
102 // the field. Using CFDate prevents that problem, and using mach_absolute_time
103 // for TimeTicks gives us nice high-resolution interval timing.
104
105 // Time -----------------------------------------------------------------------
106
107 // Core Foundation uses a double second count since 2001-01-01 00:00:00 UTC.
108 // The UNIX epoch is 1970-01-01 00:00:00 UTC.
109 // Windows uses a Gregorian epoch of 1601. We need to match this internally
110 // so that our time representations match across all platforms. See bug 14734.
111 // irb(main):010:0> Time.at(0).getutc()
112 // => Thu Jan 01 00:00:00 UTC 1970
113 // irb(main):011:0> Time.at(-11644473600).getutc()
114 // => Mon Jan 01 00:00:00 UTC 1601
115 static const int64 kWindowsEpochDeltaSeconds = INT64_C(11644473600);
116
117 // static
118 const int64 Time::kWindowsEpochDeltaMicroseconds =
119 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond;
120
121 // Some functions in time.cc use time_t directly, so we provide an offset
122 // to convert from time_t (Unix epoch) and internal (Windows epoch).
123 // static
124 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds;
125
126 // static
127 Time Time::Now() {
128 return FromCFAbsoluteTime(CFAbsoluteTimeGetCurrent());
129 }
130
131 // static
132 Time Time::FromCFAbsoluteTime(CFAbsoluteTime t) {
133 COMPILE_ASSERT(std::numeric_limits<CFAbsoluteTime>::has_infinity,
134 numeric_limits_infinity_is_undefined_when_not_has_infinity);
135 if (t == 0)
136 return Time(); // Consider 0 as a null Time.
137 if (t == std::numeric_limits<CFAbsoluteTime>::infinity())
138 return Max();
139 return Time(static_cast<int64>(
140 (t + kCFAbsoluteTimeIntervalSince1970) * kMicrosecondsPerSecond) +
141 kWindowsEpochDeltaMicroseconds);
142 }
143
144 CFAbsoluteTime Time::ToCFAbsoluteTime() const {
145 COMPILE_ASSERT(std::numeric_limits<CFAbsoluteTime>::has_infinity,
146 numeric_limits_infinity_is_undefined_when_not_has_infinity);
147 if (is_null())
148 return 0; // Consider 0 as a null Time.
149 if (is_max())
150 return std::numeric_limits<CFAbsoluteTime>::infinity();
151 return (static_cast<CFAbsoluteTime>(us_ - kWindowsEpochDeltaMicroseconds) /
152 kMicrosecondsPerSecond) - kCFAbsoluteTimeIntervalSince1970;
153 }
154
155 // static
156 Time Time::NowFromSystemTime() {
157 // Just use Now() because Now() returns the system time.
158 return Now();
159 }
160
161 // static
162 Time Time::FromExploded(bool is_local, const Exploded& exploded) {
163 CFGregorianDate date;
164 date.second = exploded.second +
165 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond);
166 date.minute = exploded.minute;
167 date.hour = exploded.hour;
168 date.day = exploded.day_of_month;
169 date.month = exploded.month;
170 date.year = exploded.year;
171
172 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone(
173 is_local ? CFTimeZoneCopySystem() : NULL);
174 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) +
175 kCFAbsoluteTimeIntervalSince1970;
176 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) +
177 kWindowsEpochDeltaMicroseconds);
178 }
179
180 void Time::Explode(bool is_local, Exploded* exploded) const {
181 // Avoid rounding issues, by only putting the integral number of seconds
182 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|).
183 int64 microsecond = us_ % kMicrosecondsPerSecond;
184 if (microsecond < 0)
185 microsecond += kMicrosecondsPerSecond;
186 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) -
187 kWindowsEpochDeltaSeconds -
188 kCFAbsoluteTimeIntervalSince1970;
189
190 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone(
191 is_local ? CFTimeZoneCopySystem() : NULL);
192 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone);
193 // 1 = Monday, ..., 7 = Sunday.
194 int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone);
195
196 exploded->year = date.year;
197 exploded->month = date.month;
198 exploded->day_of_week = cf_day_of_week % 7;
199 exploded->day_of_month = date.day;
200 exploded->hour = date.hour;
201 exploded->minute = date.minute;
202 // Make sure seconds are rounded down towards -infinity.
203 exploded->second = floor(date.second);
204 // Calculate milliseconds ourselves, since we rounded the |seconds|, making
205 // sure to round towards -infinity.
206 exploded->millisecond =
207 (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond :
208 (microsecond - kMicrosecondsPerMillisecond + 1) /
209 kMicrosecondsPerMillisecond;
210 }
211
212 // TimeTicks ------------------------------------------------------------------
213
214 // static
215 TimeTicks TimeTicks::Now() {
216 return TimeTicks(ComputeCurrentTicks());
217 }
218
219 // static
220 bool TimeTicks::IsHighResolution() {
221 return true;
222 }
223
224 // static
225 ThreadTicks ThreadTicks::Now() {
226 return ThreadTicks(ComputeThreadTicks());
227 }
228
229 // static
230 TraceTicks TraceTicks::Now() {
231 return TraceTicks(ComputeCurrentTicks());
232 }
233
234 } // namespace base
OLDNEW
« no previous file with comments | « base/time/time.cc ('k') | base/time/time_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698