blob: 0821a56f473ac2dca8a05237b125aad0576c3265 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Zeuthenf413fe52013-04-22 14:04:39 -070016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/common/clock.h"
David Zeuthenf413fe52013-04-22 14:04:39 -070018
19#include <time.h>
20
21namespace chromeos_update_engine {
22
Amin Hassani0468a762020-11-17 23:53:48 -080023base::Time Clock::GetWallclockTime() const {
David Zeuthenf413fe52013-04-22 14:04:39 -070024 return base::Time::Now();
25}
26
Amin Hassani0468a762020-11-17 23:53:48 -080027base::Time Clock::GetMonotonicTime() const {
David Zeuthenf413fe52013-04-22 14:04:39 -070028 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 Zeuthen3c55abd2013-10-14 12:48:03 -070031 // 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 Hassanib2689592019-01-13 17:04:28 -080039 now_tv.tv_usec = now_ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
David Zeuthen3c55abd2013-10-14 12:48:03 -070040 return base::Time::FromTimeVal(now_tv);
41}
42
Amin Hassani0468a762020-11-17 23:53:48 -080043base::Time Clock::GetBootTime() const {
David Zeuthen3c55abd2013-10-14 12:48:03 -070044 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 Zeuthenf413fe52013-04-22 14:04:39 -070050 return base::Time();
51 }
52 struct timeval now_tv;
53 now_tv.tv_sec = now_ts.tv_sec;
Amin Hassanib2689592019-01-13 17:04:28 -080054 now_tv.tv_usec = now_ts.tv_nsec / base::Time::kNanosecondsPerMicrosecond;
David Zeuthenf413fe52013-04-22 14:04:39 -070055 return base::Time::FromTimeVal(now_tv);
56}
57
58} // namespace chromeos_update_engine