diff options
Diffstat (limited to 'lib/weather.lua')
| -rw-r--r-- | lib/weather.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/weather.lua b/lib/weather.lua index ac1f4cf..f8c7517 100644 --- a/lib/weather.lua +++ b/lib/weather.lua @@ -113,4 +113,28 @@ function M.arrow(deg) return ARROWS[i + 1] end +-- Fraction of daylight elapsed, clamped to 0..1. +-- +-- OWM returns TODAY's sunrise and sunset, so before dawn this is negative and +-- after dusk it is over 1. Clamping parks the dot at the corresponding end of +-- the arc, which is the cheap correct-enough behaviour; multi-day astronomy +-- buys nothing for a dot on a curve. +function M.sun_t(now, sunrise, sunset) + if type(now) ~= 'number' or type(sunrise) ~= 'number' + or type(sunset) ~= 'number' then return nil end + local span = sunset - sunrise + if span <= 0 then return 0 end -- polar day/night or bad data: no division + local t = (now - sunrise) / span + if t < 0 then return 0 elseif t > 1 then return 1 end + return t +end + +-- Whether the sun is up. Inclusive at both ends, matching the polybar script's +-- comparison, so the instant of sunrise draws the day face. +function M.is_day(now, sunrise, sunset) + if type(now) ~= 'number' or type(sunrise) ~= 'number' + or type(sunset) ~= 'number' then return true end + return now >= sunrise and now <= sunset +end + return M |
