From 42f103141854afe104a2a4cd2f22233f50f3cdcd Mon Sep 17 00:00:00 2001 From: Aleksandr Beliaev Date: Wed, 6 Sep 2017 13:28:18 +1200 Subject: [PATCH 1/5] Weather widget working without utc_offset --- widget/weather.lua | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/widget/weather.lua b/widget/weather.lua index c7563d5a..c386118a 100644 --- a/widget/weather.lua +++ b/widget/weather.lua @@ -121,14 +121,25 @@ local function factory(args) local icon = weather_now["weather"][1]["icon"] local now = os.time() local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 } - local offset = utc_offset() + local offset = now - os.time(os.date("!*t", now)) local utc_m = loc_m - offset - -- if we are 1 day after the GMT, return 1 day back, and viceversa - if offset > 0 and (now - utc_m) >= 86400 then - utc_m = utc_m + 86400 - elseif offset < 0 and (utc_m - now) >= 86400 then - utc_m = utc_m - 86400 + if offset > 0 then + if now - utc_m >= 86400 then + utc_m = utc_m + 86400 + end + if loc_m >= utc_m then + sunrise = sunrise + 86400 + sunset = sunset + 86400 + end + elseif offset < 0 then + if utc_m - now >= 86400 then + utc_m = utc_m - 86400 + end + if loc_m <= utc_m then + sunrise = sunrise - 86400 + sunset = sunset - 86400 + end end if sunrise <= now and now <= sunset then From 7cdeba797b6678eeac9e61b0694c9fb9a205b400 Mon Sep 17 00:00:00 2001 From: Aleksandr Beliaev Date: Thu, 7 Sep 2017 09:48:33 +1200 Subject: [PATCH 2/5] debug for weather widget --- widget/weather.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/widget/weather.lua b/widget/weather.lua index c386118a..78f2b452 100644 --- a/widget/weather.lua +++ b/widget/weather.lua @@ -4,7 +4,7 @@ * (c) 2015, Luke Bonham --]] - +local gears = require("gears") local helpers = require("lain.helpers") local json = require("lain.util").dkjson local focused = require("awful.screen").focused @@ -147,6 +147,8 @@ local function factory(args) else icon = string.gsub(icon, "d", "n") end +gears.debug.print_warning("Times:" .. now .." " .. offset .." ".. sunrise .. " " .. sunset .. " " .. loc_m .. " " .. utc_m) +gears.debug.print_warning("Weather icon:" .. icon) weather.icon_path = icons_path .. icon .. ".png" widget = weather.widget @@ -155,7 +157,6 @@ local function factory(args) weather.icon_path = icons_path .. "na.png" weather.widget:set_markup(weather_na_markup) end - weather.icon:set_image(weather.icon_path) end) end From 43b91149608bdaddb7de563135dc709cdfa5951e Mon Sep 17 00:00:00 2001 From: Aleksandr Beliaev Date: Fri, 8 Sep 2017 13:05:17 +1200 Subject: [PATCH 3/5] Weather widget rewritten --- widget/weather.lua | 52 ++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/widget/weather.lua b/widget/weather.lua index 78f2b452..1028c7c6 100644 --- a/widget/weather.lua +++ b/widget/weather.lua @@ -4,7 +4,8 @@ * (c) 2015, Luke Bonham --]] -local gears = require("gears") + +local gears = require("gears") -- <-- To Remove: for debug printing local helpers = require("lain.helpers") local json = require("lain.util").dkjson local focused = require("awful.screen").focused @@ -26,11 +27,6 @@ local function factory(args) 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'" 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'" local city_id = args.city_id or 0 -- placeholder - local utc_offset = args.utc_offset or - function () - local now = os.time() - return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600) - end local units = args.units or "metric" local lang = args.lang or "en" local cnt = args.cnt or 5 @@ -116,38 +112,40 @@ local function factory(args) weather_now, pos, err = json.decode(f, 1, nil) if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then + -- sunrise and sunset time in UTC reported by OWM local sunrise = tonumber(weather_now["sys"]["sunrise"]) local sunset = tonumber(weather_now["sys"]["sunset"]) local icon = weather_now["weather"][1]["icon"] - local now = os.time() + -- Current time in local TZ + local loc_now = os.time() + -- Local midnight time local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 } - local offset = now - os.time(os.date("!*t", now)) - local utc_m = loc_m - offset + -- Time since local midnight + local loc_t = os.difftime(loc_now, loc_m) + local loc_d = os.date("*t", loc_now) + local utc_d = os.date("!*t", loc_now) + local utc_now = os.time(utc_d) + -- Daylight saving time offset: daylight saving time + diff for a part-hour TZ + local offdt = (loc_d.isdst and 1 or 0) * 3600 + 100 * (loc_d.min - utc_d.min) / 60 + local offset = os.difftime(loc_now, utc_now) + offdt + local offday = (offset<0 and -86400) or 86400 + + if math.abs(loc_now - utc_now - offdt + loc_t) >= 86400 then + utc_now = utc_now + offday + end - if offset > 0 then - if now - utc_m >= 86400 then - utc_m = utc_m + 86400 - end - if loc_m >= utc_m then - sunrise = sunrise + 86400 - sunset = sunset + 86400 - end - elseif offset < 0 then - if utc_m - now >= 86400 then - utc_m = utc_m - 86400 - end - if loc_m <= utc_m then - sunrise = sunrise - 86400 - sunset = sunset - 86400 - end + -- if we are still 1 day before (or after) the GMT, go 1 day forward, and viceversa + if offday * (loc_now - utc_now - offdt) > 0 then + sunrise = sunrise + offday + sunset = sunset + offday end - if sunrise <= now and now <= sunset then + if sunrise <= loc_now and loc_now <= sunset then icon = string.gsub(icon, "n", "d") else icon = string.gsub(icon, "d", "n") end -gears.debug.print_warning("Times:" .. now .." " .. offset .." ".. sunrise .. " " .. sunset .. " " .. loc_m .. " " .. utc_m) +gears.debug.print_warning("Times:" .. loc_now .." " .. offset .." ".. sunrise .. " " .. sunset .. " " .. loc_m .. " " .. offdt) gears.debug.print_warning("Weather icon:" .. icon) weather.icon_path = icons_path .. icon .. ".png" From 04587e6fe02a8f0acf99660f9714fbf8198c83bc Mon Sep 17 00:00:00 2001 From: Aleksandr Beliaev Date: Mon, 11 Sep 2017 13:28:24 +1200 Subject: [PATCH 4/5] Debug removed for merging to master --- widget/weather.lua | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/widget/weather.lua b/widget/weather.lua index 1028c7c6..ff0499b0 100644 --- a/widget/weather.lua +++ b/widget/weather.lua @@ -5,7 +5,6 @@ --]] -local gears = require("gears") -- <-- To Remove: for debug printing local helpers = require("lain.helpers") local json = require("lain.util").dkjson local focused = require("awful.screen").focused @@ -112,20 +111,15 @@ local function factory(args) weather_now, pos, err = json.decode(f, 1, nil) if not err and type(weather_now) == "table" and tonumber(weather_now["cod"]) == 200 then - -- sunrise and sunset time in UTC reported by OWM local sunrise = tonumber(weather_now["sys"]["sunrise"]) local sunset = tonumber(weather_now["sys"]["sunset"]) local icon = weather_now["weather"][1]["icon"] - -- Current time in local TZ - local loc_now = os.time() - -- Local midnight time + local loc_now = os.time() local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 } - -- Time since local midnight local loc_t = os.difftime(loc_now, loc_m) local loc_d = os.date("*t", loc_now) local utc_d = os.date("!*t", loc_now) local utc_now = os.time(utc_d) - -- Daylight saving time offset: daylight saving time + diff for a part-hour TZ local offdt = (loc_d.isdst and 1 or 0) * 3600 + 100 * (loc_d.min - utc_d.min) / 60 local offset = os.difftime(loc_now, utc_now) + offdt local offday = (offset<0 and -86400) or 86400 @@ -134,7 +128,6 @@ local function factory(args) utc_now = utc_now + offday end - -- if we are still 1 day before (or after) the GMT, go 1 day forward, and viceversa if offday * (loc_now - utc_now - offdt) > 0 then sunrise = sunrise + offday sunset = sunset + offday @@ -145,8 +138,6 @@ local function factory(args) else icon = string.gsub(icon, "d", "n") end -gears.debug.print_warning("Times:" .. loc_now .." " .. offset .." ".. sunrise .. " " .. sunset .. " " .. loc_m .. " " .. offdt) -gears.debug.print_warning("Weather icon:" .. icon) weather.icon_path = icons_path .. icon .. ".png" widget = weather.widget From 35387517edb5230df20aeeb82b46bf908cfd2b61 Mon Sep 17 00:00:00 2001 From: Aleksandr Beliaev Date: Mon, 11 Sep 2017 16:51:52 +1200 Subject: [PATCH 5/5] quashed last 3 commits --- widget/weather.lua | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/widget/weather.lua b/widget/weather.lua index c386118a..ff0499b0 100644 --- a/widget/weather.lua +++ b/widget/weather.lua @@ -26,11 +26,6 @@ local function factory(args) 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'" 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'" local city_id = args.city_id or 0 -- placeholder - local utc_offset = args.utc_offset or - function () - local now = os.time() - return os.difftime(now, os.time(os.date("!*t", now))) + ((os.date("*t").isdst and 1 or 0) * 3600) - end local units = args.units or "metric" local lang = args.lang or "en" local cnt = args.cnt or 5 @@ -119,30 +114,26 @@ local function factory(args) local sunrise = tonumber(weather_now["sys"]["sunrise"]) local sunset = tonumber(weather_now["sys"]["sunset"]) local icon = weather_now["weather"][1]["icon"] - local now = os.time() + local loc_now = os.time() local loc_m = os.time { year = os.date("%Y"), month = os.date("%m"), day = os.date("%d"), hour = 0 } - local offset = now - os.time(os.date("!*t", now)) - local utc_m = loc_m - offset + local loc_t = os.difftime(loc_now, loc_m) + local loc_d = os.date("*t", loc_now) + local utc_d = os.date("!*t", loc_now) + local utc_now = os.time(utc_d) + local offdt = (loc_d.isdst and 1 or 0) * 3600 + 100 * (loc_d.min - utc_d.min) / 60 + local offset = os.difftime(loc_now, utc_now) + offdt + local offday = (offset<0 and -86400) or 86400 + + if math.abs(loc_now - utc_now - offdt + loc_t) >= 86400 then + utc_now = utc_now + offday + end - if offset > 0 then - if now - utc_m >= 86400 then - utc_m = utc_m + 86400 - end - if loc_m >= utc_m then - sunrise = sunrise + 86400 - sunset = sunset + 86400 - end - elseif offset < 0 then - if utc_m - now >= 86400 then - utc_m = utc_m - 86400 - end - if loc_m <= utc_m then - sunrise = sunrise - 86400 - sunset = sunset - 86400 - end + if offday * (loc_now - utc_now - offdt) > 0 then + sunrise = sunrise + offday + sunset = sunset + offday end - if sunrise <= now and now <= sunset then + if sunrise <= loc_now and loc_now <= sunset then icon = string.gsub(icon, "n", "d") else icon = string.gsub(icon, "d", "n") @@ -155,7 +146,6 @@ local function factory(args) weather.icon_path = icons_path .. "na.png" weather.widget:set_markup(weather_na_markup) end - weather.icon:set_image(weather.icon_path) end) end