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