Search code examples
timeluatimezone-offset

Why does a roundtrip Lua UTC os.date -> os.time -> os.date result in an HOUR Time difference?


A roundtrip of os.date -> os.time -> os.date in UTC does not seem to get back to the same time:

print(os.date( "!%a %b %d %H:%M:%S %Y"))
print(os.date("%c" , os.time( os.date("!*t", os.time() ) ) ))

Output:

Mon Apr 15 16:08:48 2024
Mon Apr 15 17:08:48 2024

What am I missing?

p.s. the first line is the correct time


Solution

  • i think for some reason the isdst (daylight savings) value got switched and if gave a 1 hour offset.

    edit: i did a bit more research and i figured that the os.time gets the UTC time as a date table and it thinks the UTC time should be summer time (since its currently summer time). and thats why the +1 hour. your best option is to set the isdst to nil because UTC time doesnt have daylight savings time.

    print(os.date("!%a %b %d %H:%M:%S %Y"))
    
    local dt = os.date("!*t",os.time())
    dt.isdst = nil
    print(os.date("%c", os.time(dt)))