| 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 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 <zircon/syscalls.h> |
| 8 | |
| 9 | #include "base/fuchsia/fuchsia_logging.h" |
| 10 | #include "base/time/time_override.h" |
| 11 | |
| 12 | namespace base { |
| 13 | |
| 14 | // Time ----------------------------------------------------------------------- |
| 15 | |
| 16 | namespace subtle { |
| 17 | Time TimeNowIgnoringOverride() { |
| 18 | zx_time_t nanos_since_unix_epoch; |
| 19 | zx_status_t status = zx_clock_get(ZX_CLOCK_UTC, &nanos_since_unix_epoch); |
| 20 | ZX_CHECK(status == ZX_OK, status); |
| 21 | // The following expression will overflow in the year 289938 A.D.: |
| 22 | return Time::FromZxTime(nanos_since_unix_epoch); |
| 23 | } |
| 24 | |
| 25 | Time TimeNowFromSystemTimeIgnoringOverride() { |
| 26 | // Just use TimeNowIgnoringOverride() because it returns the system time. |
| 27 | return TimeNowIgnoringOverride(); |
| 28 | } |
| 29 | } // namespace subtle |
| 30 | |
| 31 | // TimeTicks ------------------------------------------------------------------ |
| 32 | |
| 33 | namespace subtle { |
| 34 | TimeTicks TimeTicksNowIgnoringOverride() { |
| 35 | const zx_time_t nanos_since_boot = zx_clock_get_monotonic(); |
| 36 | CHECK(nanos_since_boot != 0); |
| 37 | return TimeTicks::FromZxTime(nanos_since_boot); |
| 38 | } |
| 39 | } // namespace subtle |
| 40 | |
| 41 | // static |
| 42 | TimeDelta TimeDelta::FromZxDuration(zx_duration_t nanos) { |
| 43 | return TimeDelta::FromNanoseconds(nanos); |
| 44 | } |
| 45 | |
| 46 | zx_duration_t TimeDelta::ToZxDuration() const { |
| 47 | return InNanoseconds(); |
| 48 | } |
| 49 | |
| 50 | // static |
| 51 | Time Time::FromZxTime(zx_time_t nanos_since_unix_epoch) { |
| 52 | return Time::UnixEpoch() + TimeDelta::FromNanoseconds(nanos_since_unix_epoch); |
| 53 | } |
| 54 | |
| 55 | zx_time_t Time::ToZxTime() const { |
| 56 | return (*this - Time::UnixEpoch()).InNanoseconds(); |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | TimeTicks::Clock TimeTicks::GetClock() { |
| 61 | return Clock::FUCHSIA_ZX_CLOCK_MONOTONIC; |
| 62 | } |
| 63 | |
| 64 | // static |
| 65 | bool TimeTicks::IsHighResolution() { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | // static |
| 70 | bool TimeTicks::IsConsistentAcrossProcesses() { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | // static |
| 75 | TimeTicks TimeTicks::FromZxTime(zx_time_t nanos_since_boot) { |
| 76 | return TimeTicks() + TimeDelta::FromNanoseconds(nanos_since_boot); |
| 77 | } |
| 78 | |
| 79 | zx_time_t TimeTicks::ToZxTime() const { |
| 80 | return (*this - TimeTicks()).InNanoseconds(); |
| 81 | } |
| 82 | |
| 83 | // ThreadTicks ---------------------------------------------------------------- |
| 84 | |
| 85 | namespace subtle { |
| 86 | ThreadTicks ThreadTicksNowIgnoringOverride() { |
| 87 | zx_time_t nanos_since_thread_started; |
| 88 | zx_status_t status = |
| 89 | zx_clock_get(ZX_CLOCK_THREAD, &nanos_since_thread_started); |
| 90 | ZX_CHECK(status == ZX_OK, status); |
| 91 | DCHECK(nanos_since_thread_started != 0); |
| 92 | return ThreadTicks() + TimeDelta::FromNanoseconds(nanos_since_thread_started); |
| 93 | } |
| 94 | } // namespace subtle |
| 95 | |
| 96 | } // namespace base |
| 97 | |