Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
Lei Zhang
2017/05/17 08:34:17
No (c) and 2017 since this is a new file?
miu
2017/05/18 02:52:38
Even with git similarity at 1%, I was unable to ma
Lei Zhang
2017/05/18 19:06:12
It's not a big deal either way. I usually just loo
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/time/time.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <sys/time.h> | |
| 9 #include <time.h> | |
| 10 | |
| 11 #include <limits> | |
| 12 | |
| 13 #include "base/logging.h" | |
| 14 | |
| 15 namespace base { | |
| 16 | |
| 17 // static | |
| 18 TimeDelta TimeDelta::FromTimeSpec(const timespec& ts) { | |
| 19 return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond + | |
| 20 ts.tv_nsec / Time::kNanosecondsPerMicrosecond); | |
| 21 } | |
| 22 | |
| 23 struct timespec TimeDelta::ToTimeSpec() const { | |
| 24 int64_t microseconds = InMicroseconds(); | |
| 25 time_t seconds = 0; | |
| 26 if (microseconds >= Time::kMicrosecondsPerSecond) { | |
| 27 seconds = InSeconds(); | |
| 28 microseconds -= seconds * Time::kMicrosecondsPerSecond; | |
| 29 } | |
| 30 struct timespec result = { | |
| 31 seconds, | |
| 32 static_cast<long>(microseconds * Time::kNanosecondsPerMicrosecond)}; | |
| 33 return result; | |
| 34 } | |
| 35 | |
| 36 // static | |
| 37 Time Time::FromTimeVal(struct timeval t) { | |
| 38 DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond)); | |
| 39 DCHECK_GE(t.tv_usec, 0); | |
| 40 if (t.tv_usec == 0 && t.tv_sec == 0) | |
| 41 return Time(); | |
| 42 if (t.tv_usec == static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1 && | |
| 43 t.tv_sec == std::numeric_limits<time_t>::max()) | |
| 44 return Max(); | |
| 45 return Time((static_cast<int64_t>(t.tv_sec) * Time::kMicrosecondsPerSecond) + | |
| 46 t.tv_usec + kTimeTToMicrosecondsOffset); | |
| 47 } | |
| 48 | |
| 49 struct timeval Time::ToTimeVal() const { | |
| 50 struct timeval result; | |
| 51 if (is_null()) { | |
| 52 result.tv_sec = 0; | |
| 53 result.tv_usec = 0; | |
| 54 return result; | |
| 55 } | |
| 56 if (is_max()) { | |
| 57 result.tv_sec = std::numeric_limits<time_t>::max(); | |
| 58 result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; | |
| 59 return result; | |
| 60 } | |
| 61 int64_t us = us_ - kTimeTToMicrosecondsOffset; | |
| 62 result.tv_sec = us / Time::kMicrosecondsPerSecond; | |
| 63 result.tv_usec = us % Time::kMicrosecondsPerSecond; | |
| 64 return result; | |
| 65 } | |
| 66 | |
| 67 } // namespace base | |
| OLD | NEW |