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