Chromium Code Reviews| Index: base/time/time_conversion_posix.cc |
| diff --git a/base/time/time_conversion_posix.cc b/base/time/time_conversion_posix.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ba0a2b29664df07cf35ec8781b897f053ddbe376 |
| --- /dev/null |
| +++ b/base/time/time_conversion_posix.cc |
| @@ -0,0 +1,67 @@ |
| +// 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
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/time/time.h" |
| + |
| +#include <stdint.h> |
| +#include <sys/time.h> |
| +#include <time.h> |
| + |
| +#include <limits> |
| + |
| +#include "base/logging.h" |
| + |
| +namespace base { |
| + |
| +// static |
| +TimeDelta TimeDelta::FromTimeSpec(const timespec& ts) { |
| + return TimeDelta(ts.tv_sec * Time::kMicrosecondsPerSecond + |
| + ts.tv_nsec / Time::kNanosecondsPerMicrosecond); |
| +} |
| + |
| +struct timespec TimeDelta::ToTimeSpec() const { |
| + int64_t microseconds = InMicroseconds(); |
| + time_t seconds = 0; |
| + if (microseconds >= Time::kMicrosecondsPerSecond) { |
| + seconds = InSeconds(); |
| + microseconds -= seconds * Time::kMicrosecondsPerSecond; |
| + } |
| + struct timespec result = { |
| + seconds, |
| + static_cast<long>(microseconds * Time::kNanosecondsPerMicrosecond)}; |
| + return result; |
| +} |
| + |
| +// static |
| +Time Time::FromTimeVal(struct timeval t) { |
| + DCHECK_LT(t.tv_usec, static_cast<int>(Time::kMicrosecondsPerSecond)); |
| + DCHECK_GE(t.tv_usec, 0); |
| + if (t.tv_usec == 0 && t.tv_sec == 0) |
| + return Time(); |
| + if (t.tv_usec == static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1 && |
| + t.tv_sec == std::numeric_limits<time_t>::max()) |
| + return Max(); |
| + return Time((static_cast<int64_t>(t.tv_sec) * Time::kMicrosecondsPerSecond) + |
| + t.tv_usec + kTimeTToMicrosecondsOffset); |
| +} |
| + |
| +struct timeval Time::ToTimeVal() const { |
| + struct timeval result; |
| + if (is_null()) { |
| + result.tv_sec = 0; |
| + result.tv_usec = 0; |
| + return result; |
| + } |
| + if (is_max()) { |
| + result.tv_sec = std::numeric_limits<time_t>::max(); |
| + result.tv_usec = static_cast<suseconds_t>(Time::kMicrosecondsPerSecond) - 1; |
| + return result; |
| + } |
| + int64_t us = us_ - kTimeTToMicrosecondsOffset; |
| + result.tv_sec = us / Time::kMicrosecondsPerSecond; |
| + result.tv_usec = us % Time::kMicrosecondsPerSecond; |
| + return result; |
| +} |
| + |
| +} // namespace base |