我希望在Lua中获取特定时区的日期和时间,并以以下格式呈现:os.date("%a %b %d, %H:%M")
。
我知道加上感叹号("!")os.date("!%a %b %d, %H:%M")
会返回UTC时间,但是如何进行偏移以获取所需的时间?
在我的情况下,所需的时区是UTC+08:00。
我希望在Lua中获取特定时区的日期和时间,并以以下格式呈现:os.date("%a %b %d, %H:%M")
。
我知道加上感叹号("!")os.date("!%a %b %d, %H:%M")
会返回UTC时间,但是如何进行偏移以获取所需的时间?
在我的情况下,所需的时区是UTC+08:00。
os.date接受两个参数:
os.date ( [format [, time]] )
time
参数是以秒为单位的,可用于偏移返回值。
由于os.time()
以秒为单位返回当前时间,因此您可以简单地添加您的偏移量(8),乘以每分钟的秒数(60),再乘以每秒钟的分钟数(60)。
os.date( "!%a %b %d, %H:%M", os.time() + 8 * 60 * 60 )
> os.date( "%a %b %d, %H:%M")
Wed Mar 16, 09:33
> os.date( "!%a %b %d, %H:%M")
Wed Mar 16, 08:33
> os.date( "!%a %b %d, %H:%M", os.time() + 8 * 60 * 60 )
Wed Mar 16, 16:33
7.5
。