2016-05-23 06:46:01 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
--- Text clock widget.
|
|
|
|
--
|
|
|
|
-- @author Julien Danjou <julien@danjou.info>
|
|
|
|
-- @copyright 2009 Julien Danjou
|
2019-06-06 08:15:53 +02:00
|
|
|
-- @widgetmod wibox.widget.textclock
|
2016-05-23 06:46:01 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local os = os
|
|
|
|
local textbox = require("wibox.widget.textbox")
|
|
|
|
local timer = require("gears.timer")
|
2018-01-21 01:18:54 +01:00
|
|
|
local gtable = require("gears.table")
|
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 = {} }
|
|
|
|
|
2019-01-31 20:34:41 +01:00
|
|
|
local DateTime_new_now = DateTime.new_now
|
|
|
|
|
|
|
|
-- When $SOURCE_DATE_EPOCH and $SOURCE_DIRECTORY are both set, then this code is
|
|
|
|
-- most likely being run by the test runner. Ensure reproducible dates.
|
|
|
|
local source_date_epoch = tonumber(os.getenv("SOURCE_DATE_EPOCH"))
|
|
|
|
if source_date_epoch and os.getenv("SOURCE_DIRECTORY") then
|
|
|
|
DateTime_new_now = function()
|
|
|
|
return DateTime.new_from_unix_utc(source_date_epoch)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-11 03:29:39 +02:00
|
|
|
--- Set the clock's format.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2019-10-11 03:29:39 +02:00
|
|
|
-- For information about the format specifiers, see
|
|
|
|
-- [the GLib docs](https://developer.gnome.org/glib/stable/glib-GDateTime.html#g-date-time-format).
|
2018-01-21 01:18:54 +01:00
|
|
|
-- @property format
|
2019-10-11 03:29:39 +02:00
|
|
|
-- @tparam string format The new time format. This can contain pango markup.
|
2018-01-21 01:18:54 +01:00
|
|
|
|
2018-01-11 15:00:07 +01:00
|
|
|
function textclock:set_format(format)
|
|
|
|
self._private.format = format
|
|
|
|
self:force_update()
|
|
|
|
end
|
|
|
|
|
|
|
|
function textclock:get_format()
|
|
|
|
return self._private.format
|
|
|
|
end
|
|
|
|
|
2019-11-29 06:33:44 +01:00
|
|
|
--- Set the clock's timezone.
|
|
|
|
--
|
2019-11-25 22:35:41 +01:00
|
|
|
-- e.g. "Z" for UTC, "±hh:mm" or "Europe/Amsterdam". See
|
|
|
|
-- [GTimeZone](https://developer.gnome.org/glib/stable/glib-GTimeZone.html#g-time-zone-new).
|
2018-01-21 01:18:54 +01:00
|
|
|
-- @property timezone
|
2018-01-11 15:00:07 +01:00
|
|
|
-- @tparam string timezone
|
2018-01-21 01:18:54 +01:00
|
|
|
|
2018-01-11 15:00:07 +01:00
|
|
|
function textclock:set_timezone(tzid)
|
|
|
|
self._private.tzid = tzid
|
2018-04-22 08:05:40 +02:00
|
|
|
self._private.timezone = tzid and TimeZone.new(tzid)
|
2018-01-11 15:00:07 +01:00
|
|
|
self:force_update()
|
|
|
|
end
|
|
|
|
|
|
|
|
function textclock:get_timezone()
|
|
|
|
return self._private.tzid
|
|
|
|
end
|
|
|
|
|
2019-11-29 06:33:44 +01:00
|
|
|
--- Set the clock's refresh rate.
|
|
|
|
--
|
2018-01-21 01:18:54 +01:00
|
|
|
-- @property refresh
|
2019-11-25 22:35:41 +01:00
|
|
|
-- @tparam number refresh How often the clock is updated, in seconds
|
2018-01-21 01:18:54 +01:00
|
|
|
|
2018-01-21 00:57:59 +01:00
|
|
|
function textclock:set_refresh(refresh)
|
|
|
|
self._private.refresh = refresh or self._private.refresh
|
2018-01-11 15:00:07 +01:00
|
|
|
self:force_update()
|
|
|
|
end
|
|
|
|
|
2018-01-21 00:57:59 +01:00
|
|
|
function textclock:get_refresh()
|
|
|
|
return self._private.refresh
|
2018-01-11 15:00:07 +01:00
|
|
|
end
|
|
|
|
|
2017-09-10 20:12:25 +02:00
|
|
|
--- Force a textclock to update now.
|
2019-11-29 06:33:44 +01:00
|
|
|
--
|
2019-06-06 22:32:53 +02:00
|
|
|
-- @method force_update
|
2017-09-10 20:12:25 +02:00
|
|
|
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.
|
|
|
|
--
|
2019-11-25 22:35:41 +01:00
|
|
|
-- @tparam[opt=" %a %b %d, %H:%M "] string format The time [format](#format).
|
2018-01-21 00:57:59 +01:00
|
|
|
-- @tparam[opt=60] number refresh How often to update the time (in seconds).
|
2019-11-25 22:35:41 +01:00
|
|
|
-- @tparam[opt=local timezone] string timezone The [timezone](#timezone) to use.
|
2016-05-23 06:46:01 +02:00
|
|
|
-- @treturn table A textbox widget.
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct wibox.widget.textclock
|
2018-01-21 01:18:54 +01:00
|
|
|
local function new(format, refresh, tzid)
|
2016-05-23 06:46:01 +02:00
|
|
|
local w = textbox()
|
2018-01-21 01:18:54 +01:00
|
|
|
gtable.crush(w, textclock, true)
|
2018-01-11 15:00:07 +01:00
|
|
|
|
|
|
|
w._private.format = format or " %a %b %d, %H:%M "
|
2018-01-21 00:57:59 +01:00
|
|
|
w._private.refresh = refresh or 60
|
2018-01-11 15:00:07 +01:00
|
|
|
w._private.tzid = tzid
|
2018-04-22 08:05:40 +02:00
|
|
|
w._private.timezone = tzid and TimeZone.new(tzid)
|
2018-01-11 15:00:07 +01:00
|
|
|
|
2016-05-26 23:41:23 +02:00
|
|
|
function w._private.textclock_update_cb()
|
2019-01-31 20:34:41 +01:00
|
|
|
local str = DateTime_new_now(w._private.timezone or TimeZone.new_local()):format(w._private.format)
|
2017-11-24 05:27:18 +01:00
|
|
|
if str == nil then
|
|
|
|
require("gears.debug").print_warning("textclock: "
|
|
|
|
.. "g_date_time_format() failed for format "
|
2018-01-11 15:00:07 +01:00
|
|
|
.. "'" .. w._private.format .. "'")
|
2017-11-24 05:27:18 +01:00
|
|
|
end
|
|
|
|
w:set_markup(str)
|
2018-01-21 00:57:59 +01:00
|
|
|
w._timer.timeout = calc_timeout(w._private.refresh)
|
2017-09-10 20:12:25 +02:00
|
|
|
w._timer:again()
|
2016-05-23 06:46:01 +02:00
|
|
|
return true -- Continue the timer
|
|
|
|
end
|
2018-01-11 15:00:07 +01:00
|
|
|
|
2018-01-21 00:57:59 +01:00
|
|
|
w._timer = timer.weak_start_new(refresh, w._private.textclock_update_cb)
|
2017-09-10 20:12:25 +02:00
|
|
|
w:force_update()
|
2016-05-23 06:46:01 +02:00
|
|
|
return w
|
|
|
|
end
|
|
|
|
|
|
|
|
function textclock.mt:__call(...)
|
2018-01-21 01:18:54 +01:00
|
|
|
return new(...)
|
2016-05-23 06:46:01 +02:00
|
|
|
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
|