time_mac.cc revision b8cf94937c52feb53b55c39e3f82094d27de464c
1b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Use of this source code is governed by a BSD-style license that can be
3b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// found in the LICENSE file.
4b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
5b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/time/time.h"
6b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
7b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <CoreFoundation/CFDate.h>
8b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <CoreFoundation/CFTimeZone.h>
9b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <mach/mach.h>
10b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <mach/mach_time.h>
11b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <stdint.h>
12b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <sys/sysctl.h>
13b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <sys/time.h>
14b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <sys/types.h>
15b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include <time.h>
16b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
17b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/basictypes.h"
18b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/logging.h"
19b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/mac/mach_logging.h"
20b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/mac/scoped_cftyperef.h"
21b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/mac/scoped_mach_port.h"
22b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#include "base/numerics/safe_conversions.h"
23b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
24b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace {
25b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
26b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratint64_t ComputeCurrentTicks() {
27b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#if defined(OS_IOS)
28b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // On iOS mach_absolute_time stops while the device is sleeping. Instead use
29b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // now - KERN_BOOTTIME to get a time difference that is not impacted by clock
30b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // changes. KERN_BOOTTIME will be updated by the system whenever the system
31b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // clock change.
32b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  struct timeval boottime;
33b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  int mib[2] = {CTL_KERN, KERN_BOOTTIME};
34b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  size_t size = sizeof(boottime);
35b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  int kr = sysctl(mib, arraysize(mib), &boottime, &size, NULL, 0);
36b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  DCHECK_EQ(KERN_SUCCESS, kr);
37b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  base::TimeDelta time_difference = base::Time::Now() -
38b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      (base::Time::FromTimeT(boottime.tv_sec) +
39b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat       base::TimeDelta::FromMicroseconds(boottime.tv_usec));
40b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return time_difference.InMicroseconds();
41b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#else
42b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  static mach_timebase_info_data_t timebase_info;
43b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (timebase_info.denom == 0) {
44b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    // Zero-initialization of statics guarantees that denom will be 0 before
45b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    // calling mach_timebase_info.  mach_timebase_info will never set denom to
46b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    // 0 as that would be invalid, so the zero-check can be used to determine
47b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    // whether mach_timebase_info has already been called.  This is
48b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    // recommended by Apple's QA1398.
49b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    kern_return_t kr = mach_timebase_info(&timebase_info);
50b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    MACH_DCHECK(kr == KERN_SUCCESS, kr) << "mach_timebase_info";
51b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
52b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
53b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // mach_absolute_time is it when it comes to ticks on the Mac.  Other calls
54b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // with less precision (such as TickCount) just call through to
55b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // mach_absolute_time.
56b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
57b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // timebase_info converts absolute time tick units into nanoseconds.  Convert
58b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // to microseconds up front to stave off overflows.
59b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  base::CheckedNumeric<uint64_t> result(
60b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      mach_absolute_time() / base::Time::kNanosecondsPerMicrosecond);
61b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  result *= timebase_info.numer;
62b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  result /= timebase_info.denom;
63b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
64b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Don't bother with the rollover handling that the Windows version does.
65b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // With numer and denom = 1 (the expected case), the 64-bit absolute time
66b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // reported in nanoseconds is enough to last nearly 585 years.
67b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return base::checked_cast<int64_t>(result.ValueOrDie());
68b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif  // defined(OS_IOS)
69b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
70b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
71b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratint64_t ComputeThreadTicks() {
72b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#if defined(OS_IOS)
73b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  NOTREACHED();
74b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return 0;
75b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#else
76b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  base::mac::ScopedMachSendRight thread(mach_thread_self());
77b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  mach_msg_type_number_t thread_info_count = THREAD_BASIC_INFO_COUNT;
78b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  thread_basic_info_data_t thread_info_data;
79b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
80b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (thread.get() == MACH_PORT_NULL) {
81b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    DLOG(ERROR) << "Failed to get mach_thread_self()";
82b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return 0;
83b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  }
84b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
85b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  kern_return_t kr = thread_info(
86b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      thread,
87b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      THREAD_BASIC_INFO,
88b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      reinterpret_cast<thread_info_t>(&thread_info_data),
89b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      &thread_info_count);
90b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  MACH_DCHECK(kr == KERN_SUCCESS, kr) << "thread_info";
91b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
92b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  base::CheckedNumeric<int64_t> absolute_micros(
93b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      thread_info_data.user_time.seconds);
94b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  absolute_micros *= base::Time::kMicrosecondsPerSecond;
95b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  absolute_micros += thread_info_data.user_time.microseconds;
96b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return absolute_micros.ValueOrDie();
97b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat#endif  // defined(OS_IOS)
98b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
99b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
100b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}  // namespace
101b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
102b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratnamespace base {
103b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
104b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// The Time routines in this file use Mach and CoreFoundation APIs, since the
105b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// POSIX definition of time_t in Mac OS X wraps around after 2038--and
106b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// there are already cookie expiration dates, etc., past that time out in
107b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// the field.  Using CFDate prevents that problem, and using mach_absolute_time
108b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// for TimeTicks gives us nice high-resolution interval timing.
109b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
110b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Time -----------------------------------------------------------------------
111b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
112b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Core Foundation uses a double second count since 2001-01-01 00:00:00 UTC.
113b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// The UNIX epoch is 1970-01-01 00:00:00 UTC.
114b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Windows uses a Gregorian epoch of 1601.  We need to match this internally
115b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// so that our time representations match across all platforms.  See bug 14734.
116b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//   irb(main):010:0> Time.at(0).getutc()
117b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//   => Thu Jan 01 00:00:00 UTC 1970
118b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//   irb(main):011:0> Time.at(-11644473600).getutc()
119b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat//   => Mon Jan 01 00:00:00 UTC 1601
120b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratstatic const int64 kWindowsEpochDeltaSeconds = INT64_C(11644473600);
121b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
122b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
123b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratconst int64 Time::kWindowsEpochDeltaMicroseconds =
124b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond;
125b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
126b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// Some functions in time.cc use time_t directly, so we provide an offset
127b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// to convert from time_t (Unix epoch) and internal (Windows epoch).
128b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
129b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratconst int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds;
130b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
131b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
132b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTime Time::Now() {
133b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return FromCFAbsoluteTime(CFAbsoluteTimeGetCurrent());
134b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
135b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
136b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
137b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTime Time::FromCFAbsoluteTime(CFAbsoluteTime t) {
138b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  COMPILE_ASSERT(std::numeric_limits<CFAbsoluteTime>::has_infinity,
139b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                 numeric_limits_infinity_is_undefined_when_not_has_infinity);
140b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (t == 0)
141b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return Time();  // Consider 0 as a null Time.
142b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (t == std::numeric_limits<CFAbsoluteTime>::infinity())
143b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return Max();
144b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return Time(static_cast<int64>(
145b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      (t + kCFAbsoluteTimeIntervalSince1970) * kMicrosecondsPerSecond) +
146b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      kWindowsEpochDeltaMicroseconds);
147b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
148b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
149b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratCFAbsoluteTime Time::ToCFAbsoluteTime() const {
150b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  COMPILE_ASSERT(std::numeric_limits<CFAbsoluteTime>::has_infinity,
151b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                 numeric_limits_infinity_is_undefined_when_not_has_infinity);
152b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (is_null())
153b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return 0;  // Consider 0 as a null Time.
154b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (is_max())
155b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    return std::numeric_limits<CFAbsoluteTime>::infinity();
156b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return (static_cast<CFAbsoluteTime>(us_ - kWindowsEpochDeltaMicroseconds) /
157b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      kMicrosecondsPerSecond) - kCFAbsoluteTimeIntervalSince1970;
158b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
159b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
160b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
161b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTime Time::NowFromSystemTime() {
162b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Just use Now() because Now() returns the system time.
163b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return Now();
164b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
165b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
166b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
167b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTime Time::FromExploded(bool is_local, const Exploded& exploded) {
168b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  CFGregorianDate date;
169b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  date.second = exploded.second +
170b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      exploded.millisecond / static_cast<double>(kMillisecondsPerSecond);
171b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  date.minute = exploded.minute;
172b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  date.hour = exploded.hour;
173b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  date.day = exploded.day_of_month;
174b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  date.month = exploded.month;
175b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  date.year = exploded.year;
176b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
177b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  base::ScopedCFTypeRef<CFTimeZoneRef> time_zone(
178b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      is_local ? CFTimeZoneCopySystem() : NULL);
179b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  CFAbsoluteTime seconds = CFGregorianDateGetAbsoluteTime(date, time_zone) +
180b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      kCFAbsoluteTimeIntervalSince1970;
181b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return Time(static_cast<int64>(seconds * kMicrosecondsPerSecond) +
182b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      kWindowsEpochDeltaMicroseconds);
183b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
184b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
185b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratvoid Time::Explode(bool is_local, Exploded* exploded) const {
186b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Avoid rounding issues, by only putting the integral number of seconds
187b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // (rounded towards -infinity) into a |CFAbsoluteTime| (which is a |double|).
188b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  int64 microsecond = us_ % kMicrosecondsPerSecond;
189b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  if (microsecond < 0)
190b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat    microsecond += kMicrosecondsPerSecond;
191b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  CFAbsoluteTime seconds = ((us_ - microsecond) / kMicrosecondsPerSecond) -
192b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                           kWindowsEpochDeltaSeconds -
193b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                           kCFAbsoluteTimeIntervalSince1970;
194b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
195b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  base::ScopedCFTypeRef<CFTimeZoneRef> time_zone(
196b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      is_local ? CFTimeZoneCopySystem() : NULL);
197b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  CFGregorianDate date = CFAbsoluteTimeGetGregorianDate(seconds, time_zone);
198b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // 1 = Monday, ..., 7 = Sunday.
199b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  int cf_day_of_week = CFAbsoluteTimeGetDayOfWeek(seconds, time_zone);
200b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
201b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->year = date.year;
202b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->month = date.month;
203b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->day_of_week = cf_day_of_week % 7;
204b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->day_of_month = date.day;
205b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->hour = date.hour;
206b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->minute = date.minute;
207b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Make sure seconds are rounded down towards -infinity.
208b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->second = floor(date.second);
209b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // Calculate milliseconds ourselves, since we rounded the |seconds|, making
210b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  // sure to round towards -infinity.
211b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  exploded->millisecond =
212b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat      (microsecond >= 0) ? microsecond / kMicrosecondsPerMillisecond :
213b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                           (microsecond - kMicrosecondsPerMillisecond + 1) /
214b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat                               kMicrosecondsPerMillisecond;
215b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
216b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
217b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// TimeTicks ------------------------------------------------------------------
218b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
219b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
220b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTimeTicks TimeTicks::Now() {
221b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return TimeTicks(ComputeCurrentTicks());
222b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
223b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
224b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
225b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Eratbool TimeTicks::IsHighResolution() {
226b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return true;
227b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
228b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
229b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
230b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratThreadTicks ThreadTicks::Now() {
231b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return ThreadTicks(ComputeThreadTicks());
232b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
233b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
234b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat// static
235b8cf94937c52feb53b55c39e3f82094d27de464cDaniel EratTraceTicks TraceTicks::Now() {
236b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat  return TraceTicks(ComputeCurrentTicks());
237b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}
238b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat
239b8cf94937c52feb53b55c39e3f82094d27de464cDaniel Erat}  // namespace base
240