OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include <sys/time.h> | 7 #include <sys/time.h> |
8 #include <time.h> | 8 #include <time.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + | 53 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + |
54 kWindowsEpochDeltaMicroseconds); | 54 kWindowsEpochDeltaMicroseconds); |
55 } | 55 } |
56 | 56 |
57 // static | 57 // static |
58 Time Time::NowFromSystemTime() { | 58 Time Time::NowFromSystemTime() { |
59 // Just use Now() because Now() returns the system time. | 59 // Just use Now() because Now() returns the system time. |
60 return Now(); | 60 return Now(); |
61 } | 61 } |
62 | 62 |
63 #if defined(OS_NACL) | |
64 #include <stdlib.h> | |
65 // NaCl doesn't have timegm, so we use this hack for now. | |
66 // http://code.google.com/p/nativeclient/issues/detail?id=1160 | |
brettw
2010/12/01 22:59:56
I'd like to at least see a comment here about why
| |
67 static time_t my_timegm (struct tm *tm) { | |
68 time_t ret; | |
69 char *tz; | |
70 tz = getenv("TZ"); | |
71 setenv("TZ", "", 1); | |
72 tzset(); | |
73 ret = mktime(tm); | |
74 if (tz) | |
75 setenv("TZ", tz, 1); | |
76 else | |
77 unsetenv("TZ"); | |
78 tzset(); | |
79 return ret; | |
80 } | |
81 #endif | |
82 | |
63 // static | 83 // static |
64 Time Time::FromExploded(bool is_local, const Exploded& exploded) { | 84 Time Time::FromExploded(bool is_local, const Exploded& exploded) { |
65 struct tm timestruct; | 85 struct tm timestruct; |
66 timestruct.tm_sec = exploded.second; | 86 timestruct.tm_sec = exploded.second; |
67 timestruct.tm_min = exploded.minute; | 87 timestruct.tm_min = exploded.minute; |
68 timestruct.tm_hour = exploded.hour; | 88 timestruct.tm_hour = exploded.hour; |
69 timestruct.tm_mday = exploded.day_of_month; | 89 timestruct.tm_mday = exploded.day_of_month; |
70 timestruct.tm_mon = exploded.month - 1; | 90 timestruct.tm_mon = exploded.month - 1; |
71 timestruct.tm_year = exploded.year - 1900; | 91 timestruct.tm_year = exploded.year - 1900; |
72 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this | 92 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this |
73 timestruct.tm_yday = 0; // mktime/timegm ignore this | 93 timestruct.tm_yday = 0; // mktime/timegm ignore this |
74 timestruct.tm_isdst = -1; // attempt to figure it out | 94 timestruct.tm_isdst = -1; // attempt to figure it out |
95 #if !defined(OS_NACL) | |
75 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore | 96 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore |
76 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore | 97 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore |
98 #endif | |
77 | 99 |
78 time_t seconds; | 100 time_t seconds; |
79 if (is_local) | 101 if (is_local) |
80 seconds = mktime(×truct); | 102 seconds = mktime(×truct); |
81 else | 103 else |
104 #if defined(OS_NACL) | |
105 seconds = my_timegm(×truct); | |
106 #else | |
82 seconds = timegm(×truct); | 107 seconds = timegm(×truct); |
108 #endif | |
83 | 109 |
84 int64 milliseconds; | 110 int64 milliseconds; |
85 // Handle overflow. Clamping the range to what mktime and timegm might | 111 // Handle overflow. Clamping the range to what mktime and timegm might |
86 // return is the best that can be done here. It's not ideal, but it's better | 112 // return is the best that can be done here. It's not ideal, but it's better |
87 // than failing here or ignoring the overflow case and treating each time | 113 // than failing here or ignoring the overflow case and treating each time |
88 // overflow as one second prior to the epoch. | 114 // overflow as one second prior to the epoch. |
89 if (seconds == -1 && | 115 if (seconds == -1 && |
90 (exploded.year < 1969 || exploded.year > 1970)) { | 116 (exploded.year < 1969 || exploded.year > 1970)) { |
91 // If exploded.year is 1969 or 1970, take -1 as correct, with the | 117 // If exploded.year is 1969 or 1970, take -1 as correct, with the |
92 // time indicating 1 second prior to the epoch. (1970 is allowed to handle | 118 // time indicating 1 second prior to the epoch. (1970 is allowed to handle |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 return TimeTicks(); | 185 return TimeTicks(); |
160 } | 186 } |
161 | 187 |
162 absolute_micro = | 188 absolute_micro = |
163 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + | 189 (static_cast<int64>(ts.tv_sec) * Time::kMicrosecondsPerSecond) + |
164 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); | 190 (static_cast<int64>(ts.tv_nsec) / Time::kNanosecondsPerMicrosecond); |
165 | 191 |
166 return TimeTicks(absolute_micro); | 192 return TimeTicks(absolute_micro); |
167 } | 193 } |
168 | 194 |
195 #elif defined(OS_NACL) | |
196 TimeTicks TimeTicks::Now() { | |
197 // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h | |
198 // Apparently NaCl only has CLOCK_REALTIME: | |
199 // http://code.google.com/p/nativeclient/issues/detail?id=1159 | |
200 return TimeTicks(clock()); | |
201 } | |
202 | |
169 #else // _POSIX_MONOTONIC_CLOCK | 203 #else // _POSIX_MONOTONIC_CLOCK |
170 #error No usable tick clock function on this platform. | 204 #error No usable tick clock function on this platform. |
171 #endif // _POSIX_MONOTONIC_CLOCK | 205 #endif // _POSIX_MONOTONIC_CLOCK |
172 | 206 |
173 // static | 207 // static |
174 TimeTicks TimeTicks::HighResNow() { | 208 TimeTicks TimeTicks::HighResNow() { |
175 return Now(); | 209 return Now(); |
176 } | 210 } |
177 | 211 |
178 #endif // !OS_MACOSX | 212 #endif // !OS_MACOSX |
(...skipping 13 matching lines...) Expand all Loading... | |
192 | 226 |
193 struct timeval Time::ToTimeVal() const { | 227 struct timeval Time::ToTimeVal() const { |
194 struct timeval result; | 228 struct timeval result; |
195 int64 us = us_ - kTimeTToMicrosecondsOffset; | 229 int64 us = us_ - kTimeTToMicrosecondsOffset; |
196 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 230 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
197 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 231 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
198 return result; | 232 return result; |
199 } | 233 } |
200 | 234 |
201 } // namespace base | 235 } // namespace base |
OLD | NEW |