michael@0: // Copyright (c) 2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/time.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/logging.h" michael@0: #include "base/scoped_cftyperef.h" michael@0: michael@0: namespace base { michael@0: michael@0: // The Time routines in this file use Mach and CoreFoundation APIs, since the michael@0: // POSIX definition of time_t in Mac OS X wraps around after 2038--and michael@0: // there are already cookie expiration dates, etc., past that time out in michael@0: // the field. Using CFDate prevents that problem, and using mach_absolute_time michael@0: // for TimeTicks gives us nice high-resolution interval timing. michael@0: michael@0: // Time ----------------------------------------------------------------------- michael@0: michael@0: // The internal representation of Time uses a 64-bit microsecond count michael@0: // from 1970-01-01 00:00:00 UTC. Core Foundation uses a double second count michael@0: // since 2001-01-01 00:00:00 UTC. michael@0: michael@0: // Some functions in time.cc use time_t directly, so we provide a zero offset michael@0: // for them. The epoch is 1970-01-01 00:00:00 UTC. michael@0: // static michael@0: const int64_t Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); michael@0: michael@0: // static michael@0: Time Time::Now() { michael@0: CFAbsoluteTime now = michael@0: CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; michael@0: return Time(static_cast(now * kMicrosecondsPerSecond)); michael@0: } michael@0: michael@0: // static michael@0: Time Time::NowFromSystemTime() { michael@0: // Just use Now() because Now() returns the system time. michael@0: return Now(); michael@0: } michael@0: michael@0: // static michael@0: Time Time::FromExploded(bool is_local, const Exploded& exploded) { michael@0: CFGregorianDate date; michael@0: date.second = exploded.second + michael@0: exploded.millisecond / static_cast(kMillisecondsPerSecond); michael@0: date.minute = exploded.minute; michael@0: date.hour = exploded.hour; michael@0: date.day = exploded.day_of_month; michael@0: date.month = exploded.month; michael@0: date.year = exploded.year; michael@0: michael@0: scoped_cftyperef michael@0: time_zone(is_local ? CFTimeZoneCopySystem() : NULL); michael@0: CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + michael@0: kCFAbsoluteTimeIntervalSince1970; michael@0: return Time(static_cast(seconds * kMicrosecondsPerSecond)); michael@0: } michael@0: michael@0: void Time::Explode(bool is_local, Exploded* exploded) const { michael@0: CFAbsoluteTime seconds = michael@0: (static_cast(us_) / kMicrosecondsPerSecond) - michael@0: kCFAbsoluteTimeIntervalSince1970; michael@0: michael@0: scoped_cftyperef michael@0: time_zone(is_local ? CFTimeZoneCopySystem() : NULL); michael@0: CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); michael@0: michael@0: exploded->year = date.year; michael@0: exploded->month = date.month; michael@0: exploded->day_of_month = date.day; michael@0: exploded->hour = date.hour; michael@0: exploded->minute = date.minute; michael@0: exploded->second = date.second; michael@0: exploded->millisecond = michael@0: static_cast(date.second * kMillisecondsPerSecond) % michael@0: kMillisecondsPerSecond; michael@0: } michael@0: michael@0: // TimeTicks ------------------------------------------------------------------ michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::Now() { michael@0: uint64_t absolute_micro; michael@0: michael@0: static mach_timebase_info_data_t timebase_info; michael@0: if (timebase_info.denom == 0) { michael@0: // Zero-initialization of statics guarantees that denom will be 0 before michael@0: // calling mach_timebase_info. mach_timebase_info will never set denom to michael@0: // 0 as that would be invalid, so the zero-check can be used to determine michael@0: // whether mach_timebase_info has already been called. This is michael@0: // recommended by Apple's QA1398. michael@0: kern_return_t kr = mach_timebase_info(&timebase_info); michael@0: DCHECK(kr == KERN_SUCCESS); michael@0: } michael@0: michael@0: // mach_absolute_time is it when it comes to ticks on the Mac. Other calls michael@0: // with less precision (such as TickCount) just call through to michael@0: // mach_absolute_time. michael@0: michael@0: // timebase_info converts absolute time tick units into nanoseconds. Convert michael@0: // to microseconds up front to stave off overflows. michael@0: absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * michael@0: timebase_info.numer / timebase_info.denom; michael@0: michael@0: // Don't bother with the rollover handling that the Windows version does. michael@0: // With numer and denom = 1 (the expected case), the 64-bit absolute time michael@0: // reported in nanoseconds is enough to last nearly 585 years. michael@0: michael@0: return TimeTicks(absolute_micro); michael@0: } michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::HighResNow() { michael@0: return Now(); michael@0: } michael@0: michael@0: } // namespace base