Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: base/time_posix.cc

Issue 173296: Convert internal time format to Windows 1601 epoch on Linux & Mac.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/time_mac.cc ('k') | base/time_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/time.h" 5 #include "base/time.h"
6 6
7 #ifdef OS_MACOSX
8 #include <mach/mach_time.h>
9 #endif
10 #include <sys/time.h> 7 #include <sys/time.h>
11 #include <time.h> 8 #include <time.h>
12 9
13 #include <limits> 10 #include <limits>
14 11
15 #include "base/basictypes.h" 12 #include "base/basictypes.h"
16 #include "base/logging.h" 13 #include "base/logging.h"
17 14
18 namespace base { 15 namespace base {
19 16
20 // The Time routines in this file use standard POSIX routines, or almost- 17 // The Time routines in this file use standard POSIX routines, or almost-
21 // standard routines in the case of timegm. We need to use a Mach-specific 18 // standard routines in the case of timegm. We need to use a Mach-specific
22 // function for TimeTicks::Now() on Mac OS X. 19 // function for TimeTicks::Now() on Mac OS X.
23 20
24 // Time ----------------------------------------------------------------------- 21 // Time -----------------------------------------------------------------------
25 22
26 // Some functions in time.cc use time_t directly, so we provide a zero offset 23 // Windows uses a Gregorian epoch of 1601. We need to match this internally
27 // for them. The epoch is 1970-01-01 00:00:00 UTC. 24 // so that our time representations match across all platforms. See bug 14734.
25 // irb(main):010:0> Time.at(0).getutc()
26 // => Thu Jan 01 00:00:00 UTC 1970
27 // irb(main):011:0> Time.at(-11644473600).getutc()
28 // => Mon Jan 01 00:00:00 UTC 1601
29 static const int64 kWindowsEpochDeltaSeconds = GG_INT64_C(11644473600);
30 static const int64 kWindowsEpochDeltaMilliseconds =
31 kWindowsEpochDeltaSeconds * Time::kMillisecondsPerSecond;
32
28 // static 33 // static
29 const int64 Time::kTimeTToMicrosecondsOffset = GG_INT64_C(0); 34 const int64 Time::kWindowsEpochDeltaMicroseconds =
35 kWindowsEpochDeltaSeconds * Time::kMicrosecondsPerSecond;
36
37 // Some functions in time.cc use time_t directly, so we provide an offset
38 // to convert from time_t (Unix epoch) and internal (Windows epoch).
39 // static
40 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds;
30 41
31 // static 42 // static
32 Time Time::Now() { 43 Time Time::Now() {
33 struct timeval tv; 44 struct timeval tv;
34 struct timezone tz = { 0, 0 }; // UTC 45 struct timezone tz = { 0, 0 }; // UTC
35 if (gettimeofday(&tv, &tz) != 0) { 46 if (gettimeofday(&tv, &tz) != 0) {
36 DCHECK(0) << "Could not determine time of day"; 47 DCHECK(0) << "Could not determine time of day";
37 } 48 }
38 // Combine seconds and microseconds in a 64-bit field containing microseconds 49 // Combine seconds and microseconds in a 64-bit field containing microseconds
39 // since the epoch. That's enough for nearly 600 centuries. 50 // since the epoch. That's enough for nearly 600 centuries. Adjust from
40 return tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec; 51 // Unix (1970) to Windows (1601) epoch.
52 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) +
53 kWindowsEpochDeltaMicroseconds);
41 } 54 }
42 55
43 // static 56 // static
44 Time Time::NowFromSystemTime() { 57 Time Time::NowFromSystemTime() {
45 // Just use Now() because Now() returns the system time. 58 // Just use Now() because Now() returns the system time.
46 return Now(); 59 return Now();
47 } 60 }
48 61
49 // static 62 // static
50 Time Time::FromExploded(bool is_local, const Exploded& exploded) { 63 Time Time::FromExploded(bool is_local, const Exploded& exploded) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 kMillisecondsPerSecond; 106 kMillisecondsPerSecond;
94 } else { 107 } else {
95 milliseconds = (std::numeric_limits<time_t>::max() * 108 milliseconds = (std::numeric_limits<time_t>::max() *
96 kMillisecondsPerSecond) + 109 kMillisecondsPerSecond) +
97 kMillisecondsPerSecond - 1; 110 kMillisecondsPerSecond - 1;
98 } 111 }
99 } else { 112 } else {
100 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond; 113 milliseconds = seconds * kMillisecondsPerSecond + exploded.millisecond;
101 } 114 }
102 115
103 return Time(milliseconds * kMicrosecondsPerMillisecond); 116 // Adjust from Unix (1970) to Windows (1601) epoch.
117 return Time((milliseconds * kMicrosecondsPerMillisecond) +
118 kWindowsEpochDeltaMicroseconds);
104 } 119 }
105 120
106 void Time::Explode(bool is_local, Exploded* exploded) const { 121 void Time::Explode(bool is_local, Exploded* exploded) const {
107 // Time stores times with microsecond resolution, but Exploded only carries 122 // Time stores times with microsecond resolution, but Exploded only carries
108 // millisecond resolution, so begin by being lossy. 123 // millisecond resolution, so begin by being lossy. Adjust from Windows
109 int64 milliseconds = us_ / kMicrosecondsPerMillisecond; 124 // epoch (1601) to Unix epoch (1970);
125 int64 milliseconds = (us_ - kWindowsEpochDeltaMicroseconds) /
126 kMicrosecondsPerMillisecond;
110 time_t seconds = milliseconds / kMillisecondsPerSecond; 127 time_t seconds = milliseconds / kMillisecondsPerSecond;
111 128
112 struct tm timestruct; 129 struct tm timestruct;
113 if (is_local) 130 if (is_local)
114 localtime_r(&seconds, &timestruct); 131 localtime_r(&seconds, &timestruct);
115 else 132 else
116 gmtime_r(&seconds, &timestruct); 133 gmtime_r(&seconds, &timestruct);
117 134
118 exploded->year = timestruct.tm_year + 1900; 135 exploded->year = timestruct.tm_year + 1900;
119 exploded->month = timestruct.tm_mon + 1; 136 exploded->month = timestruct.tm_mon + 1;
120 exploded->day_of_week = timestruct.tm_wday; 137 exploded->day_of_week = timestruct.tm_wday;
121 exploded->day_of_month = timestruct.tm_mday; 138 exploded->day_of_month = timestruct.tm_mday;
122 exploded->hour = timestruct.tm_hour; 139 exploded->hour = timestruct.tm_hour;
123 exploded->minute = timestruct.tm_min; 140 exploded->minute = timestruct.tm_min;
124 exploded->second = timestruct.tm_sec; 141 exploded->second = timestruct.tm_sec;
125 exploded->millisecond = milliseconds % kMillisecondsPerSecond; 142 exploded->millisecond = milliseconds % kMillisecondsPerSecond;
126 } 143 }
127 144
128 // TimeTicks ------------------------------------------------------------------ 145 // TimeTicks ------------------------------------------------------------------
129 146
147 #if defined(OS_POSIX) && \
148 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
149
130 // static 150 // static
131 TimeTicks TimeTicks::Now() { 151 TimeTicks TimeTicks::Now() {
132 uint64_t absolute_micro; 152 uint64_t absolute_micro;
133 153
134 #if defined(OS_MACOSX)
135 static mach_timebase_info_data_t timebase_info;
136 if (timebase_info.denom == 0) {
137 // Zero-initialization of statics guarantees that denom will be 0 before
138 // calling mach_timebase_info. mach_timebase_info will never set denom to
139 // 0 as that would be invalid, so the zero-check can be used to determine
140 // whether mach_timebase_info has already been called. This is
141 // recommended by Apple's QA1398.
142 kern_return_t kr = mach_timebase_info(&timebase_info);
143 DCHECK(kr == KERN_SUCCESS);
144 }
145
146 // mach_absolute_time is it when it comes to ticks on the Mac. Other calls
147 // with less precision (such as TickCount) just call through to
148 // mach_absolute_time.
149
150 // timebase_info converts absolute time tick units into nanoseconds. Convert
151 // to microseconds up front to stave off overflows.
152 absolute_micro = mach_absolute_time() / Time::kNanosecondsPerMicrosecond *
153 timebase_info.numer / timebase_info.denom;
154
155 // Don't bother with the rollover handling that the Windows version does.
156 // With numer and denom = 1 (the expected case), the 64-bit absolute time
157 // reported in nanoseconds is enough to last nearly 585 years.
158
159 #elif defined(OS_POSIX) && \
160 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
161
162 struct timespec ts; 154 struct timespec ts;
163 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { 155 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
164 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed."; 156 NOTREACHED() << "clock_gettime(CLOCK_MONOTONIC) failed.";
165 return TimeTicks(); 157 return TimeTicks();
166 } 158 }
167 159
168 absolute_micro = 160 absolute_micro =
169 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + 161 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) +
170 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); 162 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond);
171 163
164 return TimeTicks(absolute_micro);
165 }
166
172 #else // _POSIX_MONOTONIC_CLOCK 167 #else // _POSIX_MONOTONIC_CLOCK
173 #error No usable tick clock function on this platform. 168 #error No usable tick clock function on this platform.
174 #endif // _POSIX_MONOTONIC_CLOCK 169 #endif // _POSIX_MONOTONIC_CLOCK
175 170
176 return TimeTicks(absolute_micro);
177 }
178
179 // static 171 // static
180 TimeTicks TimeTicks::HighResNow() { 172 TimeTicks TimeTicks::HighResNow() {
181 return Now(); 173 return Now();
182 } 174 }
183 175
184 } // namespace base 176 } // namespace base
OLDNEW
« no previous file with comments | « base/time_mac.cc ('k') | base/time_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698