Fix sunrise/sunset widget
This commit is contained in:
@@ -3,26 +3,24 @@
|
|||||||
-- data_source = "https://api.sunrise-sunset.org/"
|
-- data_source = "https://api.sunrise-sunset.org/"
|
||||||
-- type = "widget"
|
-- type = "widget"
|
||||||
-- author = "Sriram S V"
|
-- author = "Sriram S V"
|
||||||
-- version = "1.0"
|
-- version = "1.1"
|
||||||
-- foldable = "false"
|
-- foldable = "false"
|
||||||
|
|
||||||
local json = require "json"
|
local json = require "json"
|
||||||
local date = require "date"
|
|
||||||
local fmt = require "fmt"
|
local fmt = require "fmt"
|
||||||
|
|
||||||
function on_alarm()
|
function on_alarm()
|
||||||
local location=system:location()
|
local location=system:location()
|
||||||
local url="https://api.sunrise-sunset.org/json?lat="..location[1].."&lng="..location[2].."&date=today&formatted=1"
|
local url="https://api.sunrise-sunset.org/json?lat="..location[1].."&lng="..location[2].."&formatted=0"
|
||||||
|
|
||||||
http:get(url)
|
http:get(url)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function on_network_result(result)
|
function on_network_result(result)
|
||||||
local t = json.decode(result)
|
local t = json.decode(result)
|
||||||
|
|
||||||
local sunrise_time = date(t.results.sunrise):tolocal():fmt("%H:%M")
|
local sunrise_time = utc_to_local(parse_iso8601_datetime(t.results.sunrise))
|
||||||
local sunset_time = date(t.results.sunset):tolocal():fmt("%H:%M")
|
local sunset_time = utc_to_local(parse_iso8601_datetime(t.results.sunset))
|
||||||
|
|
||||||
ui:show_text(
|
ui:show_text(
|
||||||
aio:res_string("today", "Today")..":"..
|
aio:res_string("today", "Today")..":"..
|
||||||
@@ -30,3 +28,34 @@ function on_network_result(result)
|
|||||||
fmt.space(4).."⬇"..fmt.space(2)..sunset_time
|
fmt.space(4).."⬇"..fmt.space(2)..sunset_time
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function parse_iso8601_datetime(json_date)
|
||||||
|
local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%-]?)(%d?%d?)%:?(%d?%d?)"
|
||||||
|
local year, month, day, hour, minute,
|
||||||
|
seconds, offsetsign, offsethour, offsetmin = json_date:match(pattern)
|
||||||
|
local timestamp = os.time{year = year, month = month,
|
||||||
|
day = day, hour = hour, min = minute, sec = seconds}
|
||||||
|
local offset = 0
|
||||||
|
if offsetsign ~= '' and offsetsign ~= 'Z' then
|
||||||
|
offset = tonumber(offsethour) * 60 + tonumber(offsetmin)
|
||||||
|
if xoffset == "-" then offset = offset * -1 end
|
||||||
|
end
|
||||||
|
|
||||||
|
return timestamp + offset * 60
|
||||||
|
end
|
||||||
|
|
||||||
|
function utc_to_local(utctime)
|
||||||
|
local local_time_str = os.date("%H:%M", utctime)
|
||||||
|
local utc_time_str = os.date("!%H:%M", utctime)
|
||||||
|
|
||||||
|
local function time_to_seconds(timestr)
|
||||||
|
local hour, minute = timestr:match("(%d+):(%d+)")
|
||||||
|
return tonumber(hour) * 3600 + tonumber(minute) * 60
|
||||||
|
end
|
||||||
|
|
||||||
|
local local_seconds = time_to_seconds(local_time_str)
|
||||||
|
local utc_seconds = time_to_seconds(utc_time_str)
|
||||||
|
local delta = local_seconds - utc_seconds
|
||||||
|
|
||||||
|
return os.date("%H:%M", utctime + delta)
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user