blob: 9067bdd2d156db94a23c3a30c5b23f6a1e55b903 [file] [log] [blame]
Gilad Arnold78a78112014-03-13 14:58:06 -07001// Copyright (c) 2014 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
Alex Deymo63784a52014-05-28 10:46:14 -07005#include "update_engine/update_manager/real_time_provider.h"
Gilad Arnold78a78112014-03-13 14:58:06 -07006
Alex Vakulenko75039d72014-03-25 12:36:28 -07007#include <base/time/time.h>
Gilad Arnold78a78112014-03-13 14:58:06 -07008
9#include "update_engine/clock_interface.h"
10
11using base::Time;
12using base::TimeDelta;
13using chromeos_update_engine::ClockInterface;
14using std::string;
15
Alex Deymo63784a52014-05-28 10:46:14 -070016namespace chromeos_update_manager {
Gilad Arnold78a78112014-03-13 14:58:06 -070017
18// A variable returning the current date.
19class CurrDateVariable : public Variable<Time> {
20 public:
21 // TODO(garnold) Turn this into an async variable with the needed callback
22 // logic for when it value changes.
23 CurrDateVariable(const string& name, ClockInterface* clock)
24 : Variable<Time>(name, TimeDelta::FromHours(1)), clock_(clock) {}
25
26 protected:
27 virtual const Time* GetValue(base::TimeDelta /* timeout */,
28 string* /* errmsg */) {
29 Time::Exploded now_exp;
30 clock_->GetWallclockTime().LocalExplode(&now_exp);
31 now_exp.hour = now_exp.minute = now_exp.second = now_exp.millisecond = 0;
32 return new Time(Time::FromLocalExploded(now_exp));
33 }
34
35 private:
36 ClockInterface* clock_;
37
38 DISALLOW_COPY_AND_ASSIGN(CurrDateVariable);
39};
40
41// A variable returning the current hour in local time.
42class CurrHourVariable : public Variable<int> {
43 public:
44 // TODO(garnold) Turn this into an async variable with the needed callback
45 // logic for when it value changes.
46 CurrHourVariable(const string& name, ClockInterface* clock)
47 : Variable<int>(name, TimeDelta::FromMinutes(5)), clock_(clock) {}
48
49 protected:
50 virtual const int* GetValue(base::TimeDelta /* timeout */,
51 string* /* errmsg */) {
52 Time::Exploded exploded;
53 clock_->GetWallclockTime().LocalExplode(&exploded);
54 return new int(exploded.hour);
55 }
56
57 private:
58 ClockInterface* clock_;
59
60 DISALLOW_COPY_AND_ASSIGN(CurrHourVariable);
61};
62
Alex Deymo42c30c32014-04-24 18:41:18 -070063bool RealTimeProvider::Init() {
David Zeuthen21716e22014-04-23 15:42:05 -070064 var_curr_date_.reset(new CurrDateVariable("curr_date", clock_));
65 var_curr_hour_.reset(new CurrHourVariable("curr_hour", clock_));
Gilad Arnold78a78112014-03-13 14:58:06 -070066 return true;
67}
68
Alex Deymo63784a52014-05-28 10:46:14 -070069} // namespace chromeos_update_manager