【发布时间】:2017-11-07 02:57:15
【问题描述】:
我希望使用 Lua 找到时区偏移量,但是我遇到了一些看起来很奇怪的行为,所以我一定遗漏了一些东西。
我正在使用代码:
local t1 = os.time();
local t2 = os.time( os.date( "!*t" ) );
print( t1, t2, t1 - t2 );
local t1 = os.time( os.date( "*t" ) );
local t2 = os.time( os.date( "!*t" ) );
print( t1, t2, t1 - t2 );
local t1 = os.date( "%c" );
local t2 = os.date( "!%c" );
print( t1, t2 );
local t1 = os.time( os.date( "*t", 86400 ) );
local t2 = os.time( os.date( "!*t", 86400 ) );
print( t1, t2, t1 - t2 );
local t1 = os.date( "*t" );
local t2 = os.date( "!*t" );
print( t1.hour, t1.isdst, t2.hour, t2.isdst );
print( ((t1.hour - t2.hour) * 60 + (t1.min - t2.min)) * 60 );
产生输出:
1496733916 1496730316 3600
1496733916 1496730316 3600
06/06/17 09:25:16 06/06/17 07:25:16
86400 82800 3600
9 true 7 false
7200
现在我位于 UTC+2(目前是夏令时,冬季是 UTC+1,CEST),所以我预计会看到 7200 秒的偏移量。但是,前两次尝试应该是等效的,并且它们是;只给我一小时的差距。当以人类可读格式打印时间时,可以清楚地看到两者之间的偏移量为 2 小时。第四次尝试使用固定时间点(一天中的 86400 秒数,来自this question 的技术),偏移量也是 1 小时)。最后,当直接减去小时(和分钟,以防整个小时偏移)我得到 2 小时偏移。
我怀疑这是由于夏令时或 dst。我想要完成的是从时间戳(已经在 UTC)获取时间,并且由于 os.time 是本地时间,我需要转换该时间戳,使其与本地时间匹配。
还是我完全错过了什么?
【问题讨论】: