Problem
我想修改awful.widget.textclock
Awesome-wm 中的小部件可以立即反映系统时区的更改。这个小部件和所有的 Awesome-wm 配置都是用 lua 编写的。
目前,如果系统时区发生更改,小部件将继续根据运行时设置的时区显示时间。该小部件使用os.time
函数检索时间,但这与系统时间不匹配。
解决方案如下
lua脚本:
local tz=require"luatz";
require "luatz.tzcache".clear_tz_cache()
print("Before Changes (America/Los_Angeles)")
print(os.date("!%H:%M",tz.time_in()))
os.execute("timedatectl set-timezone America/Chicago")
require "luatz.tzcache".clear_tz_cache()
print("America/Chicago")
print(os.date("!%H:%M",tz.time_in()))
os.execute("timedatectl set-timezone America/New_York")
require "luatz.tzcache".clear_tz_cache()
print("America/New_York")
print(os.date("!%H:%M",tz.time_in()))
Output:
Before Changes (America/Los_Angeles)
15:33
America/Chicago
17:33
America/New_York
18:33
解决方法
这可以通过重新启动很棒的窗口管理器来解决,这会导致小部件再次获得正确的时区。当然,如果时区发生变化,则需要再次重新启动窗口管理器。
期望的效果是在时区发生变化时从系统更新时区,无论是定期还是每次os.time
函数被调用。
Use Case
如果您好奇,它的用例是在笔记本电脑上。我经常旅行和跑步tzupdate
on a systemd timer
。我想自动更改我的时区。这工作得很好,只是实际显示时间的小部件没有注意到系统时区的变化。
到目前为止尝试过
- 取消设置“$TZ”环境变量。但 Arch Linux 从一开始就没有设置这个变量,所以我不确定 lua 是如何确定正确的时区的。
- Use the
luatz
图书馆,特别是tzcache.clear_tz_cache()
功能。这似乎没有效果。
- 使用以下函数检索系统时间
os.time()
: luatz.time()
and luatz.gettime.gettime()
。这些函数与其他函数检索相同的时间。
- Use the
luatz.time_in()
函数,但这会返回对 UTC 时间应用两次时区偏移的时间。luatz.time()
返回正确的本地时间,但应该返回 UTC 时间。
UPDATED luatz
我尝试搞乱luatz
按照推荐的库,但它似乎没有重新检查系统时区,即使在调用tzcache.clear_tz_cache()
功能。
我克隆了luatz
回购并复制luatz
到系统模块目录。该脚本似乎正确加载,但没有改变忽略系统时区更改的效果。据我所知,这与os.time()
功能。
luatz测试脚本:
local luatz = require "luatz"
local tzcache = require "luatz.tzcache"
local gettime = require "luatz.gettime"
print ("\nBefore Change - System TZ is ")
os.execute("timedatectl | grep 'Time zone' | awk '{ print $3 }'")
print("\nos.time(): "..os.date("%H:%M", os.time()))
print("luatz.time(): "..os.date("%H:%M", luatz.time()))
print("gettime..gettime(): "..os.date("%H:%M", gettime.gettime()))
print("\nTime zone changed to America/New_York")
os.execute("timedatectl set-timezone America/New_York")
tzcache.clear_tz_cache()
print ("\nAfter Change - System TZ is ")
os.execute("timedatectl | grep 'Time zone' | awk '{ print $3 }'")
print ("\nos.time(): "..os.date("%H:%M", os.time()))
print ("luatz.time(): "..os.date("%H:%M", luatz.time()))
print("gettime.gettime(): "..os.date("%H:%M", gettime.gettime()))
Output:
Before Change - System TZ is
America/Los_Angeles
os.time(): 11:54
luatz.time(): 11:54
gettime..gettime(): 11:54
Time zone changed to America/New_York
After Change - System TZ is
America/New_York
os.time(): 11:54
luatz.time(): 11:54
gettime.gettime(): 11:54
luatz.time_in()
So the luatz.time_in()
函数确实会随着系统时区的变化而更新,我对此感到兴奋!然而,time_in()
不显示正确的当地时间。它将时区偏移添加到正确的当地时间,导致时间晚了几个小时。我尝试设置TZ
环境变量,但这没有效果。因为某些原因,luatz.time()
正在返回当地时间并且luatz.time_in()
返回应用时区偏移两次的结果。
lua脚本:
local tz=require"luatz";
require "luatz.tzcache".clear_tz_cache()
print("Before Changes (America/Los_Angeles)")
print(os.date("%H:%M",tz.time_in()))
os.execute("timedatectl set-timezone America/Chicago")
require "luatz.tzcache".clear_tz_cache()
print("America/Chicago")
print(os.date("%H:%M",tz.time_in()))
os.execute("timedatectl set-timezone America/New_York")
require "luatz.tzcache".clear_tz_cache()
print("America/New_York")
print(os.date("%H:%M",tz.time_in()))
output:
Before Changes (America/Los_Angeles)
08:59
America/Chicago
10:59
America/New_York
11:59
实际系统当地时间:15:59
.