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 //
16 
17 #include "update_engine/common/clock.h"
18 
19 #include <time.h>
20 
21 namespace chromeos_update_engine {
22 
GetWallclockTime()23 base::Time Clock::GetWallclockTime() {
24   return base::Time::Now();
25 }
26 
GetMonotonicTime()27 base::Time Clock::GetMonotonicTime() {
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
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;
39   now_tv.tv_usec = now_ts.tv_nsec/base::Time::kNanosecondsPerMicrosecond;
40   return base::Time::FromTimeVal(now_tv);
41 }
42 
GetBootTime()43 base::Time Clock::GetBootTime() {
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.
50     return base::Time();
51   }
52   struct timeval now_tv;
53   now_tv.tv_sec = now_ts.tv_sec;
54   now_tv.tv_usec = now_ts.tv_nsec/base::Time::kNanosecondsPerMicrosecond;
55   return base::Time::FromTimeVal(now_tv);
56 }
57 
58 }  // namespace chromeos_update_engine
59