diff options
author | amanda@chromium.org <amanda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-03 19:36:59 +0000 |
---|---|---|
committer | amanda@chromium.org <amanda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-03 19:36:59 +0000 |
commit | 8822d9730d5f30a5863d6fa5da36d967b25b8c68 (patch) | |
tree | 173c012ad6152774e807bc5648d51c3925ad1b37 /base | |
parent | ec565e6000dc1304dd92f4c5c9c0bd525fc194ea (diff) | |
download | chromium_src-8822d9730d5f30a5863d6fa5da36d967b25b8c68.zip chromium_src-8822d9730d5f30a5863d6fa5da36d967b25b8c68.tar.gz chromium_src-8822d9730d5f30a5863d6fa5da36d967b25b8c68.tar.bz2 |
Update Mac implemention of Time to prevent problems with
times later than the UNIX epoch 32 bit rollover in 2038 (such as
cookie expirations). time_t is only 32 bits in MacOS X, so
we can't just use time_posix.cc
Review URL: http://codereview.chromium.org/9249
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4473 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.xcodeproj/project.pbxproj | 8 | ||||
-rw-r--r-- | base/time_mac.cc | 121 | ||||
-rw-r--r-- | base/time_posix.cc | 31 |
3 files changed, 126 insertions, 34 deletions
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj index 3e8ed71..02ad377 100644 --- a/base/base.xcodeproj/project.pbxproj +++ b/base/base.xcodeproj/project.pbxproj @@ -117,7 +117,7 @@ 824653450DC1230B007C2BAA /* lock.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403250D92D2090006B936 /* lock.cc */; }; 824653680DC12CEC007C2BAA /* condition_variable_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 824653670DC12CEC007C2BAA /* condition_variable_posix.cc */; }; 824653740DC12D0E007C2BAA /* shared_memory_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 824653730DC12D0E007C2BAA /* shared_memory_posix.cc */; }; - 824654530DC25633007C2BAA /* time_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BEB81490D9B0F33009BA8DD /* time_posix.cc */; }; + 824654530DC25633007C2BAA /* time_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BEB81490D9B0F33009BA8DD /* time_mac.cc */; }; 8246548C0DC259DB007C2BAA /* revocable_store.cc in Sources */ = {isa = PBXBuildFile; fileRef = 8254035E0D92D27C0006B936 /* revocable_store.cc */; }; 824654910DC25A8C007C2BAA /* time.cc in Sources */ = {isa = PBXBuildFile; fileRef = 824654900DC25A8C007C2BAA /* time.cc */; }; 824654A60DC25CD7007C2BAA /* pickle.cc in Sources */ = {isa = PBXBuildFile; fileRef = 8254033E0D92D2210006B936 /* pickle.cc */; }; @@ -439,7 +439,7 @@ 7BD8F7730E65E89800034DE9 /* string16.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = string16.cc; sourceTree = "<group>"; }; 7BD9E84E0DA447F800FC7A01 /* singleton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = singleton.h; sourceTree = "<group>"; }; 7BEB81100D9AD288009BA8DD /* prcpucfg_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = prcpucfg_mac.h; path = third_party/nspr/prcpucfg_mac.h; sourceTree = "<group>"; }; - 7BEB81490D9B0F33009BA8DD /* time_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = time_posix.cc; sourceTree = "<group>"; }; + 7BEB81490D9B0F33009BA8DD /* time_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = time_mac.cc; sourceTree = "<group>"; }; 7BED30C60E59F63000A747DB /* executable.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = executable.xcconfig; sourceTree = "<group>"; }; 7BED30C70E59F63000A747DB /* staticlib.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = staticlib.xcconfig; sourceTree = "<group>"; }; 7BEFC29C0D99832D000829AD /* lock_impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lock_impl.h; sourceTree = "<group>"; }; @@ -1031,7 +1031,7 @@ 825403840D92D2CF0006B936 /* time.h */, 82D094540E5B892600FEC05C /* time_format.cc */, 82D094530E5B892600FEC05C /* time_format.h */, - 7BEB81490D9B0F33009BA8DD /* time_posix.cc */, + 7BEB81490D9B0F33009BA8DD /* time_mac.cc */, E4AFA4EF0E50F7B000201347 /* time_unittest.cc */, 825403850D92D2CF0006B936 /* timer.cc */, 825403860D92D2CF0006B936 /* timer.h */, @@ -1413,7 +1413,7 @@ 829E36730DC0FBAD00819EBF /* thread_local_storage_posix.cc in Sources */, 824654910DC25A8C007C2BAA /* time.cc in Sources */, 82D094550E5B892600FEC05C /* time_format.cc in Sources */, - 824654530DC25633007C2BAA /* time_posix.cc in Sources */, + 824654530DC25633007C2BAA /* time_mac.cc in Sources */, E49357220E422A36008F8B09 /* timer.cc in Sources */, 7BF882800E71929B000BAF8A /* trace_event.cc in Sources */, 820EB5020E3A618B009668FC /* tracked.cc in Sources */, diff --git a/base/time_mac.cc b/base/time_mac.cc new file mode 100644 index 0000000..d71825c --- /dev/null +++ b/base/time_mac.cc @@ -0,0 +1,121 @@ +// Copyright (c) 2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/time.h" + +#include <CoreFoundation/CFDate.h> +#include <CoreFoundation/CFTimeZone.h> +#include <mach/mach_time.h> +#include <sys/time.h> +#include <time.h> + +#include "base/basictypes.h" +#include "base/logging.h" +#include "base/scoped_cftyperef.h" + +namespace base { + +// The Time routines in this file use Mach and CoreFoundation APIs, since the +// POSIX definition of time_t in Mac OS X wraps around after 2038--and +// there are already cookie expiration dates, etc., past that time out in +// the field. Using CFDate prevents that problem, and using mach_absolute_time +// for TimeTicks gives us nice high-resolution interval timing. + +// Time ----------------------------------------------------------------------- + +// The internal representation of Time uses a 64-bit microsecond count +// from 1970-01-01 00:00:00 UTC. Core Foundation uses a double second count +// since 2001-01-01 00:00:00 UTC. + +// Some functions in time.c use time_t directly, so we provide a zero offset +// for them. The epoch is 1970-01-01 00:00:00 UTC. +// static +const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); + +// static +Time Time::Now() { + CFAbsoluteTime now = + CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; + return Time(static_cast<int64>(now * kMicrosecondsPerSecond)); +} + +// static +Time Time::FromExploded(bool is_local, const Exploded& exploded) { + CFGregorianDate date; + date.second = exploded.second + + exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); + date.minute = exploded.minute; + date.hour = exploded.hour; + date.day = exploded.day_of_month; + date.month = exploded.month; + date.year = exploded.year; + + scoped_cftyperef<CFTimeZoneRef> time_zone; + if (is_local) + time_zone.reset(CFTimeZoneCopySystem()); + CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + + kCFAbsoluteTimeIntervalSince1970; + return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond)); +} + +void Time::Explode(bool is_local, Exploded* exploded) const { + CFAbsoluteTime seconds = + (static_cast<double>(us_) / kMicrosecondsPerSecond) - + kCFAbsoluteTimeIntervalSince1970; + + scoped_cftyperef<CFTimeZoneRef> time_zone; + if (is_local) + time_zone.reset(CFTimeZoneCopySystem()); + CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); + + exploded->year = date.year; + exploded->month = date.month; + exploded->day_of_month = date.day; + exploded->hour = date.hour; + exploded->minute = date.minute; + exploded->second = date.second; + exploded->millisecond = + static_cast<int>(date.second * kMillisecondsPerSecond) % + kMillisecondsPerSecond; +} + +// TimeTicks ------------------------------------------------------------------ + +// static +TimeTicks TimeTicks::Now() { + uint64_t absolute_micro; + + static mach_timebase_info_data_t timebase_info; + if (timebase_info.denom == 0) { + // Zero-initialization of statics guarantees that denom will be 0 before + // calling mach_timebase_info. mach_timebase_info will never set denom to + // 0 as that would be invalid, so the zero-check can be used to determine + // whether mach_timebase_info has already been called. This is + // recommended by Apple's QA1398. + kern_return_t kr = mach_timebase_info(&timebase_info); + DCHECK(kr == KERN_SUCCESS); + } + + // mach_absolute_time is it when it comes to ticks on the Mac. Other calls + // with less precision (such as TickCount) just call through to + // mach_absolute_time. + + // timebase_info converts absolute time tick units into nanoseconds. Convert + // to microseconds up front to stave off overflows. + absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * + timebase_info.numer / timebase_info.denom; + + // Don't bother with the rollover handling that the Windows version does. + // With numer and denom = 1 (the expected case), the 64-bit absolute time + // reported in nanoseconds is enough to last nearly 585 years. + + return TimeTicks(absolute_micro); +} + +// static +TimeTicks TimeTicks::HighResNow() { + return Now(); +} + +} // namespace base diff --git a/base/time_posix.cc b/base/time_posix.cc index 901b65c..81f9bad 100644 --- a/base/time_posix.cc +++ b/base/time_posix.cc @@ -4,9 +4,6 @@ #include "base/time.h" -#ifdef OS_MACOSX -#include <mach/mach_time.h> -#endif #include <sys/time.h> #include <time.h> @@ -92,33 +89,7 @@ void Time::Explode(bool is_local, Exploded* exploded) const { TimeTicks TimeTicks::Now() { uint64_t absolute_micro; -#if defined(OS_MACOSX) - - static mach_timebase_info_data_t timebase_info; - if (timebase_info.denom == 0) { - // Zero-initialization of statics guarantees that denom will be 0 before - // calling mach_timebase_info. mach_timebase_info will never set denom to - // 0 as that would be invalid, so the zero-check can be used to determine - // whether mach_timebase_info has already been called. This is - // recommended by Apple's QA1398. - kern_return_t kr = mach_timebase_info(&timebase_info); - DCHECK(kr == KERN_SUCCESS); - } - - // mach_absolute_time is it when it comes to ticks on the Mac. Other calls - // with less precision (such as TickCount) just call through to - // mach_absolute_time. - - // timebase_info converts absolute time tick units into nanoseconds. Convert - // to microseconds up front to stave off overflows. - absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * - timebase_info.numer / timebase_info.denom; - - // Don't bother with the rollover handling that the Windows version does. - // With numer and denom = 1 (the expected case), the 64-bit absolute time - // reported in nanoseconds is enough to last nearly 585 years. - -#elif defined(OS_POSIX) && \ +#if defined(OS_POSIX) && \ defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 struct timespec ts; |