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