Surface {when} in local time for weather widget

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
This commit is contained in:
Arthur Axel 'fREW' Schmidt 2019-05-19 20:36:45 -07:00 committed by Nguyễn Gia Phong
parent 92a2138296
commit cf81341fc9
2 changed files with 25 additions and 1 deletions

View File

@ -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

View File

@ -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}"])