42
guint calculate_milliseconds_until_next_minute(GDateTime * now)
45
GDateTime * start_of_next;
46
GTimeSpan interval_usec;
49
next = g_date_time_add_minutes(now, 1);
50
start_of_next = g_date_time_new_local(g_date_time_get_year(next),
51
g_date_time_get_month(next),
52
g_date_time_get_day_of_month(next),
53
g_date_time_get_hour(next),
54
g_date_time_get_minute(next),
57
interval_usec = g_date_time_difference(start_of_next, now);
58
interval_msec = (interval_usec + 999) / 1000;
60
g_date_time_unref(start_of_next);
61
g_date_time_unref(next);
65
gint calculate_milliseconds_until_next_second(GDateTime * now)
51
gint calculate_milliseconds_until_next_second(const DateTime& now)
67
53
gint interval_usec;
68
54
guint interval_msec;
70
interval_usec = G_USEC_PER_SEC - g_date_time_get_microsecond(now);
56
interval_usec = G_USEC_PER_SEC - g_date_time_get_microsecond(now.get());
71
57
interval_msec = (interval_usec + 999) / 1000;
72
58
return interval_msec;
143
m_owner->headerFormat.changed().connect([this](const std::string& /*fmt*/){updateHeader();});
144
m_clock->skewDetected.connect([this](){updateHeader();});
124
m_owner->headerFormat.changed().connect([this](const std::string& /*fmt*/){update_header();});
125
m_clock->minuteChanged.connect([this](){update_header();});
147
128
restartRelativeTimer();
152
clearTimer(m_header_timer);
133
clear_timer(m_header_seconds_timer);
134
clear_timer(m_relative_timer);
139
static bool format_shows_seconds(const std::string& fmt)
141
return (fmt.find("%s") != std::string::npos)
142
|| (fmt.find("%S") != std::string::npos)
143
|| (fmt.find("%T") != std::string::npos)
144
|| (fmt.find("%X") != std::string::npos)
145
|| (fmt.find("%c") != std::string::npos);
150
// update the header property
159
151
const auto fmt = m_owner->headerFormat.get();
160
152
const auto str = m_clock->localtime().format(fmt);
161
153
m_owner->header.set(str);
163
restartHeaderTimer();
155
// if the header needs to show seconds, set a timer.
156
if (format_shows_seconds(fmt))
157
start_header_timer();
159
clear_timer(m_header_seconds_timer);
166
void restartHeaderTimer()
162
// we've got a header format that shows seconds,
163
// so we need to update it every second
164
void start_header_timer()
168
clearTimer(m_header_timer);
170
const auto fmt = m_owner->headerFormat.get();
171
const bool header_shows_seconds = (fmt.find("%s") != std::string::npos)
172
|| (fmt.find("%S") != std::string::npos)
173
|| (fmt.find("%T") != std::string::npos)
174
|| (fmt.find("%X") != std::string::npos)
175
|| (fmt.find("%c") != std::string::npos);
166
clear_timer(m_header_seconds_timer);
178
168
const auto now = m_clock->localtime();
179
auto str = now.format("%F %T");
180
if (header_shows_seconds)
181
interval_msec = calculate_milliseconds_until_next_second(now.get());
183
interval_msec = calculate_milliseconds_until_next_minute(now.get());
169
auto interval_msec = calculate_milliseconds_until_next_second(now);
185
170
interval_msec += 50; // add a small margin to ensure the callback
186
171
// fires /after/ next is reached
188
m_header_timer = g_timeout_add_full(G_PRIORITY_HIGH, interval_msec, onHeaderTimer, this, nullptr);
172
m_header_seconds_timer = g_timeout_add_full(G_PRIORITY_HIGH,
191
static gboolean onHeaderTimer(gpointer gself)
179
static gboolean on_header_timer(gpointer gself)
193
static_cast<Formatter::Impl*>(gself)->updateHeader();
181
static_cast<Formatter::Impl*>(gself)->update_header();
194
182
return G_SOURCE_REMOVE;