From 8dc6fa866628605083a26a70357f762eb51644ff Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 6 Dec 2014 17:15:24 +0100 Subject: [PATCH] textclock: Round timeout so that displayed time is more accurate Signed-off-by: Uli Schlachter --- lib/awful/widget/textclock.lua.in | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/awful/widget/textclock.lua.in b/lib/awful/widget/textclock.lua.in index bf8efffc..8537ebdf 100644 --- a/lib/awful/widget/textclock.lua.in +++ b/lib/awful/widget/textclock.lua.in @@ -6,6 +6,7 @@ local setmetatable = setmetatable local os = os +local fmod = math.fmod local textbox = require("wibox.widget.textbox") local timer = require("gears.timer") @@ -13,6 +14,14 @@ local timer = require("gears.timer") -- awful.widget.textclock local textclock = { mt = {} } +-- This lowers the timeout so that it occurs "correctly". For example, a timeout +-- of 60 is rounded so that it occurs the next time the clock reads ":00 seconds". +local function calc_timeout(real_timeout) + local date = os.date("*t") + local date_time = (date.hour * 60 + date.min) * 60 + date.sec + return real_timeout - fmod(date_time, real_timeout) +end + --- Create a textclock widget. It draws the time it is in a textbox. -- @param format The time format. Default is " %a %b %d, %H:%M ". -- @param timeout How often update the time. Default is 60. @@ -23,7 +32,11 @@ function textclock.new(format, timeout) local w = textbox() local t = timer { timeout = timeout } - t:connect_signal("timeout", function() w:set_markup(os.date(format)) end) + t:connect_signal("timeout", function() + w:set_markup(os.date(format)) + t.timeout = calc_timeout(timeout) + t:again() + end) t:start() t:emit_signal("timeout") return w