Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Lua获取当前系统时区_Lua_Lua Table - Fatal编程技术网

如何使用Lua获取当前系统时区

如何使用Lua获取当前系统时区,lua,lua-table,Lua,Lua Table,如何使用Lua获取当前系统的时区。(美国/山区)。我正在Linux操作系统上工作。我需要知道如何获得像(美国/山地、亚洲/孟买)这样的Linux系统。如何为该打印编写代码(操作系统日期(“%m/%d/%y%H:%m:%S%z',t0))=03/25/19 10:57:29太平洋夏令时我在美国华盛顿州西雅图 %z为您提供了时区,这可能足以满足您的需要,但请注意: 无法使用os.date(“%z”),因为其返回值的格式不可移植;特别是,Windows系统不使用strftime()的C99语义 或者,

如何使用Lua获取当前系统的时区。(美国/山区)。我正在Linux操作系统上工作。我需要知道如何获得像(美国/山地、亚洲/孟买)这样的Linux系统。如何为该打印编写代码(操作系统日期(“%m/%d/%y%H:%m:%S%z',t0))=
03/25/19 10:57:29太平洋夏令时
我在美国华盛顿州西雅图

%z
为您提供了时区,这可能足以满足您的需要,但请注意:

无法使用os.date(“%z”),因为其返回值的格式不可移植;特别是,Windows系统不使用strftime()的C99语义

或者,您可以执行以下操作来确定偏移的实际值:

local function get_timezone_offset(ts)
    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

参考资料:

您可以使用Luarock软件包:

然后

该库返回时区信息的功能有限:

> america_new_york = luatz.get_tz('America/New_York')
> for key,val in pairs(america_new_york:find_current(now)) do print(key,val) end
abbrind 4
isstd   false
isdst   true
isgmt   false
gmtoff  -14400
abbr    EDT
> europe_paris = luatz.get_tz('Europe/Paris')
> for key,val in pairs(europe_paris:find_current(now)) do print(key,val) end
abbrind 17
isstd   true
isdst   false
isgmt   true
gmtoff  3600
abbr    CET

要查询当前系统时区,请使用不带参数的
luatz.get_tz()
。我看不出有什么办法可以得到奥尔森时区的名字,但是你可以得到一些数据

> now = luatz.time()
> mytz = luatz.get_tz()
> mytz_info = mytz:find_current(now)
> mytz_info.abbr
EDT
> mytz_info.gmtoff
-14400
> mytz_info.isdst
true

你的区域设置是什么?你是指答案的第一行吗?我说的是西雅图,我可以更清楚地说是美国华盛顿州的西雅图。我很惊讶你能得到这样的日期格式。我得到了2019年3月25日星期一15:51:05-0400。
%c
输出取决于语言环境
locale | grep LC_TIME
outputs
LC_TIME=en_CA.utf-8
locale date_fmt
输出
%a%b%e%H:%M:%S%Z%Y
——您的输出是什么?@glennjackman
%c
不应该依赖于区域设置。它在@glennjackman上记录为
%c日期和时间(例如,09/16/98 23:48:10)
我在windows机器上,因此我的日期格式为短日期=
M/d/yyyy
和我的时间格式为长时间=
h:mm:ss tt
库是否具有查找当前系统时区的功能?快速阅读文档,但没有看到,但假设我忽略了它。。。看起来可能是
get_tz()
?时区名称,编号。偏移量和缩写,是的
> america_new_york = luatz.get_tz('America/New_York')
> for key,val in pairs(america_new_york:find_current(now)) do print(key,val) end
abbrind 4
isstd   false
isdst   true
isgmt   false
gmtoff  -14400
abbr    EDT
> europe_paris = luatz.get_tz('Europe/Paris')
> for key,val in pairs(europe_paris:find_current(now)) do print(key,val) end
abbrind 17
isstd   true
isdst   false
isgmt   true
gmtoff  3600
abbr    CET
> now = luatz.time()
> mytz = luatz.get_tz()
> mytz_info = mytz:find_current(now)
> mytz_info.abbr
EDT
> mytz_info.gmtoff
-14400
> mytz_info.isdst
true