Lua的UTC时间不一致。

Lua 5.1文档中写道:

如果格式以'!'开头,则日期以协调世界时格式化。

如果格式是%c,则的行为似乎是正确的

local date_1 = os.date("!%c")
local date_2 = os.date("%c")
print("utc date: "..date_1)
print("not utc date: "..date_2)

如果格式是*t,则的行为似乎被交换了

local time_1 = os.time(os.date("!*t"))
local time_2 = os.time(os.date("*t"))
print("should be utc time, but is not: "..time_1) -- this should be UTC, and is not
print("should not be utc time, but is: "..time_2) -- this should not be UTC, but is

日期测试来自:http://www.epochconverter.com/

为什么会这样?

点赞
用户1009479
用户1009479

os.date("!*t")os.date("*t") 返回的表格是正确的。我打印了 hour 字段。注意它们与 %c 格式一致:

local date_1 = os.date("!%c")
local date_2 = os.date("%c")
print("utc date: "..date_1)
print("not utc date: "..date_2)

print("utc date hour: " .. os.date("!*t").hour)
print("not utc date hour: " .. os.date("*t").hour)

在我的机器上输出(中国标准时间,UTC+08:00):

utc date: 02/06/15 02:02:29
not utc date: 02/06/15 10:02:29
utc date hour: 2
not utc date hour: 10

但是,os.time 接受表格作为输入,假设它是本地时间,并返回时代。因此,本地时间转换为真实时代,但未转换 utc 时间。

print(os.time{year=1970, month=1, day=1, hour=8})

在我的机器上输出 0

2015-02-06 02:06:52