David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS 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 "update_engine/clock.h" |
| 6 | |
| 7 | #include <time.h> |
| 8 | |
| 9 | namespace chromeos_update_engine { |
| 10 | |
| 11 | base::Time Clock::GetWallclockTime() { |
| 12 | return base::Time::Now(); |
| 13 | } |
| 14 | |
| 15 | base::Time Clock::GetMonotonicTime() { |
| 16 | struct timespec now_ts; |
| 17 | if (clock_gettime(CLOCK_MONOTONIC_RAW, &now_ts) != 0) { |
| 18 | // Avoid logging this as an error as call-sites may call this very |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 19 | // often and we don't want to fill up the disk. Note that this |
| 20 | // only fails if running on ancient kernels (CLOCK_MONOTONIC_RAW |
| 21 | // was added in Linux 2.6.28) so it never fails on a ChromeOS |
| 22 | // device. |
| 23 | return base::Time(); |
| 24 | } |
| 25 | struct timeval now_tv; |
| 26 | now_tv.tv_sec = now_ts.tv_sec; |
| 27 | now_tv.tv_usec = now_ts.tv_nsec/base::Time::kNanosecondsPerMicrosecond; |
| 28 | return base::Time::FromTimeVal(now_tv); |
| 29 | } |
| 30 | |
| 31 | base::Time Clock::GetBootTime() { |
| 32 | struct timespec now_ts; |
| 33 | if (clock_gettime(CLOCK_BOOTTIME, &now_ts) != 0) { |
| 34 | // Avoid logging this as an error as call-sites may call this very |
| 35 | // often and we don't want to fill up the disk. Note that this |
| 36 | // only fails if running on ancient kernels (CLOCK_BOOTTIME was |
| 37 | // added in Linux 2.6.39) so it never fails on a ChromeOS device. |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 38 | return base::Time(); |
| 39 | } |
| 40 | struct timeval now_tv; |
| 41 | now_tv.tv_sec = now_ts.tv_sec; |
| 42 | now_tv.tv_usec = now_ts.tv_nsec/base::Time::kNanosecondsPerMicrosecond; |
| 43 | return base::Time::FromTimeVal(now_tv); |
| 44 | } |
| 45 | |
| 46 | } // namespace chromeos_update_engine |