OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <sys/time.h> | 8 #include <sys/time.h> |
9 #include <time.h> | 9 #include <time.h> |
10 #if defined(OS_ANDROID) && !defined(__LP64__) | 10 #if defined(OS_ANDROID) && !defined(__LP64__) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 } | 54 } |
55 | 55 |
56 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { | 56 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { |
57 base::AutoLock locked(*GetSysTimeToTimeStructLock()); | 57 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
58 if (is_local) | 58 if (is_local) |
59 localtime64_r(&t, timestruct); | 59 localtime64_r(&t, timestruct); |
60 else | 60 else |
61 gmtime64_r(&t, timestruct); | 61 gmtime64_r(&t, timestruct); |
62 } | 62 } |
63 | 63 |
| 64 #elif defined(OS_AIX) |
| 65 |
| 66 // timegm is not available on AIX |
| 67 // we should perhaps use a more portable alternative |
| 68 time_t aix_timegm(struct tm* tm) { |
| 69 time_t ret; |
| 70 char* tz; |
| 71 |
| 72 tz = getenv("TZ"); |
| 73 if (tz) { |
| 74 tz = strdup(tz); |
| 75 } |
| 76 setenv("TZ", "GMT0", 1); |
| 77 tzset(); |
| 78 ret = mktime(tm); |
| 79 if (tz) { |
| 80 setenv("TZ", tz, 1); |
| 81 free(tz); |
| 82 } else { |
| 83 unsetenv("TZ"); |
| 84 } |
| 85 tzset(); |
| 86 return ret; |
| 87 } |
| 88 |
| 89 typedef time_t SysTime; |
| 90 |
| 91 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { |
| 92 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
| 93 if (is_local) |
| 94 return mktime(timestruct); |
| 95 else |
| 96 return aix_timegm(timestruct); |
| 97 } |
| 98 |
| 99 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { |
| 100 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
| 101 if (is_local) |
| 102 localtime_r(&t, timestruct); |
| 103 else |
| 104 gmtime_r(&t, timestruct); |
| 105 } |
| 106 |
64 #else // OS_ANDROID && !__LP64__ | 107 #else // OS_ANDROID && !__LP64__ |
65 typedef time_t SysTime; | 108 typedef time_t SysTime; |
66 | 109 |
67 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { | 110 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { |
68 base::AutoLock locked(*GetSysTimeToTimeStructLock()); | 111 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
69 if (is_local) | 112 if (is_local) |
70 return mktime(timestruct); | 113 return mktime(timestruct); |
71 else | 114 else |
72 return timegm(timestruct); | 115 return timegm(timestruct); |
73 } | 116 } |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 struct tm timestruct; | 284 struct tm timestruct; |
242 timestruct.tm_sec = exploded.second; | 285 timestruct.tm_sec = exploded.second; |
243 timestruct.tm_min = exploded.minute; | 286 timestruct.tm_min = exploded.minute; |
244 timestruct.tm_hour = exploded.hour; | 287 timestruct.tm_hour = exploded.hour; |
245 timestruct.tm_mday = exploded.day_of_month; | 288 timestruct.tm_mday = exploded.day_of_month; |
246 timestruct.tm_mon = month.ValueOrDie(); | 289 timestruct.tm_mon = month.ValueOrDie(); |
247 timestruct.tm_year = year.ValueOrDie(); | 290 timestruct.tm_year = year.ValueOrDie(); |
248 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this | 291 timestruct.tm_wday = exploded.day_of_week; // mktime/timegm ignore this |
249 timestruct.tm_yday = 0; // mktime/timegm ignore this | 292 timestruct.tm_yday = 0; // mktime/timegm ignore this |
250 timestruct.tm_isdst = -1; // attempt to figure it out | 293 timestruct.tm_isdst = -1; // attempt to figure it out |
251 #if !defined(OS_NACL) && !defined(OS_SOLARIS) | 294 #if !defined(OS_NACL) && !defined(OS_SOLARIS) && !defined(OS_AIX) |
252 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore | 295 timestruct.tm_gmtoff = 0; // not a POSIX field, so mktime/timegm ignore |
253 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore | 296 timestruct.tm_zone = NULL; // not a POSIX field, so mktime/timegm ignore |
254 #endif | 297 #endif |
255 | 298 |
256 SysTime seconds; | 299 SysTime seconds; |
257 | 300 |
258 // Certain exploded dates do not really exist due to daylight saving times, | 301 // Certain exploded dates do not really exist due to daylight saving times, |
259 // and this causes mktime() to return implementation-defined values when | 302 // and this causes mktime() to return implementation-defined values when |
260 // tm_isdst is set to -1. On Android, the function will return -1, while the | 303 // tm_isdst is set to -1. On Android, the function will return -1, while the |
261 // C libraries of other platforms typically return a liberally-chosen value. | 304 // C libraries of other platforms typically return a liberally-chosen value. |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 463 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
421 return result; | 464 return result; |
422 } | 465 } |
423 int64_t us = us_ - kTimeTToMicrosecondsOffset; | 466 int64_t us = us_ - kTimeTToMicrosecondsOffset; |
424 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 467 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
425 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 468 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
426 return result; | 469 return result; |
427 } | 470 } |
428 | 471 |
429 } // namespace base | 472 } // namespace base |
OLD | NEW |