time_posix.cc (7542B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #include "base/time.h" 8 9 #ifdef XP_DARWIN 10 # include <mach/mach_time.h> 11 #endif 12 #include <sys/time.h> 13 #if defined(ANDROID) && !defined(__LP64__) 14 # include <time64.h> 15 #else 16 # include <time.h> 17 #endif 18 #if defined(ANDROID) || defined(XP_UNIX) 19 # include <unistd.h> 20 #endif 21 22 #include <limits> 23 #include <cstdint> 24 25 #include "base/logging.h" 26 27 namespace base { 28 29 // The Time routines in this file use standard POSIX routines, or almost- 30 // standard routines in the case of timegm. We need to use a Mach-specific 31 // function for TimeTicks::Now() on Mac OS X. 32 33 // Time ----------------------------------------------------------------------- 34 35 // Some functions in time.cc use time_t directly, so we provide a zero offset 36 // for them. The epoch is 1970-01-01 00:00:00 UTC. 37 // static 38 const int64_t Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); 39 40 // static 41 Time Time::Now() { 42 struct timeval tv; 43 struct timezone tz = {0, 0}; // UTC 44 if (gettimeofday(&tv, &tz) != 0) { 45 DCHECK(0) << "Could not determine time of day"; 46 } 47 // Combine seconds and microseconds in a 64-bit field containing microseconds 48 // since the epoch. That's enough for nearly 600 centuries. 49 return Time(tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec); 50 } 51 52 // static 53 Time Time::NowFromSystemTime() { 54 // Just use Now() because Now() returns the system time. 55 return Now(); 56 } 57 58 // static 59 Time Time::FromExploded(bool is_local, const Exploded& exploded) { 60 struct tm timestruct; 61 timestruct.tm_sec = exploded.second; 62 timestruct.tm_min = exploded.minute; 63 timestruct.tm_hour = exploded.hour; 64 timestruct.tm_mday = exploded.day_of_month; 65 timestruct.tm_mon = exploded.month - 1; 66 timestruct.tm_year = exploded.year - 1900; 67 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this 68 timestruct.tm_yday = 0; // mktime/timegm ignore this 69 timestruct.tm_isdst = -1; // attempt to figure it out 70 #ifndef XP_SOLARIS 71 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore 72 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore 73 #endif 74 75 time_t seconds; 76 #ifdef ANDROID 77 seconds = mktime(×truct); 78 #else 79 if (is_local) 80 seconds = mktime(×truct); 81 else 82 seconds = timegm(×truct); 83 #endif 84 85 int64_t 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 && (exploded.year < 1969 || exploded.year > 1970)) { 91 // If exploded.year is 1969 or 1970, take -1 as correct, with the 92 // time indicating 1 second prior to the epoch. (1970 is allowed to handle 93 // time zone and DST offsets.) Otherwise, return the most future or past 94 // time representable. Assumes the time_t epoch is 1970-01-01 00:00:00 UTC. 95 // 96 // The minimum and maximum representible times that mktime and timegm could 97 // return are used here instead of values outside that range to allow for 98 // proper round-tripping between exploded and counter-type time 99 // representations in the presence of possible truncation to time_t by 100 // division and use with other functions that accept time_t. 101 // 102 // When representing the most distant time in the future, add in an extra 103 // 999ms to avoid the time being less than any other possible value that 104 // this function can return. 105 106 // Take care to avoid overflows when time_t is int64_t. 107 if (exploded.year < 1969) { 108 int64_t min_seconds = (sizeof(time_t) < sizeof(int64_t)) 109 ? std::numeric_limits<time_t>::min() 110 : std::numeric_limits<int32_t>::min(); 111 milliseconds = min_seconds * kMillisecondsPerSecond; 112 } else { 113 int64_t max_seconds = (sizeof(time_t) < sizeof(int64_t)) 114 ? std::numeric_limits<time_t>::max() 115 : std::numeric_limits<int32_t>::max(); 116 milliseconds = max_seconds * kMillisecondsPerSecond; 117 milliseconds += kMillisecondsPerSecond - 1; 118 } 119 } else { 120 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; 121 } 122 123 return Time(milliseconds * kMicrosecondsPerMillisecond); 124 } 125 126 void Time::Explode(bool is_local, Exploded* exploded) const { 127 // Time stores times with microsecond resolution, but Exploded only carries 128 // millisecond resolution, so begin by being lossy. 129 int64_t milliseconds = us_ / kMicrosecondsPerMillisecond; 130 time_t seconds = milliseconds / kMillisecondsPerSecond; 131 132 struct tm timestruct; 133 if (is_local) 134 localtime_r(&seconds, ×truct); 135 else 136 gmtime_r(&seconds, ×truct); 137 138 exploded->year = timestruct.tm_year + 1900; 139 exploded->month = timestruct.tm_mon + 1; 140 exploded->day_of_week = timestruct.tm_wday; 141 exploded->day_of_month = timestruct.tm_mday; 142 exploded->hour = timestruct.tm_hour; 143 exploded->minute = timestruct.tm_min; 144 exploded->second = timestruct.tm_sec; 145 exploded->millisecond = milliseconds % kMillisecondsPerSecond; 146 } 147 148 // TimeTicks ------------------------------------------------------------------ 149 150 // static 151 TimeTicks TimeTicks::Now() { 152 uint64_t absolute_micro; 153 154 #if defined(XP_DARWIN) 155 static mach_timebase_info_data_t timebase_info; 156 if (timebase_info.denom == 0) { 157 // Zero-initialization of statics guarantees that denom will be 0 before 158 // calling mach_timebase_info. mach_timebase_info will never set denom to 159 // 0 as that would be invalid, so the zero-check can be used to determine 160 // whether mach_timebase_info has already been called. This is 161 // recommended by Apple's QA1398. 162 kern_return_t kr = mach_timebase_info(&timebase_info); 163 DCHECK(kr == KERN_SUCCESS); 164 } 165 166 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls 167 // with less precision (such as TickCount) just call through to 168 // mach_absolute_time. 169 170 // timebase_info converts absolute time tick units into nanoseconds. Convert 171 // to microseconds up front to stave off overflows. 172 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * 173 timebase_info.numer / timebase_info.denom; 174 175 // Don't bother with the rollover handling that the Windows version does. 176 // With numer and denom = 1 (the expected case), the 64-bit absolute time 177 // reported in nanoseconds is enough to last nearly 585 years. 178 179 #elif defined(XP_OPENBSD) || defined(XP_UNIX) && \ 180 defined(_POSIX_MONOTONIC_CLOCK) && \ 181 _POSIX_MONOTONIC_CLOCK >= 0 182 183 struct timespec ts; 184 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { 185 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; 186 return TimeTicks(); 187 } 188 189 absolute_micro = 190 (static_cast<int64_t>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + 191 (static_cast<int64_t>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); 192 193 #else // _POSIX_MONOTONIC_CLOCK 194 # error No usable tick clock function on this platform. 195 #endif // _POSIX_MONOTONIC_CLOCK 196 197 return TimeTicks(absolute_micro); 198 } 199 200 } // namespace base