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