From cf81341fc92b4b58adf691fcd4711d8515f01564 Mon Sep 17 00:00:00 2001 From: Arthur Axel 'fREW' Schmidt Date: Sun, 19 May 2019 20:36:45 -0700 Subject: [PATCH] Surface {when} in local time for weather widget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Here's how I use this: local weather_widget = function( code, url, name ) local actual_widget = wibox.widget.textbox() local actual_tooltip = awful.tooltip({ objects = { actual_widget } }); actual_widget:buttons(awful.util.table.join( awful.button({}, 1, function () awful.spawn("firefox '" .. url .. os.time() .. "'") end) )) vicious.register(actual_widget, vicious.widgets.weather, function (widget, args) actual_tooltip:set_text( "City: " .. args["{city}"] .. "\nWind: " .. args["{windmph}"] .. "mph " .. "\nSky: " .. args["{sky}"] .. "\nHumidity: " .. args["{humid}"] .. "\nMeasured at: " .. os.date("%F %T", args["{when}"])) return name .. " " .. args["{tempf}"] .. "°F" end, 60 * 10, code) return actual_widget end --- README.md | 2 +- widgets/weather_all.lua | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d8ff255..27fbce5 100644 --- a/README.md +++ b/README.md @@ -505,7 +505,7 @@ Supported platforms: any having Awesome and `curl` installed. * Argument: the ICAO station code, e.g. `"LDRI"` * Returns a table with string keys: `${city}`, `${wind}`, `${windmph}`, `${windkmh}`, `${sky}`, `${weather}`, `${tempf}`, `${tempc}`, `${humid}`, - `${dewf}`, `${dewc}` and `${press}` + `${dewf}`, `${dewc}` and `${press}`, `${when}` ### vicious.widgets.wifi diff --git a/widgets/weather_all.lua b/widgets/weather_all.lua index 6bd4cfc..23502c4 100644 --- a/widgets/weather_all.lua +++ b/widgets/weather_all.lua @@ -6,6 +6,7 @@ -- {{{ Grab environment local tonumber = tonumber local math = { ceil = math.ceil } +local os = { date = os.date, difftime = os.difftime, time = os.time } local string = { match = string.match } local spawn = require"vicious.spawn" @@ -17,12 +18,22 @@ local helpers = require"vicious.helpers" -- vicious.widgets.weather local weather_all = {} +-- copied from http://lua-users.org/wiki/TimeZone +local function get_timezone_offset() + local ts = os.time() + local utcdate = os.date("!*t", ts) + local localdate = os.date("*t", ts) + localdate.isdst = false -- this is the trick + return os.difftime(os.time(localdate), os.time(utcdate)) +end + -- {{{ Weather widget type local function parse(stdout, stderr, exitreason, exitcode) -- Initialize function tables local _weather = { ["{city}"] = "N/A", + ["{when}"] = "N/A", ["{wind}"] = "N/A", ["{windmph}"] = "N/A", ["{windkmh}"] = "N/A", @@ -58,6 +69,19 @@ local function parse(stdout, stderr, exitreason, exitcode) _weather["{press}"] = -- Pressure in hPa string.match(stdout, "Pressure[%s].+%((.+)[%s]hPa%)") or _weather["{press}"] + local year, month, day, hour, min = + string.match(stdout, "(%d%d%d%d).(%d%d).(%d%d) (%d%d)(%d%d) UTC") + if year ~= nil then + local utctable = { + year = year, + month = month, + day = day, + hour = hour, + min = min, + } + _weather["{when}"] = os.time(utctable) + get_timezone_offset() + end + -- Wind speed in km/h if MPH was available if _weather["{windmph}"] ~= "N/A" then _weather["{windmph}"] = tonumber(_weather["{windmph}"])