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