aboutsummaryrefslogtreecommitdiffstats
path: root/lib/weather.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/weather.lua')
-rw-r--r--lib/weather.lua22
1 files changed, 16 insertions, 6 deletions
diff --git a/lib/weather.lua b/lib/weather.lua
index 56cf92b..a30663f 100644
--- a/lib/weather.lua
+++ b/lib/weather.lua
@@ -106,9 +106,15 @@ end
function M.duration(s)
-- ponytail: anything over 1e9 s (about 31 years) is bad data, not a duration.
if type(s) ~= 'number' or not (s >= 0 and s < 1e9) then return '--' end
- local h, m = math.tointeger(math.floor(s / 3600)), math.tointeger(math.floor(s % 3600 / 60))
- if not (h and m) then return '--' end
- return string.format('%dh %02dm', h, m)
+ return string.format('%dh %02dm', math.floor(s / 3600), math.floor(s % 3600 / 60))
+end
+
+-- Epoch as clock text. os.date raises on a float epoch (1.5) and on huge ones
+-- (1e17), and a raise is a blank dashboard, so those read '--' like missing.
+function M.clock(t, fmt)
+ if type(t) ~= 'number' then return '--' end
+ local ok, s = pcall(os.date, fmt or '%H:%M', t)
+ return ok and s or '--'
end
-- Threshold role names, which index the palette (colors.ok and so on).
@@ -197,7 +203,11 @@ function M.parse(src)
for line in src:gmatch('[^\n]+') do
local f = {}
for field in (line .. '\t'):gmatch('([^\t]*)\t') do f[#f + 1] = field end
- local function n(i) return tonumber(f[i]) end
+ local function n(i)
+ local v = tonumber(f[i])
+ -- tonumber('1e999') is inf; NaN fails both comparisons.
+ if v and v > -math.huge and v < math.huge then return v end
+ end
local kind = f[1]
if kind == 'units' then
w.units = f[2]
@@ -234,9 +244,9 @@ end
-- Short age: "12m", "3h", "2d". A negative age (clock skew) reads as fresh.
function M.age_str(dt, now)
- if type(dt) ~= 'number' or type(now) ~= 'number' then return '?' end
+ if type(dt) ~= 'number' or type(now) ~= 'number' then return '--' end
local s = now - dt
- if s ~= s then return '?' end
+ if not (s > -math.huge and s < math.huge) then return '--' end
if s < 0 then s = 0 end
if s < 3600 then return math.floor(s / 60) .. 'm' end
if s < 86400 then return math.floor(s / 3600) .. 'h' end