awesome/lib/wibox/widget/textclock.lua

118 lines
3.5 KiB
Lua
Raw Normal View History

---------------------------------------------------------------------------
--- 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")
local gtable = require("gears.table")
local glib = require("lgi").GLib
local DateTime = glib.DateTime
local TimeZone = glib.TimeZone
local textclock = { mt = {} }
--- Set the clock's format
-- @property format
-- @tparam string format The new time format. This can contain pango markup
function textclock:set_format(format)
self._private.format = format
self:force_update()
end
function textclock:get_format()
return self._private.format
end
--- Set the clock's timezone
-- @property timezone
-- @tparam string timezone
function textclock:set_timezone(tzid)
self._private.tzid = tzid
self._private.timezone = tzid and TimeZone.new(tzid) or TimeZone.new_local()
self:force_update()
end
function textclock:get_timezone()
return self._private.tzid
end
2018-01-21 00:57:59 +01:00
--- Set the clock's refresh rate
-- @property refresh
-- @tparam number How often the clock is updated, in seconds
2018-01-21 00:57:59 +01:00
function textclock:set_refresh(refresh)
self._private.refresh = refresh or self._private.refresh
self:force_update()
end
2018-01-21 00:57:59 +01:00
function textclock:get_refresh()
return self._private.refresh
end
--- Force a textclock to update now.
function textclock:force_update()
self._timer:emit_signal("timeout")
end
--- 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.
2018-01-21 00:57:59 +01:00
-- @tparam[opt=60] number refresh How often to update the time (in seconds).
-- @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.
-- @treturn table A textbox widget.
-- @function wibox.widget.textclock
local function new(format, refresh, tzid)
local w = textbox()
gtable.crush(w, textclock, true)
w._private.format = format or " %a %b %d, %H:%M "
2018-01-21 00:57:59 +01:00
w._private.refresh = refresh or 60
w._private.tzid = tzid
w._private.timezone = tzid and TimeZone.new(tzid) or TimeZone.new_local()
2016-05-26 23:41:23 +02:00
function w._private.textclock_update_cb()
local str = DateTime.new_now(w._private.timezone):format(w._private.format)
if str == nil then
require("gears.debug").print_warning("textclock: "
.. "g_date_time_format() failed for format "
.. "'" .. w._private.format .. "'")
end
w:set_markup(str)
2018-01-21 00:57:59 +01:00
w._timer.timeout = calc_timeout(w._private.refresh)
w._timer:again()
return true -- Continue the timer
end
2018-01-21 00:57:59 +01:00
w._timer = timer.weak_start_new(refresh, w._private.textclock_update_cb)
w:force_update()
return w
end
function textclock.mt:__call(...)
return new(...)
end
2016-05-26 23:41:23 +02:00
--@DOC_widget_COMMON@
--@DOC_object_COMMON@
return setmetatable(textclock, textclock.mt)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80