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

Side by Side Diff: base/time_posix.cc

Issue 5434001: Help make Base compile under NaCl... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years 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 | « no previous file | no next file » | 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) 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
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(&timestruct); 102 seconds = mktime(&timestruct);
81 else 103 else
104 #if defined(OS_NACL)
105 seconds = my_timegm(&timestruct);
106 #else
82 seconds = timegm(&timestruct); 107 seconds = timegm(&timestruct);
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
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
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
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698