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