Skip to content

Commit 794f5d3

Browse files
trap000dLuke Bonham
authored and
Luke Bonham
committedSep 11, 2017
Weather widget working without utc_offset
1 parent d070b7b commit 794f5d3

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed
 

‎widget/weather.lua

+17-15
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ local function factory(args)
2626
local current_call = args.current_call or "curl -s 'http://api.openweathermap.org/data/2.5/weather?id=%s&units=%s&lang=%s&APPID=%s'"
2727
local forecast_call = args.forecast_call or "curl -s 'http://api.openweathermap.org/data/2.5/forecast/daily?id=%s&units=%s&lang=%s&cnt=%s&APPID=%s'"
2828
local city_id = args.city_id or 0 -- placeholder
29-
local utc_offset = args.utc_offset or
30-
function ()
31-
local now = os.time()
32-
return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600)
33-
end
3429
local units = args.units or "metric"
3530
local lang = args.lang or "en"
3631
local cnt = args.cnt or 5
@@ -119,19 +114,26 @@ local function factory(args)
119114
local sunrise = tonumber(weather_now["sys"]["sunrise"])
120115
local sunset = tonumber(weather_now["sys"]["sunset"])
121116
local icon = weather_now["weather"][1]["icon"]
122-
local now = os.time()
117+
local loc_now = os.time()
123118
local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 }
124-
local offset = utc_offset()
125-
local utc_m = loc_m - offset
126-
127-
-- if we are 1 day after the GMT, return 1 day back, and viceversa
128-
if offset > 0 and (now - utc_m) >= 86400 then
129-
utc_m = utc_m + 86400
130-
elseif offset < 0 and (utc_m - now) >= 86400 then
131-
utc_m = utc_m - 86400
119+
local loc_t = os.difftime(loc_now, loc_m)
120+
local loc_d = os.date("*t", loc_now)
121+
local utc_d = os.date("!*t", loc_now)
122+
local utc_now = os.time(utc_d)
123+
local offdt = (loc_d.isdst and 1 or 0) * 3600 + 100 * (loc_d.min - utc_d.min) / 60
124+
local offset = os.difftime(loc_now, utc_now) + offdt
125+
local offday = (offset < 0 and -86400) or 86400
126+
127+
if math.abs(loc_now - utc_now - offdt + loc_t) >= 86400 then
128+
utc_now = utc_now + offday
129+
end
130+
131+
if offday * (loc_now - utc_now - offdt) > 0 then
132+
sunrise = sunrise + offday
133+
sunset = sunset + offday
132134
end
133135

134-
if sunrise <= now and now <= sunset then
136+
if sunrise <= loc_now and loc_now <= sunset then
135137
icon = string.gsub(icon, "n", "d")
136138
else
137139
icon = string.gsub(icon, "d", "n")

‎wiki

Submodule wiki updated from 1e25033 to c221a27

0 commit comments

Comments
 (0)