michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/time.h" michael@0: michael@0: #ifdef OS_MACOSX michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifdef ANDROID michael@0: #include michael@0: #else michael@0: #include michael@0: #endif michael@0: #if defined(ANDROID) || defined(OS_POSIX) michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: michael@0: #include "base/basictypes.h" michael@0: #include "base/logging.h" michael@0: michael@0: namespace base { michael@0: michael@0: // The Time routines in this file use standard POSIX routines, or almost- michael@0: // standard routines in the case of timegm. We need to use a Mach-specific michael@0: // function for TimeTicks::Now() on Mac OS X. michael@0: michael@0: // Time ----------------------------------------------------------------------- michael@0: michael@0: // Some functions in time.cc use time_t directly, so we provide a zero offset michael@0: // for them. The epoch is 1970-01-01 00:00:00 UTC. michael@0: // static michael@0: const int64_t Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); michael@0: michael@0: // static michael@0: Time Time::Now() { michael@0: struct timeval tv; michael@0: struct timezone tz = { 0, 0 }; // UTC michael@0: if (gettimeofday(&tv, &tz) != 0) { michael@0: DCHECK(0) << "Could not determine time of day"; michael@0: } michael@0: // Combine seconds and microseconds in a 64-bit field containing microseconds michael@0: // since the epoch. That's enough for nearly 600 centuries. michael@0: return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; michael@0: } michael@0: michael@0: // static michael@0: Time Time::NowFromSystemTime() { michael@0: // Just use Now() because Now() returns the system time. michael@0: return Now(); michael@0: } michael@0: michael@0: // static michael@0: Time Time::FromExploded(bool is_local, const Exploded& exploded) { michael@0: struct tm timestruct; michael@0: timestruct.tm_sec = exploded.second; michael@0: timestruct.tm_min = exploded.minute; michael@0: timestruct.tm_hour = exploded.hour; michael@0: timestruct.tm_mday = exploded.day_of_month; michael@0: timestruct.tm_mon = exploded.month - 1; michael@0: timestruct.tm_year = exploded.year - 1900; michael@0: timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this michael@0: timestruct.tm_yday = 0; // mktime/timegm ignore this michael@0: timestruct.tm_isdst = -1; // attempt to figure it out michael@0: timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore michael@0: timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore michael@0: michael@0: time_t seconds; michael@0: #ifdef ANDROID michael@0: seconds = mktime(×truct); michael@0: #else michael@0: if (is_local) michael@0: seconds = mktime(×truct); michael@0: else michael@0: seconds = timegm(×truct); michael@0: #endif michael@0: michael@0: int64_t milliseconds; michael@0: // Handle overflow. Clamping the range to what mktime and timegm might michael@0: // return is the best that can be done here. It's not ideal, but it's better michael@0: // than failing here or ignoring the overflow case and treating each time michael@0: // overflow as one second prior to the epoch. michael@0: if (seconds == -1 && michael@0: (exploded.year < 1969 || exploded.year > 1970)) { michael@0: // If exploded.year is 1969 or 1970, take -1 as correct, with the michael@0: // time indicating 1 second prior to the epoch. (1970 is allowed to handle michael@0: // time zone and DST offsets.) Otherwise, return the most future or past michael@0: // time representable. Assumes the time_t epoch is 1970-01-01 00:00:00 UTC. michael@0: // michael@0: // The minimum and maximum representible times that mktime and timegm could michael@0: // return are used here instead of values outside that range to allow for michael@0: // proper round-tripping between exploded and counter-type time michael@0: // representations in the presence of possible truncation to time_t by michael@0: // division and use with other functions that accept time_t. michael@0: // michael@0: // When representing the most distant time in the future, add in an extra michael@0: // 999ms to avoid the time being less than any other possible value that michael@0: // this function can return. michael@0: if (exploded.year < 1969) { michael@0: milliseconds = std::numeric_limits::min() * michael@0: kMillisecondsPerSecond; michael@0: } else { michael@0: milliseconds = (std::numeric_limits::max() * michael@0: kMillisecondsPerSecond) + michael@0: kMillisecondsPerSecond - 1; michael@0: } michael@0: } else { michael@0: milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; michael@0: } michael@0: michael@0: return Time(milliseconds * kMicrosecondsPerMillisecond); michael@0: } michael@0: michael@0: void Time::Explode(bool is_local, Exploded* exploded) const { michael@0: // Time stores times with microsecond resolution, but Exploded only carries michael@0: // millisecond resolution, so begin by being lossy. michael@0: int64_t milliseconds = us_ / kMicrosecondsPerMillisecond; michael@0: time_t seconds = milliseconds / kMillisecondsPerSecond; michael@0: michael@0: struct tm timestruct; michael@0: if (is_local) michael@0: localtime_r(&seconds, ×truct); michael@0: else michael@0: gmtime_r(&seconds, ×truct); michael@0: michael@0: exploded->year = timestruct.tm_year + 1900; michael@0: exploded->month = timestruct.tm_mon + 1; michael@0: exploded->day_of_week = timestruct.tm_wday; michael@0: exploded->day_of_month = timestruct.tm_mday; michael@0: exploded->hour = timestruct.tm_hour; michael@0: exploded->minute = timestruct.tm_min; michael@0: exploded->second = timestruct.tm_sec; michael@0: exploded->millisecond = milliseconds % kMillisecondsPerSecond; michael@0: } michael@0: michael@0: // TimeTicks ------------------------------------------------------------------ michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::Now() { michael@0: uint64_t absolute_micro; michael@0: michael@0: #if defined(OS_MACOSX) michael@0: static mach_timebase_info_data_t timebase_info; michael@0: if (timebase_info.denom == 0) { michael@0: // Zero-initialization of statics guarantees that denom will be 0 before michael@0: // calling mach_timebase_info. mach_timebase_info will never set denom to michael@0: // 0 as that would be invalid, so the zero-check can be used to determine michael@0: // whether mach_timebase_info has already been called. This is michael@0: // recommended by Apple's QA1398. michael@0: kern_return_t kr = mach_timebase_info(&timebase_info); michael@0: DCHECK(kr == KERN_SUCCESS); michael@0: } michael@0: michael@0: // mach_absolute_time is it when it comes to ticks on the Mac. Other calls michael@0: // with less precision (such as TickCount) just call through to michael@0: // mach_absolute_time. michael@0: michael@0: // timebase_info converts absolute time tick units into nanoseconds. Convert michael@0: // to microseconds up front to stave off overflows. michael@0: absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond * michael@0: timebase_info.numer / timebase_info.denom; michael@0: michael@0: // Don't bother with the rollover handling that the Windows version does. michael@0: // With numer and denom = 1 (the expected case), the 64-bit absolute time michael@0: // reported in nanoseconds is enough to last nearly 585 years. michael@0: michael@0: #elif defined(OS_OPENBSD) || defined(OS_POSIX) && \ michael@0: defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0 michael@0: michael@0: struct timespec ts; michael@0: if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { michael@0: NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; michael@0: return TimeTicks(); michael@0: } michael@0: michael@0: absolute_micro = michael@0: (static_cast(ts.tv_sec) * Time::kMicrosecondsPerSecond) + michael@0: (static_cast(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); michael@0: michael@0: #else // _POSIX_MONOTONIC_CLOCK michael@0: #error No usable tick clock function on this platform. michael@0: #endif // _POSIX_MONOTONIC_CLOCK michael@0: michael@0: return TimeTicks(absolute_micro); michael@0: } michael@0: michael@0: // static michael@0: TimeTicks TimeTicks::HighResNow() { michael@0: return Now(); michael@0: } michael@0: michael@0: } // namespace base