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__) |
11 #include <time64.h> | 11 #include <time64.h> |
12 #endif | 12 #endif |
13 #include <unistd.h> | 13 #include <unistd.h> |
14 | 14 |
15 #include <limits> | 15 #include <limits> |
16 #include <ostream> | 16 #include <ostream> |
17 | 17 |
18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
20 #include "base/port.h" | 20 #include "base/port.h" |
21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
22 | 22 |
23 #if defined(OS_ANDROID) | 23 #if defined(OS_ANDROID) |
24 #include "base/os_compat_android.h" | 24 #include "base/os_compat_android.h" |
25 #elif defined(OS_NACL) | 25 #elif defined(OS_NACL) |
26 #include "base/os_compat_nacl.h" | 26 #include "base/os_compat_nacl.h" |
27 #endif | 27 #endif |
28 | 28 |
| 29 #if !defined(OS_MACOSX) |
| 30 #include "base/lazy_instance.h" |
| 31 #include "base/synchronization/lock.h" |
| 32 #endif |
| 33 |
29 namespace { | 34 namespace { |
30 | 35 |
31 #if !defined(OS_MACOSX) | 36 #if !defined(OS_MACOSX) |
| 37 // This prevents a crash on traversing the environment global and looking up |
| 38 // the 'TZ' variable in libc. See: crbug.com/390567. |
| 39 base::LazyInstance<base::Lock>::Leaky |
| 40 g_sys_time_to_time_struct_lock = LAZY_INSTANCE_INITIALIZER; |
| 41 |
32 // Define a system-specific SysTime that wraps either to a time_t or | 42 // Define a system-specific SysTime that wraps either to a time_t or |
33 // a time64_t depending on the host system, and associated convertion. | 43 // a time64_t depending on the host system, and associated convertion. |
34 // See crbug.com/162007 | 44 // See crbug.com/162007 |
35 #if defined(OS_ANDROID) && !defined(__LP64__) | 45 #if defined(OS_ANDROID) && !defined(__LP64__) |
36 typedef time64_t SysTime; | 46 typedef time64_t SysTime; |
37 | 47 |
38 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { | 48 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { |
| 49 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); |
39 if (is_local) | 50 if (is_local) |
40 return mktime64(timestruct); | 51 return mktime64(timestruct); |
41 else | 52 else |
42 return timegm64(timestruct); | 53 return timegm64(timestruct); |
43 } | 54 } |
44 | 55 |
45 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(g_sys_time_to_time_struct_lock.Get()); |
46 if (is_local) | 58 if (is_local) |
47 localtime64_r(&t, timestruct); | 59 localtime64_r(&t, timestruct); |
48 else | 60 else |
49 gmtime64_r(&t, timestruct); | 61 gmtime64_r(&t, timestruct); |
50 } | 62 } |
51 | 63 |
52 #else // OS_ANDROID && !__LP64__ | 64 #else // OS_ANDROID && !__LP64__ |
53 typedef time_t SysTime; | 65 typedef time_t SysTime; |
54 | 66 |
55 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { | 67 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { |
| 68 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); |
56 if (is_local) | 69 if (is_local) |
57 return mktime(timestruct); | 70 return mktime(timestruct); |
58 else | 71 else |
59 return timegm(timestruct); | 72 return timegm(timestruct); |
60 } | 73 } |
61 | 74 |
62 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { | 75 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { |
| 76 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); |
63 if (is_local) | 77 if (is_local) |
64 localtime_r(&t, timestruct); | 78 localtime_r(&t, timestruct); |
65 else | 79 else |
66 gmtime_r(&t, timestruct); | 80 gmtime_r(&t, timestruct); |
67 } | 81 } |
68 #endif // OS_ANDROID | 82 #endif // OS_ANDROID |
69 | 83 |
70 // Helper function to get results from clock_gettime() as TimeTicks object. | 84 // Helper function to get results from clock_gettime() as TimeTicks object. |
71 // Minimum requirement is MONOTONIC_CLOCK to be supported on the system. | 85 // Minimum requirement is MONOTONIC_CLOCK to be supported on the system. |
72 // FreeBSD 6 has CLOCK_MONOTONIC but defines _POSIX_MONOTONIC_CLOCK to -1. | 86 // FreeBSD 6 has CLOCK_MONOTONIC but defines _POSIX_MONOTONIC_CLOCK to -1. |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 391 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
378 return result; | 392 return result; |
379 } | 393 } |
380 int64 us = us_ - kTimeTToMicrosecondsOffset; | 394 int64 us = us_ - kTimeTToMicrosecondsOffset; |
381 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 395 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
382 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 396 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
383 return result; | 397 return result; |
384 } | 398 } |
385 | 399 |
386 } // namespace base | 400 } // namespace base |
OLD | NEW |