Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2013 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/common/clock.h" |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 18 | |
| 19 | #include <time.h> |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 23 | base::Time Clock::GetWallclockTime() const { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 24 | return base::Time::Now(); |
| 25 | } |
| 26 | |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 27 | base::Time Clock::GetMonotonicTime() const { |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 28 | struct timespec now_ts; |
| 29 | if (clock_gettime(CLOCK_MONOTONIC_RAW, &now_ts) != 0) { |
| 30 | // 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] | 31 | // often and we don't want to fill up the disk. Note that this |
| 32 | // only fails if running on ancient kernels (CLOCK_MONOTONIC_RAW |
| 33 | // was added in Linux 2.6.28) so it never fails on a ChromeOS |
| 34 | // device. |
| 35 | return base::Time(); |
| 36 | } |
| 37 | struct timeval now_tv; |
| 38 | now_tv.tv_sec = now_ts.tv_sec; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 39 | now_tv.tv_usec = now_ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond; |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 40 | return base::Time::FromTimeVal(now_tv); |
| 41 | } |
| 42 | |
Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 43 | base::Time Clock::GetBootTime() const { |
David Zeuthen | 3c55abd | 2013-10-14 12:48:03 -0700 | [diff] [blame] | 44 | struct timespec now_ts; |
| 45 | if (clock_gettime(CLOCK_BOOTTIME, &now_ts) != 0) { |
| 46 | // Avoid logging this as an error as call-sites may call this very |
| 47 | // often and we don't want to fill up the disk. Note that this |
| 48 | // only fails if running on ancient kernels (CLOCK_BOOTTIME was |
| 49 | // 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] | 50 | return base::Time(); |
| 51 | } |
| 52 | struct timeval now_tv; |
| 53 | now_tv.tv_sec = now_ts.tv_sec; |
Amin Hassani | b268959 | 2019-01-13 17:04:28 -0800 | [diff] [blame] | 54 | now_tv.tv_usec = now_ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond; |
David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 55 | return base::Time::FromTimeVal(now_tv); |
| 56 | } |
| 57 | |
| 58 | } // namespace chromeos_update_engine |