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