OLD | NEW |
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 <CoreFoundation/CFDate.h> | 7 #include <CoreFoundation/CFDate.h> |
8 #include <CoreFoundation/CFTimeZone.h> | 8 #include <CoreFoundation/CFTimeZone.h> |
9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <mach/mach_time.h> | 10 #include <mach/mach_time.h> |
| 11 #include <stddef.h> |
11 #include <stdint.h> | 12 #include <stdint.h> |
12 #include <sys/sysctl.h> | 13 #include <sys/sysctl.h> |
13 #include <sys/time.h> | 14 #include <sys/time.h> |
14 #include <sys/types.h> | 15 #include <sys/types.h> |
15 #include <time.h> | 16 #include <time.h> |
16 | 17 |
17 #include "base/basictypes.h" | |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/mac/mach_logging.h" | 19 #include "base/mac/mach_logging.h" |
20 #include "base/mac/scoped_cftyperef.h" | 20 #include "base/mac/scoped_cftyperef.h" |
21 #include "base/mac/scoped_mach_port.h" | 21 #include "base/mac/scoped_mach_port.h" |
| 22 #include "base/macros.h" |
22 #include "base/numerics/safe_conversions.h" | 23 #include "base/numerics/safe_conversions.h" |
| 24 #include "build/build_config.h" |
23 | 25 |
24 namespace { | 26 namespace { |
25 | 27 |
26 int64_t ComputeCurrentTicks() { | 28 int64_t ComputeCurrentTicks() { |
27 #if defined(OS_IOS) | 29 #if defined(OS_IOS) |
28 // On iOS mach_absolute_time stops while the device is sleeping. Instead use | 30 // 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 | 31 // 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 | 32 // changes. KERN_BOOTTIME will be updated by the system whenever the system |
31 // clock change. | 33 // clock change. |
32 struct timeval boottime; | 34 struct timeval boottime; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 // Time ----------------------------------------------------------------------- | 112 // Time ----------------------------------------------------------------------- |
111 | 113 |
112 // Core Foundation uses a double second count since 2001-01-01 00:00:00 UTC. | 114 // Core Foundation uses a double second count since 2001-01-01 00:00:00 UTC. |
113 // The UNIX epoch is 1970-01-01 00:00:00 UTC. | 115 // The UNIX epoch is 1970-01-01 00:00:00 UTC. |
114 // Windows uses a Gregorian epoch of 1601. We need to match this internally | 116 // Windows uses a Gregorian epoch of 1601. We need to match this internally |
115 // so that our time representations match across all platforms. See bug 14734. | 117 // so that our time representations match across all platforms. See bug 14734. |
116 // irb(main):010:0> Time.at(0).getutc() | 118 // irb(main):010:0> Time.at(0).getutc() |
117 // => Thu Jan 01 00:00:00 UTC 1970 | 119 // => Thu Jan 01 00:00:00 UTC 1970 |
118 // irb(main):011:0> Time.at(-11644473600).getutc() | 120 // irb(main):011:0> Time.at(-11644473600).getutc() |
119 // => Mon Jan 01 00:00:00 UTC 1601 | 121 // => Mon Jan 01 00:00:00 UTC 1601 |
120 static const int64 kWindowsEpochDeltaSeconds = INT64_C(11644473600); | 122 static const int64_t kWindowsEpochDeltaSeconds = INT64_C(11644473600); |
121 | 123 |
122 // static | 124 // static |
123 const int64 Time::kWindowsEpochDeltaMicroseconds = | 125 const int64_t Time::kWindowsEpochDeltaMicroseconds = |
124 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; | 126 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
125 | 127 |
126 // Some functions in time.cc use time_t directly, so we provide an offset | 128 // Some functions in time.cc use time_t directly, so we provide an offset |
127 // to convert from time_t (Unix epoch) and internal (Windows epoch). | 129 // to convert from time_t (Unix epoch) and internal (Windows epoch). |
128 // static | 130 // static |
129 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; | 131 const int64_t Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
130 | 132 |
131 // static | 133 // static |
132 Time Time::Now() { | 134 Time Time::Now() { |
133 return FromCFAbsoluteTime(CFAbsoluteTimeGetCurrent()); | 135 return FromCFAbsoluteTime(CFAbsoluteTimeGetCurrent()); |
134 } | 136 } |
135 | 137 |
136 // static | 138 // static |
137 Time Time::FromCFAbsoluteTime(CFAbsoluteTime t) { | 139 Time Time::FromCFAbsoluteTime(CFAbsoluteTime t) { |
138 static_assert(std::numeric_limits<CFAbsoluteTime>::has_infinity, | 140 static_assert(std::numeric_limits<CFAbsoluteTime>::has_infinity, |
139 "CFAbsoluteTime must have an infinity value"); | 141 "CFAbsoluteTime must have an infinity value"); |
140 if (t == 0) | 142 if (t == 0) |
141 return Time(); // Consider 0 as a null Time. | 143 return Time(); // Consider 0 as a null Time. |
142 if (t == std::numeric_limits<CFAbsoluteTime>::infinity()) | 144 if (t == std::numeric_limits<CFAbsoluteTime>::infinity()) |
143 return Max(); | 145 return Max(); |
144 return Time(static_cast<int64>( | 146 return Time(static_cast<int64_t>((t + kCFAbsoluteTimeIntervalSince1970) * |
145 (t + kCFAbsoluteTimeIntervalSince1970) * kMicrosecondsPerSecond) + | 147 kMicrosecondsPerSecond) + |
146 kWindowsEpochDeltaMicroseconds); | 148 kWindowsEpochDeltaMicroseconds); |
147 } | 149 } |
148 | 150 |
149 CFAbsoluteTime Time::ToCFAbsoluteTime() const { | 151 CFAbsoluteTime Time::ToCFAbsoluteTime() const { |
150 static_assert(std::numeric_limits<CFAbsoluteTime>::has_infinity, | 152 static_assert(std::numeric_limits<CFAbsoluteTime>::has_infinity, |
151 "CFAbsoluteTime must have an infinity value"); | 153 "CFAbsoluteTime must have an infinity value"); |
152 if (is_null()) | 154 if (is_null()) |
153 return 0; // Consider 0 as a null Time. | 155 return 0; // Consider 0 as a null Time. |
154 if (is_max()) | 156 if (is_max()) |
155 return std::numeric_limits<CFAbsoluteTime>::infinity(); | 157 return std::numeric_limits<CFAbsoluteTime>::infinity(); |
156 return (static_cast<CFAbsoluteTime>(us_ - kWindowsEpochDeltaMicroseconds) / | 158 return (static_cast<CFAbsoluteTime>(us_ - kWindowsEpochDeltaMicroseconds) / |
(...skipping 14 matching lines...) Expand all Loading... |
171 date.minute = exploded.minute; | 173 date.minute = exploded.minute; |
172 date.hour = exploded.hour; | 174 date.hour = exploded.hour; |
173 date.day = exploded.day_of_month; | 175 date.day = exploded.day_of_month; |
174 date.month = exploded.month; | 176 date.month = exploded.month; |
175 date.year = exploded.year; | 177 date.year = exploded.year; |
176 | 178 |
177 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | 179 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
178 is_local ? CFTimeZoneCopySystem() : NULL); | 180 is_local ? CFTimeZoneCopySystem() : NULL); |
179 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + | 181 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + |
180 kCFAbsoluteTimeIntervalSince1970; | 182 kCFAbsoluteTimeIntervalSince1970; |
181 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + | 183 return Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + |
182 kWindowsEpochDeltaMicroseconds); | 184 kWindowsEpochDeltaMicroseconds); |
183 } | 185 } |
184 | 186 |
185 void Time::Explode(bool is_local, Exploded* exploded) const { | 187 void Time::Explode(bool is_local, Exploded* exploded) const { |
186 // Avoid rounding issues, by only putting the integral number of seconds | 188 // Avoid rounding issues, by only putting the integral number of seconds |
187 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). | 189 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
188 int64 microsecond = us_ % kMicrosecondsPerSecond; | 190 int64_t microsecond = us_ % kMicrosecondsPerSecond; |
189 if (microsecond < 0) | 191 if (microsecond < 0) |
190 microsecond += kMicrosecondsPerSecond; | 192 microsecond += kMicrosecondsPerSecond; |
191 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - | 193 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - |
192 kWindowsEpochDeltaSeconds - | 194 kWindowsEpochDeltaSeconds - |
193 kCFAbsoluteTimeIntervalSince1970; | 195 kCFAbsoluteTimeIntervalSince1970; |
194 | 196 |
195 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | 197 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
196 is_local ? CFTimeZoneCopySystem() : NULL); | 198 is_local ? CFTimeZoneCopySystem() : NULL); |
197 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); | 199 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); |
198 // 1 = Monday, ..., 7 = Sunday. | 200 // 1 = Monday, ..., 7 = Sunday. |
(...skipping 26 matching lines...) Expand all Loading... |
225 bool TimeTicks::IsHighResolution() { | 227 bool TimeTicks::IsHighResolution() { |
226 return true; | 228 return true; |
227 } | 229 } |
228 | 230 |
229 // static | 231 // static |
230 ThreadTicks ThreadTicks::Now() { | 232 ThreadTicks ThreadTicks::Now() { |
231 return ThreadTicks(ComputeThreadTicks()); | 233 return ThreadTicks(ComputeThreadTicks()); |
232 } | 234 } |
233 | 235 |
234 } // namespace base | 236 } // namespace base |
OLD | NEW |