2
* Copyright 2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License along
14
* with this program. If not, see <http://www.gnu.org/licenses/>.
17
* Charles Kerr <charles.kerr@canonical.com>
20
#include <datetime/clock.h>
30
Impl(LiveClock& owner, const std::shared_ptr<Timezones>& tzd):
36
timezones_->timezone.changed().connect ([this](const std::string& z) {setTimezone(z);});
37
setTimezone(timezones_->timezone.get());
40
owner_.skewTestIntervalSec.changed().connect([this](unsigned int intervalSec) {setInterval(intervalSec);});
41
setInterval(owner_.skewTestIntervalSec.get());
48
g_clear_pointer (&timezone_, g_time_zone_unref);
51
GDateTime* localtime() const
53
g_assert (timezone_ != nullptr);
55
return g_date_time_new_now (timezone_);
60
void setTimezone (const std::string& str)
62
g_clear_pointer (&timezone_, g_time_zone_unref);
63
timezone_= g_time_zone_new (str.c_str());
64
owner_.skewDetected();
73
g_source_remove(skew_timeout_id_);
77
g_clear_pointer(&prev_datetime_, g_date_time_unref);
80
void setInterval(unsigned int seconds)
86
prev_datetime_ = owner_.localtime();
87
skew_timeout_id_ = g_timeout_add_seconds(seconds, onTimerPulse, this);
91
static gboolean onTimerPulse(gpointer gself)
93
auto self = static_cast<Impl*>(gself);
95
// check to see if too much time passed since the last check */
96
GDateTime * now = self->owner_.localtime();
97
const GTimeSpan diff = g_date_time_difference(now, self->prev_datetime_);
98
const GTimeSpan fuzz = 5;
99
const GTimeSpan max = (self->owner_.skewTestIntervalSec.get() + fuzz) * G_USEC_PER_SEC;
101
self->owner_.skewDetected();
103
// update prev_datetime
104
g_clear_pointer(&self->prev_datetime_, g_date_time_unref);
105
self->prev_datetime_ = now;
107
return G_SOURCE_CONTINUE;
113
GTimeZone * timezone_ = nullptr;
114
std::shared_ptr<Timezones> timezones_;
116
GDateTime * prev_datetime_ = nullptr;
117
unsigned int skew_timeout_id_ = 0;
118
unsigned int sleep_subscription_id_ = 0;
121
LiveClock::LiveClock(const std::shared_ptr<Timezones>& tzd):
122
p (new Impl (*this, tzd))
126
LiveClock::~LiveClock() =default;
129
LiveClock::localtime() const
131
return p->localtime();
134
} // namespace datetime
135
} // namespace indicator