datetimetimeluatimestampstrftime

Getting UTC UNIX timestamp in Lua


An API returns a timestamp as UNIX timestamp at UTC and I would like to know if this timestamp was more than x seconds ago. As expected, this works fine with os.time() - x > timestamp in UTC, but blows up in other timezones.

Unfortunately I can't find a good way solve this in lua.

os.date helpfully has the ! prefix (e.g. os.date("!%H:%M:%S")) to return time at UTC, but it seems that despite the documentation stating it supports all strftime options, this does not support the %s option. I have heard people mention that this is caused by Lua compile time options for a similar issue, but changing these is not possible as the interpreter is provided by the user.


Solution

  • You can use

    os.time(os.date("!*t"))

    to get the current UNIX epoch.