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/logging.h" | 18 #include "base/logging.h" |
19 #include "base/numerics/safe_math.h" | 19 #include "base/numerics/safe_math.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 | 21 |
22 #if defined(OS_ANDROID) | 22 #if defined(OS_ANDROID) |
23 #include "base/os_compat_android.h" | 23 #include "base/os_compat_android.h" |
24 #elif defined(OS_NACL) | 24 #elif defined(OS_NACL) |
25 #include "base/os_compat_nacl.h" | 25 #include "base/os_compat_nacl.h" |
26 #endif | 26 #endif |
27 | 27 |
28 #if !defined(OS_MACOSX) | 28 #if !defined(OS_MACOSX) |
29 #include "base/lazy_instance.h" | |
30 #include "base/synchronization/lock.h" | 29 #include "base/synchronization/lock.h" |
31 #endif | 30 #endif |
32 | 31 |
33 namespace { | 32 namespace { |
34 | 33 |
35 #if !defined(OS_MACOSX) | 34 #if !defined(OS_MACOSX) |
36 // This prevents a crash on traversing the environment global and looking up | 35 // This prevents a crash on traversing the environment global and looking up |
37 // the 'TZ' variable in libc. See: crbug.com/390567. | 36 // the 'TZ' variable in libc. See: crbug.com/390567. |
38 base::LazyInstance<base::Lock>::Leaky | 37 base::Lock* GetSysTimeToTimeStructLock() { |
39 g_sys_time_to_time_struct_lock = LAZY_INSTANCE_INITIALIZER; | 38 static auto lock = new base::Lock(); |
| 39 return lock; |
| 40 } |
40 | 41 |
41 // 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 |
42 // a time64_t depending on the host system, and associated convertion. | 43 // a time64_t depending on the host system, and associated convertion. |
43 // See crbug.com/162007 | 44 // See crbug.com/162007 |
44 #if defined(OS_ANDROID) && !defined(__LP64__) | 45 #if defined(OS_ANDROID) && !defined(__LP64__) |
45 typedef time64_t SysTime; | 46 typedef time64_t SysTime; |
46 | 47 |
47 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { | 48 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { |
48 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); | 49 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
49 if (is_local) | 50 if (is_local) |
50 return mktime64(timestruct); | 51 return mktime64(timestruct); |
51 else | 52 else |
52 return timegm64(timestruct); | 53 return timegm64(timestruct); |
53 } | 54 } |
54 | 55 |
55 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { | 56 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { |
56 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); | 57 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
57 if (is_local) | 58 if (is_local) |
58 localtime64_r(&t, timestruct); | 59 localtime64_r(&t, timestruct); |
59 else | 60 else |
60 gmtime64_r(&t, timestruct); | 61 gmtime64_r(&t, timestruct); |
61 } | 62 } |
62 | 63 |
63 #else // OS_ANDROID && !__LP64__ | 64 #else // OS_ANDROID && !__LP64__ |
64 typedef time_t SysTime; | 65 typedef time_t SysTime; |
65 | 66 |
66 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { | 67 SysTime SysTimeFromTimeStruct(struct tm* timestruct, bool is_local) { |
67 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); | 68 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
68 if (is_local) | 69 if (is_local) |
69 return mktime(timestruct); | 70 return mktime(timestruct); |
70 else | 71 else |
71 return timegm(timestruct); | 72 return timegm(timestruct); |
72 } | 73 } |
73 | 74 |
74 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { | 75 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { |
75 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); | 76 base::AutoLock locked(*GetSysTimeToTimeStructLock()); |
76 if (is_local) | 77 if (is_local) |
77 localtime_r(&t, timestruct); | 78 localtime_r(&t, timestruct); |
78 else | 79 else |
79 gmtime_r(&t, timestruct); | 80 gmtime_r(&t, timestruct); |
80 } | 81 } |
81 #endif // OS_ANDROID | 82 #endif // OS_ANDROID |
82 | 83 |
83 int64_t ConvertTimespecToMicros(const struct timespec& ts) { | 84 int64_t ConvertTimespecToMicros(const struct timespec& ts) { |
84 // On 32-bit systems, the calculation cannot overflow int64_t. | 85 // On 32-bit systems, the calculation cannot overflow int64_t. |
85 // 2**32 * 1000000 + 2**64 / 1000 < 2**63 | 86 // 2**32 * 1000000 + 2**64 / 1000 < 2**63 |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 420 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
420 return result; | 421 return result; |
421 } | 422 } |
422 int64_t us = us_ - kTimeTToMicrosecondsOffset; | 423 int64_t us = us_ - kTimeTToMicrosecondsOffset; |
423 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 424 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
424 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 425 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
425 return result; | 426 return result; |
426 } | 427 } |
427 | 428 |
428 } // namespace base | 429 } // namespace base |
OLD | NEW |