blob: dab9829dc302b396c32547ef44e9827377121f5c [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 Vakulenko072359c2014-07-18 11:41:07 -07007#include <string>
8
Alex Vakulenko75039d72014-03-25 12:36:28 -07009#include <base/time/time.h>
Gilad Arnold78a78112014-03-13 14:58:06 -070010
11#include "update_engine/clock_interface.h"
12
13using base::Time;
14using base::TimeDelta;
15using chromeos_update_engine::ClockInterface;
16using std::string;
17
Alex Deymo63784a52014-05-28 10:46:14 -070018namespace chromeos_update_manager {
Gilad Arnold78a78112014-03-13 14:58:06 -070019
20// A variable returning the current date.
21class CurrDateVariable : public Variable<Time> {
22 public:
23 // TODO(garnold) Turn this into an async variable with the needed callback
24 // logic for when it value changes.
25 CurrDateVariable(const string& name, ClockInterface* clock)
26 : Variable<Time>(name, TimeDelta::FromHours(1)), clock_(clock) {}
27
28 protected:
Alex Deymof329b932014-10-30 01:37:48 -070029 virtual const Time* GetValue(TimeDelta /* timeout */,
Gilad Arnold78a78112014-03-13 14:58:06 -070030 string* /* errmsg */) {
31 Time::Exploded now_exp;
32 clock_->GetWallclockTime().LocalExplode(&now_exp);
33 now_exp.hour = now_exp.minute = now_exp.second = now_exp.millisecond = 0;
34 return new Time(Time::FromLocalExploded(now_exp));
35 }
36
37 private:
38 ClockInterface* clock_;
39
40 DISALLOW_COPY_AND_ASSIGN(CurrDateVariable);
41};
42
43// A variable returning the current hour in local time.
44class CurrHourVariable : public Variable<int> {
45 public:
46 // TODO(garnold) Turn this into an async variable with the needed callback
47 // logic for when it value changes.
48 CurrHourVariable(const string& name, ClockInterface* clock)
49 : Variable<int>(name, TimeDelta::FromMinutes(5)), clock_(clock) {}
50
51 protected:
Alex Deymof329b932014-10-30 01:37:48 -070052 virtual const int* GetValue(TimeDelta /* timeout */,
Gilad Arnold78a78112014-03-13 14:58:06 -070053 string* /* errmsg */) {
54 Time::Exploded exploded;
55 clock_->GetWallclockTime().LocalExplode(&exploded);
56 return new int(exploded.hour);
57 }
58
59 private:
60 ClockInterface* clock_;
61
62 DISALLOW_COPY_AND_ASSIGN(CurrHourVariable);
63};
64
Alex Deymo42c30c32014-04-24 18:41:18 -070065bool RealTimeProvider::Init() {
David Zeuthen21716e22014-04-23 15:42:05 -070066 var_curr_date_.reset(new CurrDateVariable("curr_date", clock_));
67 var_curr_hour_.reset(new CurrHourVariable("curr_hour", clock_));
Gilad Arnold78a78112014-03-13 14:58:06 -070068 return true;
69}
70
Alex Deymo63784a52014-05-28 10:46:14 -070071} // namespace chromeos_update_manager