textclock: Round timeout so that displayed time is more accurate

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2014-12-06 17:15:24 +01:00
parent 94e61ec94b
commit 8dc6fa8666
1 changed files with 14 additions and 1 deletions

View File

@ -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