gpt4 book ai didi

time - Lua UTC 时间不一致

转载 作者:行者123 更新时间:2023-12-02 03:29:08 25 4
gpt4 key购买 nike

Lua 5.1 文档说:

If format starts with '!', then the date is formatted in Coordinated Universal Time.

如果格式是%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/

这是为什么?

最佳答案

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

关于time - Lua UTC 时间不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28357120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com