2016-05-23 06:46:01 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
--- Text clock widget.
|
|
|
|
--
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
|
|
|
-- @classmod wibox.widget.textclock
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local os = os
|
|
|
|
local textbox = require("wibox.widget.textbox")
|
|
|
|
local timer = require("gears.timer")
|
2017-04-21 16:34:23 +02:00
|
|
|
local glib = require("lgi").GLib
|
|
|
|
local DateTime = glib.DateTime
|
|
|
|
local TimeZone = glib.TimeZone
|
2016-05-23 06:46:01 +02:00
|
|
|
|
|
|
|
local textclock = { mt = {} }
|
|
|
|
|
2017-09-10 20:12:25 +02:00
|
|
|
--- Force a textclock to update now.
|
|
|
|
function textclock:force_update()
|
|
|
|
self._timer:emit_signal("timeout")
|
|
|
|
end
|
|
|
|
|
2016-05-23 06:46:01 +02:00
|
|
|
--- 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)
|
|
|
|
return real_timeout - os.time() % real_timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Create a textclock widget. It draws the time it is in a textbox.
|
|
|
|
--
|
|
|
|
-- @tparam[opt=" %a %b %d, %H:%M "] string format The time format.
|
|
|
|
-- @tparam[opt=60] number timeout How often update the time (in seconds).
|
2017-04-21 16:34:23 +02:00
|
|
|
-- @tparam[opt=local timezone] string timezone The timezone to use,
|
|
|
|
-- e.g. "Z" for UTC, "±hh:mm" or "Europe/Amsterdam". See
|
|
|
|
-- https://developer.gnome.org/glib/stable/glib-GTimeZone.html#g-time-zone-new.
|
2016-05-23 06:46:01 +02:00
|
|
|
-- @treturn table A textbox widget.
|
|
|
|
-- @function wibox.widget.textclock
|
2017-04-21 16:34:23 +02:00
|
|
|
function textclock.new(format, timeout, timezone)
|
2016-05-23 06:46:01 +02:00
|
|
|
format = format or " %a %b %d, %H:%M "
|
|
|
|
timeout = timeout or 60
|
2017-04-21 16:34:23 +02:00
|
|
|
timezone = timezone and TimeZone.new(timezone) or TimeZone.new_local()
|
2016-05-23 06:46:01 +02:00
|
|
|
|
|
|
|
local w = textbox()
|
2017-09-10 20:12:25 +02:00
|
|
|
w.force_update = textclock.force_update
|
2016-05-26 23:41:23 +02:00
|
|
|
function w._private.textclock_update_cb()
|
2017-04-21 16:34:23 +02:00
|
|
|
w:set_markup(DateTime.new_now(timezone):format(format))
|
2017-09-10 20:12:25 +02:00
|
|
|
w._timer.timeout = calc_timeout(timeout)
|
|
|
|
w._timer:again()
|
2016-05-23 06:46:01 +02:00
|
|
|
return true -- Continue the timer
|
|
|
|
end
|
2017-09-10 20:12:25 +02:00
|
|
|
w._timer = timer.weak_start_new(timeout, w._private.textclock_update_cb)
|
|
|
|
w:force_update()
|
2016-05-23 06:46:01 +02:00
|
|
|
return w
|
|
|
|
end
|
|
|
|
|
|
|
|
function textclock.mt:__call(...)
|
|
|
|
return textclock.new(...)
|
|
|
|
end
|
|
|
|
|
2016-05-26 23:41:23 +02:00
|
|
|
--@DOC_widget_COMMON@
|
|
|
|
|
|
|
|
--@DOC_object_COMMON@
|
|
|
|
|
2016-05-23 06:46:01 +02:00
|
|
|
return setmetatable(textclock, textclock.mt)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|