blob: baa8ae34ea5e56693fc6a1a23b94ee8e8ab48837 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 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//
Gilad Arnold78a78112014-03-13 14:58:06 -070016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/real_time_provider.h"
Gilad Arnold78a78112014-03-13 14:58:06 -070018
Alex Vakulenko072359c2014-07-18 11:41:07 -070019#include <string>
20
Alex Vakulenko75039d72014-03-25 12:36:28 -070021#include <base/time/time.h>
Gilad Arnold78a78112014-03-13 14:58:06 -070022
Alex Deymo39910dc2015-11-09 17:04:30 -080023#include "update_engine/common/clock_interface.h"
Gilad Arnold78a78112014-03-13 14:58:06 -070024
25using base::Time;
26using base::TimeDelta;
27using chromeos_update_engine::ClockInterface;
28using std::string;
29
Alex Deymo63784a52014-05-28 10:46:14 -070030namespace chromeos_update_manager {
Gilad Arnold78a78112014-03-13 14:58:06 -070031
32// A variable returning the current date.
33class CurrDateVariable : public Variable<Time> {
34 public:
35 // TODO(garnold) Turn this into an async variable with the needed callback
36 // logic for when it value changes.
37 CurrDateVariable(const string& name, ClockInterface* clock)
38 : Variable<Time>(name, TimeDelta::FromHours(1)), clock_(clock) {}
39
40 protected:
Alex Deymof329b932014-10-30 01:37:48 -070041 virtual const Time* GetValue(TimeDelta /* timeout */,
Gilad Arnold78a78112014-03-13 14:58:06 -070042 string* /* errmsg */) {
43 Time::Exploded now_exp;
44 clock_->GetWallclockTime().LocalExplode(&now_exp);
45 now_exp.hour = now_exp.minute = now_exp.second = now_exp.millisecond = 0;
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090046 Time* now = new Time();
47 bool success = Time::FromLocalExploded(now_exp, now);
48 DCHECK(success);
49 return now;
Gilad Arnold78a78112014-03-13 14:58:06 -070050 }
51
52 private:
53 ClockInterface* clock_;
54
55 DISALLOW_COPY_AND_ASSIGN(CurrDateVariable);
56};
57
58// A variable returning the current hour in local time.
59class CurrHourVariable : public Variable<int> {
60 public:
61 // TODO(garnold) Turn this into an async variable with the needed callback
62 // logic for when it value changes.
63 CurrHourVariable(const string& name, ClockInterface* clock)
64 : Variable<int>(name, TimeDelta::FromMinutes(5)), clock_(clock) {}
65
66 protected:
Alex Deymof329b932014-10-30 01:37:48 -070067 virtual const int* GetValue(TimeDelta /* timeout */,
Gilad Arnold78a78112014-03-13 14:58:06 -070068 string* /* errmsg */) {
69 Time::Exploded exploded;
70 clock_->GetWallclockTime().LocalExplode(&exploded);
71 return new int(exploded.hour);
72 }
73
74 private:
75 ClockInterface* clock_;
76
77 DISALLOW_COPY_AND_ASSIGN(CurrHourVariable);
78};
79
Adolfo Victoria96143502018-06-13 18:44:49 -070080class CurrMinuteVariable : public Variable<int> {
81 public:
82 CurrMinuteVariable(const string& name, ClockInterface* clock)
83 : Variable<int>(name, TimeDelta::FromSeconds(15)), clock_(clock) {}
84
85 protected:
86 virtual const int* GetValue(TimeDelta /* timeout */, string* /* errmsg */) {
87 Time::Exploded exploded;
88 clock_->GetWallclockTime().LocalExplode(&exploded);
89 return new int(exploded.minute);
90 }
91
92 private:
93 ClockInterface* clock_;
94
95 DISALLOW_COPY_AND_ASSIGN(CurrMinuteVariable);
96};
97
Alex Deymo42c30c32014-04-24 18:41:18 -070098bool RealTimeProvider::Init() {
David Zeuthen21716e22014-04-23 15:42:05 -070099 var_curr_date_.reset(new CurrDateVariable("curr_date", clock_));
100 var_curr_hour_.reset(new CurrHourVariable("curr_hour", clock_));
Adolfo Victoria96143502018-06-13 18:44:49 -0700101 var_curr_minute_.reset(new CurrMinuteVariable("curr_minute", clock_));
Gilad Arnold78a78112014-03-13 14:58:06 -0700102 return true;
103}
104
Alex Deymo63784a52014-05-28 10:46:14 -0700105} // namespace chromeos_update_manager