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> |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 int64_t ComputeCurrentTicks() { | 28 int64_t ComputeCurrentTicks() { |
29 #if defined(OS_IOS) | 29 #if defined(OS_IOS) |
30 // 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 |
31 // 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 |
32 // 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 |
33 // clock change. | 33 // clock change. |
34 struct timeval boottime; | 34 struct timeval boottime; |
35 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; | 35 int mib[2] = {CTL_KERN, KERN_BOOTTIME}; |
36 size_t size = sizeof(boottime); | 36 size_t size = sizeof(boottime); |
37 int kr = sysctl(mib, arraysize(mib), &boottime, &size, nullptr, 0); | 37 int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0); |
38 DCHECK_EQ(KERN_SUCCESS, kr); | 38 DCHECK_EQ(KERN_SUCCESS, kr); |
39 base::TimeDelta time_difference = base::Time::Now() - | 39 base::TimeDelta time_difference = base::Time::Now() - |
40 (base::Time::FromTimeT(boottime.tv_sec) + | 40 (base::Time::FromTimeT(boottime.tv_sec) + |
41 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); | 41 base::TimeDelta::FromMicroseconds(boottime.tv_usec)); |
42 return time_difference.InMicroseconds(); | 42 return time_difference.InMicroseconds(); |
43 #else | 43 #else |
44 static mach_timebase_info_data_t timebase_info; | 44 static mach_timebase_info_data_t timebase_info; |
45 if (timebase_info.denom == 0) { | 45 if (timebase_info.denom == 0) { |
46 // Zero-initialization of statics guarantees that denom will be 0 before | 46 // Zero-initialization of statics guarantees that denom will be 0 before |
47 // calling mach_timebase_info. mach_timebase_info will never set denom to | 47 // calling mach_timebase_info. mach_timebase_info will never set denom to |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 kMicrosecondsPerSecond) - kCFAbsoluteTimeIntervalSince1970; | 161 kMicrosecondsPerSecond) - kCFAbsoluteTimeIntervalSince1970; |
162 } | 162 } |
163 | 163 |
164 // static | 164 // static |
165 Time Time::NowFromSystemTime() { | 165 Time Time::NowFromSystemTime() { |
166 // Just use Now() because Now() returns the system time. | 166 // Just use Now() because Now() returns the system time. |
167 return Now(); | 167 return Now(); |
168 } | 168 } |
169 | 169 |
170 // static | 170 // static |
171 bool Time::FromExploded(bool is_local, const Exploded& exploded, Time* time) { | 171 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
172 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( | 172 base::ScopedCFTypeRef<CFTimeZoneRef> time_zone( |
173 is_local | 173 is_local |
174 ? CFTimeZoneCopySystem() | 174 ? CFTimeZoneCopySystem() |
175 : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); | 175 : CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, 0)); |
176 base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier( | 176 base::ScopedCFTypeRef<CFCalendarRef> gregorian(CFCalendarCreateWithIdentifier( |
177 kCFAllocatorDefault, kCFGregorianCalendar)); | 177 kCFAllocatorDefault, kCFGregorianCalendar)); |
178 CFCalendarSetTimeZone(gregorian, time_zone); | 178 CFCalendarSetTimeZone(gregorian, time_zone); |
179 CFAbsoluteTime absolute_time; | 179 CFAbsoluteTime absolute_time; |
180 // 'S' is not defined in componentDesc in Apple documentation, but can be | 180 // 'S' is not defined in componentDesc in Apple documentation, but can be |
181 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c | 181 // found at http://www.opensource.apple.com/source/CF/CF-855.17/CFCalendar.c |
182 CFCalendarComposeAbsoluteTime( | 182 CFCalendarComposeAbsoluteTime( |
183 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, | 183 gregorian, &absolute_time, "yMdHmsS", exploded.year, exploded.month, |
184 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, | 184 exploded.day_of_month, exploded.hour, exploded.minute, exploded.second, |
185 exploded.millisecond); | 185 exploded.millisecond); |
186 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; | 186 CFAbsoluteTime seconds = absolute_time + kCFAbsoluteTimeIntervalSince1970; |
187 | 187 return Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + |
188 base::Time converted_time = | 188 kWindowsEpochDeltaMicroseconds); |
189 Time(static_cast<int64_t>(seconds * kMicrosecondsPerSecond) + | |
190 kWindowsEpochDeltaMicroseconds); | |
191 | |
192 // If |exploded.day_of_month| is set to 31 | |
193 // on a 28-30 day month, it will return the first day of the next month. | |
194 // Thus round-trip the time and compare the initial |exploded| with | |
195 // |utc_to_exploded| time. | |
196 base::Time::Exploded to_exploded; | |
197 if (!is_local) | |
198 converted_time.UTCExplode(&to_exploded); | |
199 else | |
200 converted_time.LocalExplode(&to_exploded); | |
201 | |
202 if (ExplodedMostlyEquals(to_exploded, exploded)) { | |
203 *time = converted_time; | |
204 return true; | |
205 } | |
206 | |
207 *time = Time(0); | |
208 return false; | |
209 } | 189 } |
210 | 190 |
211 void Time::Explode(bool is_local, Exploded* exploded) const { | 191 void Time::Explode(bool is_local, Exploded* exploded) const { |
212 // Avoid rounding issues, by only putting the integral number of seconds | 192 // Avoid rounding issues, by only putting the integral number of seconds |
213 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). | 193 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|). |
214 int64_t microsecond = us_ % kMicrosecondsPerSecond; | 194 int64_t microsecond = us_ % kMicrosecondsPerSecond; |
215 if (microsecond < 0) | 195 if (microsecond < 0) |
216 microsecond += kMicrosecondsPerSecond; | 196 microsecond += kMicrosecondsPerSecond; |
217 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - | 197 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) - |
218 kWindowsEpochDeltaSeconds - | 198 kWindowsEpochDeltaSeconds - |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 return Clock::MAC_MACH_ABSOLUTE_TIME; | 246 return Clock::MAC_MACH_ABSOLUTE_TIME; |
267 #endif // defined(OS_IOS) | 247 #endif // defined(OS_IOS) |
268 } | 248 } |
269 | 249 |
270 // static | 250 // static |
271 ThreadTicks ThreadTicks::Now() { | 251 ThreadTicks ThreadTicks::Now() { |
272 return ThreadTicks(ComputeThreadTicks()); | 252 return ThreadTicks(ComputeThreadTicks()); |
273 } | 253 } |
274 | 254 |
275 } // namespace base | 255 } // namespace base |
OLD | NEW |