In case anyone runs into this thread looking for a valid use for UTC time functions.
os.time()
returns the number of seconds since the unix epoch (January 1, 1970 12:00 AM UTC). This may be different than other Lua environments you’re used to. You should call this function on the server to save timestamps for things like “last logged on”.
On the client, you should call os.date("*t", some_time_value)
to get a table containing the year/month/day/etc for the value that os.time()
returned. The reason you should do this on the client is because the *t
option uses the client’s local timezone, as opposed to the !*t
option which will show the UTC date for the given timestamp.
For example:
local function pretty_date(date)
local dateString = "{year}-{month}-{day} {hour}:{min}"
local result = string.gsub(dateString, "{(%w+)}", date)
return result
end
-- the time I wrote this was May 18, 2018 11:48 AM PDT, these values will obviously be different.
local now = os.time() -- roughly 1526668592
print(pretty_date(os.date("*t", now))) -- 2018-5-18 11:48, this is the local time
print(pretty_date(os.date("!*t", now))) -- 2018-5-18 18:48, this is as a UTC date
If you need the UTC time on the client for exploit mitigation, you may as well use os.time()
. Any mechanism that we add into the engine would be possible to exploit just as easily as if you added a RemoteEvent that fires every second with the current time. Nothing on the client can ever be trusted, you should do as much mitigation on the server as possible and only use client-sided mitigations once you’ve already done everything else you can.
Edit (May 2021): Some time after this post was written, the DateTime API was introduced. I recommend using that for new work instead of os.time
/os.date
.