Getting UTC UNIX timestamp in Lua

Ok, so you want the UTC time. Keep in mind that os.time actually knows nothing about timezones, so for example:

os.time(os.date("!*t"))
  1. Will get UTC time and populate table struct.
  2. Will convert table struct according to current timezone to unix timestamp.

So you actually would get your UNIX_TIME - TIMEZONE_OFFSET. If you are in GMT+5 you will get timestamp at UTC-5.

The correct way to do time conversion in lua is:

os.time() -- get current epoch value
os.time{ ... } -- get epoch value for local date/time values
os.date("*t"),os.date("%format") -- get your local date/time
os.date("!*t") or os.date("!%format") -- get UTC date/time
os.date("*t", timestamp),os.date("%format", timestamp) -- get your local date/time for given timestamp
os.date("!*t", timestamp) or os.date("!%format", timestamp) -- get UTC date/time for given timestamp

Kudos to Mons at https://gist.github.com/ichramm/5674287.

If you really need to convert any UTC date to timestamp, there's a good description on how to do this in this question: Convert a string date to a timestamp


You can use

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

to get the current UNIX epoch.