textclock: Produce reproducible output (#2622)

When $SOURCE_DIRECTORY is set, we are most likely currently running the
examples test, i.e. generating images. These images end up in the
documentation.

To make the images reproducable, i.e. independent from the current time,
this commit makes the textclock honor $SOURCE_DATE_EPOCH if
$SOURCE_DIRECTORY is set.

See commit 9d7eaf02 for some more details.

Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2019-01-31 20:34:41 +01:00 committed by Daniel Hahler
parent 0b4a84ccf3
commit 5f2cdabc01
1 changed files with 12 additions and 1 deletions

View File

@ -17,6 +17,17 @@ local TimeZone = glib.TimeZone
local textclock = { mt = {} }
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
--- Set the clock's format
-- @property format
-- @tparam string format The new time format. This can contain pango markup
@ -87,7 +98,7 @@ local function new(format, refresh, tzid)
w._private.timezone = tzid and TimeZone.new(tzid)
function w._private.textclock_update_cb()
local str = DateTime.new_now(w._private.timezone or TimeZone.new_local()):format(w._private.format)
local str = DateTime_new_now(w._private.timezone or TimeZone.new_local()):format(w._private.format)
if str == nil then
require("gears.debug").print_warning("textclock: "
.. "g_date_time_format() failed for format "