Blog-like notes

Displaying time for different timezones in Awesome

Awesome, like most window managers or desktop environments, displays the current time somewhere on the screen. I want it to display also the current time in a different timezone than the local timezone.

As far as I know, there is nothing in Lua to do that, so I wrote a small helper module in C. It defines a single function, ctime_for_tz, which takes three arguments:

The function temporarily sets the TZ environment variable to the specified timezone, calls localtime(3) and strftime(3) to obtain and format the time in that timezone, then resets TZ to its previous value. It’s a bit clumsy, but I do not know of any easy way to obtain the local time of a specific timezone.

Here is the code of the C module:

chelpers.c (text/x-c, 1.1K)

Compile it with:

$ gcc -c -fPIC chelpers.c
$ gcc -shared -o chelpers.so chelpers.so

Then, in Awesome’s configuration script:

chelpers_lib = package.loadlib("chelpers.so", "luaopen_chelpers")
chelpers_lib()

Now you can create a new widget:

wdgt_wclock = widget({ type = "textbox" })
timer_wclock = timer({ timeout = 60 })
timer_wlock.add_signal("timeout", function()
    wdgt_clock.text = chelpers.ctime_for_tz("Europe/Athens", "Athens time: %H:%M")
end)
timer_wclock:start()

and add it somewhere in Awesome’s “wibox”.