| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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.h" | 5 #include "base/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_time.h> | 9 #include <mach/mach_time.h> |
| 10 #include <sys/time.h> | 10 #include <sys/time.h> |
| 11 #include <time.h> | 11 #include <time.h> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/scoped_cftyperef.h" | 15 #include "base/scoped_cftyperef.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 | 18 |
| 19 // The Time routines in this file use Mach and CoreFoundation APIs, since the | 19 // The Time routines in this file use Mach and CoreFoundation APIs, since the |
| 20 // POSIX definition of time_t in Mac OS X wraps around after 2038--and | 20 // POSIX definition of time_t in Mac OS X wraps around after 2038--and |
| 21 // there are already cookie expiration dates, etc., past that time out in | 21 // there are already cookie expiration dates, etc., past that time out in |
| 22 // the field. Using CFDate prevents that problem, and using mach_absolute_time | 22 // the field. Using CFDate prevents that problem, and using mach_absolute_time |
| 23 // for TimeTicks gives us nice high-resolution interval timing. | 23 // for TimeTicks gives us nice high-resolution interval timing. |
| 24 | 24 |
| 25 // Time ----------------------------------------------------------------------- | 25 // Time ----------------------------------------------------------------------- |
| 26 | 26 |
| 27 // The internal representation of Time uses a 64-bit microsecond count | 27 // Core Foundation uses a double second count since 2001-01-01 00:00:00 UTC. |
| 28 // from 1970-01-01 00:00:00 UTC. Core Foundation uses a double second count | 28 // The UNIX epoch is 1970-01-01 00:00:00 UTC. |
| 29 // since 2001-01-01 00:00:00 UTC. | 29 // Windows uses a Gregorian epoch of 1601. We need to match this internally |
| 30 // so that our time representations match across all platforms. See bug 14734. |
| 31 // irb(main):010:0> Time.at(0).getutc() |
| 32 // => Thu Jan 01 00:00:00 UTC 1970 |
| 33 // irb(main):011:0> Time.at(-11644473600).getutc() |
| 34 // => Mon Jan 01 00:00:00 UTC 1601 |
| 35 static const int64 kWindowsEpochDeltaSeconds = GG_INT64_C(11644473600); |
| 36 static const int64 kWindowsEpochDeltaMilliseconds = |
| 37 kWindowsEpochDeltaSeconds * Time::kMillisecondsPerSecond; |
| 30 | 38 |
| 31 // Some functions in time.cc use time_t directly, so we provide a zero offset | |
| 32 // for them. The epoch is 1970-01-01 00:00:00 UTC. | |
| 33 // static | 39 // static |
| 34 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); | 40 const int64 Time::kWindowsEpochDeltaMicroseconds = |
| 41 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
| 42 |
| 43 // Some functions in time.cc use time_t directly, so we provide an offset |
| 44 // to convert from time_t (Unix epoch) and internal (Windows epoch). |
| 45 // static |
| 46 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
| 35 | 47 |
| 36 // static | 48 // static |
| 37 Time Time::Now() { | 49 Time Time::Now() { |
| 38 CFAbsoluteTime now = | 50 CFAbsoluteTime now = |
| 39 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; | 51 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; |
| 40 return Time(static_cast<int64>(now * kMicrosecondsPerSecond)); | 52 return Time(static_cast<int64>(now * kMicrosecondsPerSecond) + |
| 53 kWindowsEpochDeltaMicroseconds); |
| 41 } | 54 } |
| 42 | 55 |
| 43 // static | 56 // static |
| 44 Time Time::NowFromSystemTime() { | 57 Time Time::NowFromSystemTime() { |
| 45 // Just use Now() because Now() returns the system time. | 58 // Just use Now() because Now() returns the system time. |
| 46 return Now(); | 59 return Now(); |
| 47 } | 60 } |
| 48 | 61 |
| 49 // static | 62 // static |
| 50 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 63 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
| 51 CFGregorianDate date; | 64 CFGregorianDate date; |
| 52 date.second = exploded.second + | 65 date.second = exploded.second + |
| 53 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); | 66 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); |
| 54 date.minute = exploded.minute; | 67 date.minute = exploded.minute; |
| 55 date.hour = exploded.hour; | 68 date.hour = exploded.hour; |
| 56 date.day = exploded.day_of_month; | 69 date.day = exploded.day_of_month; |
| 57 date.month = exploded.month; | 70 date.month = exploded.month; |
| 58 date.year = exploded.year; | 71 date.year = exploded.year; |
| 59 | 72 |
| 60 scoped_cftyperef<CFTimeZoneRef> | 73 scoped_cftyperef<CFTimeZoneRef> |
| 61 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); | 74 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
| 62 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + | 75 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + |
| 63 kCFAbsoluteTimeIntervalSince1970; | 76 kCFAbsoluteTimeIntervalSince1970; |
| 64 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond)); | 77 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + |
| 78 kWindowsEpochDeltaMicroseconds); |
| 65 } | 79 } |
| 66 | 80 |
| 67 void Time::Explode(bool is_local, Exploded* exploded) const { | 81 void Time::Explode(bool is_local, Exploded* exploded) const { |
| 68 CFAbsoluteTime seconds = | 82 CFAbsoluteTime seconds = |
| 69 (static_cast<double>(us_) / kMicrosecondsPerSecond) - | 83 (static_cast<double>((us_ - kWindowsEpochDeltaMicroseconds) / |
| 70 kCFAbsoluteTimeIntervalSince1970; | 84 kMicrosecondsPerSecond) - kCFAbsoluteTimeIntervalSince1970); |
| 71 | 85 |
| 72 scoped_cftyperef<CFTimeZoneRef> | 86 scoped_cftyperef<CFTimeZoneRef> |
| 73 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); | 87 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
| 74 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); | 88 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); |
| 75 | 89 |
| 76 exploded->year = date.year; | 90 exploded->year = date.year; |
| 77 exploded->month = date.month; | 91 exploded->month = date.month; |
| 78 exploded->day_of_month = date.day; | 92 exploded->day_of_month = date.day; |
| 79 exploded->hour = date.hour; | 93 exploded->hour = date.hour; |
| 80 exploded->minute = date.minute; | 94 exploded->minute = date.minute; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 130 |
| 117 return TimeTicks(absolute_micro); | 131 return TimeTicks(absolute_micro); |
| 118 } | 132 } |
| 119 | 133 |
| 120 // static | 134 // static |
| 121 TimeTicks TimeTicks::HighResNow() { | 135 TimeTicks TimeTicks::HighResNow() { |
| 122 return Now(); | 136 return Now(); |
| 123 } | 137 } |
| 124 | 138 |
| 125 } // namespace base | 139 } // namespace base |
| OLD | NEW |