| 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.h" | 5 #include "base/time.h" |
| 6 | 6 |
| 7 #include <sys/time.h> | 7 #include <sys/time.h> |
| 8 #include <time.h> | 8 #include <time.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // to convert from time_t (Unix epoch) and internal (Windows epoch). | 63 // to convert from time_t (Unix epoch) and internal (Windows epoch). |
| 64 // static | 64 // static |
| 65 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; | 65 const int64 Time::kTimeTToMicrosecondsOffset = kWindowsEpochDeltaMicroseconds; |
| 66 | 66 |
| 67 // static | 67 // static |
| 68 Time Time::Now() { | 68 Time Time::Now() { |
| 69 struct timeval tv; | 69 struct timeval tv; |
| 70 struct timezone tz = { 0, 0 }; // UTC | 70 struct timezone tz = { 0, 0 }; // UTC |
| 71 if (gettimeofday(&tv, &tz) != 0) { | 71 if (gettimeofday(&tv, &tz) != 0) { |
| 72 DCHECK(0) << "Could not determine time of day"; | 72 DCHECK(0) << "Could not determine time of day"; |
| 73 LOG_ERRNO(ERROR) << "Call to gettimeofday failed."; |
| 74 // Return null instead of uninitialized |tv| value, which contains random |
| 75 // garbage data. This may result in the crash seen in crbug.com/147570. |
| 76 return Time(); |
| 73 } | 77 } |
| 74 // Combine seconds and microseconds in a 64-bit field containing microseconds | 78 // Combine seconds and microseconds in a 64-bit field containing microseconds |
| 75 // since the epoch. That's enough for nearly 600 centuries. Adjust from | 79 // since the epoch. That's enough for nearly 600 centuries. Adjust from |
| 76 // Unix (1970) to Windows (1601) epoch. | 80 // Unix (1970) to Windows (1601) epoch. |
| 77 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + | 81 return Time((tv.tv_sec * kMicrosecondsPerSecond + tv.tv_usec) + |
| 78 kWindowsEpochDeltaMicroseconds); | 82 kWindowsEpochDeltaMicroseconds); |
| 79 } | 83 } |
| 80 | 84 |
| 81 // static | 85 // static |
| 82 Time Time::NowFromSystemTime() { | 86 Time Time::NowFromSystemTime() { |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | 293 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
| 290 return result; | 294 return result; |
| 291 } | 295 } |
| 292 int64 us = us_ - kTimeTToMicrosecondsOffset; | 296 int64 us = us_ - kTimeTToMicrosecondsOffset; |
| 293 result.tv_sec = us / Time::kMicrosecondsPerSecond; | 297 result.tv_sec = us / Time::kMicrosecondsPerSecond; |
| 294 result.tv_usec = us % Time::kMicrosecondsPerSecond; | 298 result.tv_usec = us % Time::kMicrosecondsPerSecond; |
| 295 return result; | 299 return result; |
| 296 } | 300 } |
| 297 | 301 |
| 298 } // namespace base | 302 } // namespace base |
| OLD | NEW |