| 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; |
| 38 static const int64 kWindowsEpochDeltaMicroseconds = |
| 39 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
| 30 | 40 |
| 31 // Some functions in time.cc use time_t directly, so we provide a zero offset | 41 // Some functions in time.cc use time_t directly, so we provide an offset |
| 32 // for them. The epoch is 1970-01-01 00:00:00 UTC. | 42 // to convert from time_t (Unix epoch) and internal (Windows epoch). |
| 33 // static | 43 // static |
| 34 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); | 44 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
| 35 | 45 |
| 36 // static | 46 // static |
| 37 Time Time::Now() { | 47 Time Time::Now() { |
| 38 CFAbsoluteTime now = | 48 CFAbsoluteTime now = |
| 39 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; | 49 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970; |
| 40 return Time(static_cast<int64>(now * kMicrosecondsPerSecond)); | 50 return Time(static_cast<int64>(now * kMicrosecondsPerSecond) + |
| 51 kWindowsEpochDeltaMicroseconds); |
| 41 } | 52 } |
| 42 | 53 |
| 43 // static | 54 // static |
| 44 Time Time::NowFromSystemTime() { | 55 Time Time::NowFromSystemTime() { |
| 45 // Just use Now() because Now() returns the system time. | 56 // Just use Now() because Now() returns the system time. |
| 46 return Now(); | 57 return Now(); |
| 47 } | 58 } |
| 48 | 59 |
| 49 // static | 60 // static |
| 50 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 61 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
| 51 CFGregorianDate date; | 62 CFGregorianDate date; |
| 52 date.second = exploded.second + | 63 date.second = exploded.second + |
| 53 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); | 64 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond); |
| 54 date.minute = exploded.minute; | 65 date.minute = exploded.minute; |
| 55 date.hour = exploded.hour; | 66 date.hour = exploded.hour; |
| 56 date.day = exploded.day_of_month; | 67 date.day = exploded.day_of_month; |
| 57 date.month = exploded.month; | 68 date.month = exploded.month; |
| 58 date.year = exploded.year; | 69 date.year = exploded.year; |
| 59 | 70 |
| 60 scoped_cftyperef<CFTimeZoneRef> | 71 scoped_cftyperef<CFTimeZoneRef> |
| 61 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); | 72 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
| 62 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + | 73 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) + |
| 63 kCFAbsoluteTimeIntervalSince1970; | 74 kCFAbsoluteTimeIntervalSince1970; |
| 64 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond)); | 75 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) + |
| 76 kWindowsEpochDeltaMicroseconds); |
| 65 } | 77 } |
| 66 | 78 |
| 67 void Time::Explode(bool is_local, Exploded* exploded) const { | 79 void Time::Explode(bool is_local, Exploded* exploded) const { |
| 68 CFAbsoluteTime seconds = | 80 CFAbsoluteTime seconds = |
| 69 (static_cast<double>(us_) / kMicrosecondsPerSecond) - | 81 (static_cast<double>((us_ - kWindowsEpochDeltaMicroseconds) / |
| 70 kCFAbsoluteTimeIntervalSince1970; | 82 kMicrosecondsPerSecond) - kCFAbsoluteTimeIntervalSince1970); |
| 71 | 83 |
| 72 scoped_cftyperef<CFTimeZoneRef> | 84 scoped_cftyperef<CFTimeZoneRef> |
| 73 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); | 85 time_zone(is_local ? CFTimeZoneCopySystem() : NULL); |
| 74 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); | 86 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone); |
| 75 | 87 |
| 76 exploded->year = date.year; | 88 exploded->year = date.year; |
| 77 exploded->month = date.month; | 89 exploded->month = date.month; |
| 78 exploded->day_of_month = date.day; | 90 exploded->day_of_month = date.day; |
| 79 exploded->hour = date.hour; | 91 exploded->hour = date.hour; |
| 80 exploded->minute = date.minute; | 92 exploded->minute = date.minute; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 128 |
| 117 return TimeTicks(absolute_micro); | 129 return TimeTicks(absolute_micro); |
| 118 } | 130 } |
| 119 | 131 |
| 120 // static | 132 // static |
| 121 TimeTicks TimeTicks::HighResNow() { | 133 TimeTicks TimeTicks::HighResNow() { |
| 122 return Now(); | 134 return Now(); |
| 123 } | 135 } |
| 124 | 136 |
| 125 } // namespace base | 137 } // namespace base |
| OLD | NEW |