Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(712)

Side by Side Diff: base/time/time_posix.cc

Issue 2361803002: Avoid unnecessary checking in getting current time on 32-bit systems. (Closed)
Patch Set: update comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) { 73 void SysTimeToTimeStruct(SysTime t, struct tm* timestruct, bool is_local) {
74 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get()); 74 base::AutoLock locked(g_sys_time_to_time_struct_lock.Get());
75 if (is_local) 75 if (is_local)
76 localtime_r(&t, timestruct); 76 localtime_r(&t, timestruct);
77 else 77 else
78 gmtime_r(&t, timestruct); 78 gmtime_r(&t, timestruct);
79 } 79 }
80 #endif // OS_ANDROID 80 #endif // OS_ANDROID
81 81
82 int64_t ConvertTimespecToMicros(const struct timespec& ts) { 82 int64_t ConvertTimespecToMicros(const struct timespec& ts) {
83 base::CheckedNumeric<int64_t> result(ts.tv_sec); 83 // On 32-bit systems, the calculation cannot overflow int64_t.
84 result *= base::Time::kMicrosecondsPerSecond; 84 // 2**32 * 1000000 + 2**64 / 1000 < 2**63
85 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond); 85 if (sizeof(ts.tv_sec) <= 4 && sizeof(ts.tv_nsec) <= 8) {
86 return result.ValueOrDie(); 86 int64_t result = ts.tv_sec;
87 result *= base::Time::kMicrosecondsPerSecond;
88 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond);
89 return result;
90 } else {
91 base::CheckedNumeric<int64_t> result(ts.tv_sec);
92 result *= base::Time::kMicrosecondsPerSecond;
93 result += (ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond);
94 return result.ValueOrDie();
95 }
87 } 96 }
88 97
89 // Helper function to get results from clock_gettime() and convert to a 98 // Helper function to get results from clock_gettime() and convert to a
90 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported 99 // microsecond timebase. Minimum requirement is MONOTONIC_CLOCK to be supported
91 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines 100 // on the system. FreeBSD 6 has CLOCK_MONOTONIC but defines
92 // _POSIX_MONOTONIC_CLOCK to -1. 101 // _POSIX_MONOTONIC_CLOCK to -1.
93 #if (defined(OS_POSIX) && \ 102 #if (defined(OS_POSIX) && \
94 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \ 103 defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0) || \
95 defined(OS_BSD) || defined(OS_ANDROID) 104 defined(OS_BSD) || defined(OS_ANDROID)
96 int64_t ClockNow(clockid_t clk_id) { 105 int64_t ClockNow(clockid_t clk_id) {
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; 391 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1;
383 return result; 392 return result;
384 } 393 }
385 int64_t us = us_ - kTimeTToMicrosecondsOffset; 394 int64_t us = us_ - kTimeTToMicrosecondsOffset;
386 result.tv_sec = us / Time::kMicrosecondsPerSecond; 395 result.tv_sec = us / Time::kMicrosecondsPerSecond;
387 result.tv_usec = us % Time::kMicrosecondsPerSecond; 396 result.tv_usec = us % Time::kMicrosecondsPerSecond;
388 return result; 397 return result;
389 } 398 }
390 399
391 } // namespace base 400 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698