tony@chromium.org | 1507065 | 2009-08-29 06:03:17 +0900 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 4 | |
| 5 | #include "base/time.h" |
| 6 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 7 | #include <sys/time.h> |
| 8 | #include <time.h> |
| 9 | |
mark@chromium.org | 430a647 | 2008-11-05 04:45:51 +0900 | [diff] [blame] | 10 | #include <limits> |
| 11 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 12 | #include "base/basictypes.h" |
| 13 | #include "base/logging.h" |
| 14 | |
dsh@google.com | 0f8dd26 | 2008-10-28 05:43:33 +0900 | [diff] [blame] | 15 | namespace base { |
| 16 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 17 | // The Time routines in this file use standard POSIX routines, or almost- |
| 18 | // standard routines in the case of timegm. We need to use a Mach-specific |
| 19 | // function for TimeTicks::Now() on Mac OS X. |
| 20 | |
| 21 | // Time ----------------------------------------------------------------------- |
| 22 | |
brettw@chromium.org | d2f9782 | 2009-08-26 11:53:36 +0900 | [diff] [blame] | 23 | // Windows uses a Gregorian epoch of 1601. We need to match this internally |
| 24 | // so that our time representations match across all platforms. See bug 14734. |
| 25 | // irb(main):010:0> Time.at(0).getutc() |
| 26 | // => Thu Jan 01 00:00:00 UTC 1970 |
| 27 | // irb(main):011:0> Time.at(-11644473600).getutc() |
| 28 | // => Mon Jan 01 00:00:00 UTC 1601 |
| 29 | static const int64 kWindowsEpochDeltaSeconds = GG_INT64_C(11644473600); |
| 30 | static const int64 kWindowsEpochDeltaMilliseconds = |
| 31 | kWindowsEpochDeltaSeconds * Time::kMillisecondsPerSecond; |
| 32 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 33 | // static |
brettw@chromium.org | d2f9782 | 2009-08-26 11:53:36 +0900 | [diff] [blame] | 34 | const int64 Time::kWindowsEpochDeltaMicroseconds = |
| 35 | kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond; |
| 36 | |
| 37 | // Some functions in time.cc use time_t directly, so we provide an offset |
| 38 | // to convert from time_t (Unix epoch) and internal (Windows epoch). |
| 39 | // static |
| 40 | const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 41 | |
| 42 | // static |
deanm@chromium.org | a9d0d48 | 2008-09-11 23:06:48 +0900 | [diff] [blame] | 43 | Time Time::Now() { |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 44 | struct timeval tv; |
| 45 | struct timezone tz = { 0, 0 }; // UTC |
| 46 | if (gettimeofday(&tv, &tz) != 0) { |
| 47 | DCHECK(0) << "Could not determine time of day"; |
| 48 | } |
| 49 | // Combine seconds and microseconds in a 64-bit field containing microseconds |
brettw@chromium.org | d2f9782 | 2009-08-26 11:53:36 +0900 | [diff] [blame] | 50 | // since the epoch. That's enough for nearly 600 centuries. Adjust from |
| 51 | // Unix (1970) to Windows (1601) epoch. |
| 52 | return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + |
| 53 | kWindowsEpochDeltaMicroseconds); |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // static |
erikkay@google.com | 9ac2676 | 2009-04-18 09:42:48 +0900 | [diff] [blame] | 57 | Time Time::NowFromSystemTime() { |
| 58 | // Just use Now() because Now() returns the system time. |
| 59 | return Now(); |
| 60 | } |
| 61 | |
| 62 | // static |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 63 | Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
| 64 | struct tm timestruct; |
| 65 | timestruct.tm_sec = exploded.second; |
| 66 | timestruct.tm_min = exploded.minute; |
| 67 | timestruct.tm_hour = exploded.hour; |
| 68 | timestruct.tm_mday = exploded.day_of_month; |
| 69 | timestruct.tm_mon = exploded.month - 1; |
| 70 | timestruct.tm_year = exploded.year - 1900; |
| 71 | timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this |
| 72 | timestruct.tm_yday = 0; // mktime/timegm ignore this |
| 73 | timestruct.tm_isdst = -1; // attempt to figure it out |
| 74 | timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore |
| 75 | timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore |
| 76 | |
| 77 | time_t seconds; |
| 78 | if (is_local) |
| 79 | seconds = mktime(×truct); |
| 80 | else |
| 81 | seconds = timegm(×truct); |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 82 | |
mark@chromium.org | 430a647 | 2008-11-05 04:45:51 +0900 | [diff] [blame] | 83 | int64 milliseconds; |
| 84 | // Handle overflow. Clamping the range to what mktime and timegm might |
| 85 | // return is the best that can be done here. It's not ideal, but it's better |
| 86 | // than failing here or ignoring the overflow case and treating each time |
| 87 | // overflow as one second prior to the epoch. |
| 88 | if (seconds == -1 && |
| 89 | (exploded.year < 1969 || exploded.year > 1970)) { |
| 90 | // If exploded.year is 1969 or 1970, take -1 as correct, with the |
| 91 | // time indicating 1 second prior to the epoch. (1970 is allowed to handle |
| 92 | // time zone and DST offsets.) Otherwise, return the most future or past |
| 93 | // time representable. Assumes the time_t epoch is 1970-01-01 00:00:00 UTC. |
| 94 | // |
| 95 | // The minimum and maximum representible times that mktime and timegm could |
| 96 | // return are used here instead of values outside that range to allow for |
| 97 | // proper round-tripping between exploded and counter-type time |
| 98 | // representations in the presence of possible truncation to time_t by |
| 99 | // division and use with other functions that accept time_t. |
| 100 | // |
| 101 | // When representing the most distant time in the future, add in an extra |
| 102 | // 999ms to avoid the time being less than any other possible value that |
| 103 | // this function can return. |
| 104 | if (exploded.year < 1969) { |
| 105 | milliseconds = std::numeric_limits<time_t>::min() * |
| 106 | kMillisecondsPerSecond; |
| 107 | } else { |
| 108 | milliseconds = (std::numeric_limits<time_t>::max() * |
| 109 | kMillisecondsPerSecond) + |
| 110 | kMillisecondsPerSecond - 1; |
| 111 | } |
| 112 | } else { |
| 113 | milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; |
| 114 | } |
| 115 | |
brettw@chromium.org | d2f9782 | 2009-08-26 11:53:36 +0900 | [diff] [blame] | 116 | // Adjust from Unix (1970) to Windows (1601) epoch. |
| 117 | return Time((milliseconds * kMicrosecondsPerMillisecond) + |
| 118 | kWindowsEpochDeltaMicroseconds); |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void Time::Explode(bool is_local, Exploded* exploded) const { |
| 122 | // Time stores times with microsecond resolution, but Exploded only carries |
brettw@chromium.org | d2f9782 | 2009-08-26 11:53:36 +0900 | [diff] [blame] | 123 | // millisecond resolution, so begin by being lossy. Adjust from Windows |
| 124 | // epoch (1601) to Unix epoch (1970); |
| 125 | int64 milliseconds = (us_ - kWindowsEpochDeltaMicroseconds) / |
| 126 | kMicrosecondsPerMillisecond; |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 127 | time_t seconds = milliseconds / kMillisecondsPerSecond; |
| 128 | |
| 129 | struct tm timestruct; |
| 130 | if (is_local) |
| 131 | localtime_r(&seconds, ×truct); |
| 132 | else |
| 133 | gmtime_r(&seconds, ×truct); |
| 134 | |
| 135 | exploded->year = timestruct.tm_year + 1900; |
| 136 | exploded->month = timestruct.tm_mon + 1; |
| 137 | exploded->day_of_week = timestruct.tm_wday; |
| 138 | exploded->day_of_month = timestruct.tm_mday; |
| 139 | exploded->hour = timestruct.tm_hour; |
| 140 | exploded->minute = timestruct.tm_min; |
| 141 | exploded->second = timestruct.tm_sec; |
| 142 | exploded->millisecond = milliseconds % kMillisecondsPerSecond; |
| 143 | } |
| 144 | |
| 145 | // TimeTicks ------------------------------------------------------------------ |
wtc@chromium.org | 9b07a8f | 2009-09-01 07:25:00 +0900 | [diff] [blame] | 146 | // FreeBSD 6 has CLOCK_MONOLITHIC but defines _POSIX_MONOTONIC_CLOCK to -1. |
| 147 | #if (defined(OS_POSIX) && \ |
| 148 | defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ |
| 149 | defined(OS_FREEBSD) |
brettw@chromium.org | d2f9782 | 2009-08-26 11:53:36 +0900 | [diff] [blame] | 150 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 151 | // static |
| 152 | TimeTicks TimeTicks::Now() { |
| 153 | uint64_t absolute_micro; |
| 154 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 155 | struct timespec ts; |
| 156 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { |
| 157 | NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; |
| 158 | return TimeTicks(); |
| 159 | } |
| 160 | |
| 161 | absolute_micro = |
| 162 | (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
| 163 | (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
| 164 | |
brettw@chromium.org | d2f9782 | 2009-08-26 11:53:36 +0900 | [diff] [blame] | 165 | return TimeTicks(absolute_micro); |
| 166 | } |
| 167 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 168 | #else // _POSIX_MONOTONIC_CLOCK |
| 169 | #error No usable tick clock function on this platform. |
| 170 | #endif // _POSIX_MONOTONIC_CLOCK |
| 171 | |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 172 | // static |
mbelshe@google.com | 556bc84 | 2008-09-26 12:00:00 +0900 | [diff] [blame] | 173 | TimeTicks TimeTicks::HighResNow() { |
mmentovai@google.com | 7ba78d7 | 2008-08-12 03:15:10 +0900 | [diff] [blame] | 174 | return Now(); |
| 175 | } |
dsh@google.com | 0f8dd26 | 2008-10-28 05:43:33 +0900 | [diff] [blame] | 176 | |
| 177 | } // namespace base |