blob: da17e28af4eedd34ee5b605e6a8bf6ecd49cb813 [file] [log] [blame]
[email protected]cc6fe332012-01-10 00:22:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8822d972008-11-03 19:36:592// 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 <CoreFoundation/CFDate.h>
8#include <CoreFoundation/CFTimeZone.h>
9#include <mach/mach_time.h>
10#include <sys/time.h>
11#include <time.h>
12
13#include "base/basictypes.h"
14#include "base/logging.h"
[email protected]df0ca6c82010-10-17 04:09:0615#include "base/mac/scoped_cftyperef.h"
[email protected]8822d972008-11-03 19:36:5916
17namespace base {
18
19// The Time routines in this file use Mach and CoreFoundation APIs, since the
20// POSIX definition of time_t in Mac OS X wraps around after 2038--and
21// there are already cookie expiration dates, etc., past that time out in
22// the field. Using CFDate prevents that problem, and using mach_absolute_time
23// for TimeTicks gives us nice high-resolution interval timing.
24
25// Time -----------------------------------------------------------------------
26
[email protected]a4a3292e2009-08-26 02:53:3627// Core Foundation uses a double second count since 2001-01-01 00:00:00 UTC.
28// The UNIX epoch is 1970-01-01 00:00:00 UTC.
29// Windows uses a Gregorian epoch of 1601. We need to match this internally
30// so that our time representations match across all platforms. See bug 14734.
31// irb(main):010:0> Time.at(0).getutc()
32// => Thu Jan 01 00:00:00 UTC 1970
33// irb(main):011:0> Time.at(-11644473600).getutc()
34// => Mon Jan 01 00:00:00 UTC 1601
35static const int64 kWindowsEpochDeltaSeconds = GG_INT64_C(11644473600);
36static const int64 kWindowsEpochDeltaMilliseconds =
37 kWindowsEpochDeltaSeconds * Time::kMillisecondsPerSecond;
[email protected]8822d972008-11-03 19:36:5938
[email protected]8822d972008-11-03 19:36:5939// static
[email protected]a4a3292e2009-08-26 02:53:3640const int64 Time::kWindowsEpochDeltaMicroseconds =
41 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond;
42
43// Some functions in time.cc use time_t directly, so we provide an offset
44// to convert from time_t (Unix epoch) and internal (Windows epoch).
45// static
46const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds;
[email protected]8822d972008-11-03 19:36:5947
48// static
49Time Time::Now() {
50 CFAbsoluteTime now =
51 CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970;
[email protected]a4a3292e2009-08-26 02:53:3652 return Time(static_cast<int64>(now * kMicrosecondsPerSecond) +
53 kWindowsEpochDeltaMicroseconds);
[email protected]8822d972008-11-03 19:36:5954}
55
56// static
[email protected]85786f92009-04-18 00:42:4857Time Time::NowFromSystemTime() {
58 // Just use Now() because Now() returns the system time.
59 return Now();
60}
61
62// static
[email protected]8822d972008-11-03 19:36:5963Time Time::FromExploded(bool is_local, const Exploded& exploded) {
64 CFGregorianDate date;
65 date.second = exploded.second +
66 exploded.millisecond / static_cast<double>(kMillisecondsPerSecond);
67 date.minute = exploded.minute;
68 date.hour = exploded.hour;
69 date.day = exploded.day_of_month;
70 date.month = exploded.month;
71 date.year = exploded.year;
72
[email protected]df0ca6c82010-10-17 04:09:0673 base::mac::ScopedCFTypeRef<CFTimeZoneRef>
[email protected]1b5753a2008-11-04 20:36:1174 time_zone(is_local ? CFTimeZoneCopySystem() : NULL);
[email protected]8822d972008-11-03 19:36:5975 CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) +
76 kCFAbsoluteTimeIntervalSince1970;
[email protected]a4a3292e2009-08-26 02:53:3677 return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) +
78 kWindowsEpochDeltaMicroseconds);
[email protected]8822d972008-11-03 19:36:5979}
80
81void Time::Explode(bool is_local, Exploded* exploded) const {
[email protected]cc6fe332012-01-10 00:22:3582 // Avoid rounding issues, by only putting the integral number of seconds
83 // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|).
84 int64 microsecond = us_ % kMicrosecondsPerSecond;
85 if (microsecond < 0)
86 microsecond += kMicrosecondsPerSecond;
87 CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) -
88 kWindowsEpochDeltaSeconds -
89 kCFAbsoluteTimeIntervalSince1970;
[email protected]52a261f2009-03-03 15:01:1290
[email protected]df0ca6c82010-10-17 04:09:0691 base::mac::ScopedCFTypeRef<CFTimeZoneRef>
[email protected]1b5753a2008-11-04 20:36:1192 time_zone(is_local ? CFTimeZoneCopySystem() : NULL);
[email protected]8822d972008-11-03 19:36:5993 CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone);
[email protected]cc6fe332012-01-10 00:22:3594 // 1 = Monday, ..., 7 = Sunday.
95 int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone);
[email protected]52a261f2009-03-03 15:01:1296
[email protected]8822d972008-11-03 19:36:5997 exploded->year = date.year;
98 exploded->month = date.month;
[email protected]cc6fe332012-01-10 00:22:3599 exploded->day_of_week = (cf_day_of_week == 7) ? 0 : cf_day_of_week - 1;
[email protected]8822d972008-11-03 19:36:59100 exploded->day_of_month = date.day;
101 exploded->hour = date.hour;
102 exploded->minute = date.minute;
[email protected]cc6fe332012-01-10 00:22:35103 // Make sure seconds are rounded down towards -infinity.
104 exploded->second = floor(date.second);
105 // Calculate milliseconds ourselves, since we rounded the |seconds|, making
106 // sure to round towards -infinity.
107 exploded->millisecond =
108 (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond :
109 (microsecond - kMicrosecondsPerMillisecond + 1) /
110 kMicrosecondsPerMillisecond;
[email protected]8822d972008-11-03 19:36:59111}
112
113// TimeTicks ------------------------------------------------------------------
114
115// static
116TimeTicks TimeTicks::Now() {
117 uint64_t absolute_micro;
118
119 static mach_timebase_info_data_t timebase_info;
120 if (timebase_info.denom == 0) {
121 // Zero-initialization of statics guarantees that denom will be 0 before
122 // calling mach_timebase_info. mach_timebase_info will never set denom to
123 // 0 as that would be invalid, so the zero-check can be used to determine
124 // whether mach_timebase_info has already been called. This is
125 // recommended by Apple's QA1398.
126 kern_return_t kr = mach_timebase_info(&timebase_info);
[email protected]e1be56d2011-05-04 01:29:38127 DCHECK_EQ(KERN_SUCCESS, kr);
[email protected]8822d972008-11-03 19:36:59128 }
129
130 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
131 // with less precision (such as TickCount) just call through to
132 // mach_absolute_time.
133
134 // timebase_info converts absolute time tick units into nanoseconds. Convert
135 // to microseconds up front to stave off overflows.
136 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
137 timebase_info.numer / timebase_info.denom;
138
139 // Don't bother with the rollover handling that the Windows version does.
140 // With numer and denom = 1 (the expected case), the 64-bit absolute time
141 // reported in nanoseconds is enough to last nearly 585 years.
142
143 return TimeTicks(absolute_micro);
144}
145
146// static
147TimeTicks TimeTicks::HighResNow() {
148 return Now();
149}
150
151} // namespace base