Is there any way to get the millisecond of now, from the server in Universal Coordinated Time?
I know tick() exists but that gets the local timezone instead of UTC and os.time() doesn’t get the millisecond.
Something like C#'s DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
}
}
I want the exact millisecond, this has the same problem. os.time() * 1000 is basically doing DateTimeOffset.UtcNow.ToUnixTimeSeconds() * 1000 which isn’t what I’m looking for
using System;
public class Program
{
public static void Main()
{
Console.WriteLine(DateTimeOffset.UtcNow.ToUnixTimeSeconds() * 1000);
Console.WriteLine(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
}
}
1586988139000
1586988139913
See the difference? May not look a lot, but to me, it does.
os.time returns only whole number seconds. This is not what @Blockzez is looking for.
As for the OP, your best bet would be adding on local milliseconds like so: os.time() + tick()%1. The mod 1 bit essentially gets “only the decimal value.” You can’t expect milliseconds to always be accurate across Roblox, so less than a second off isn’t too bad.
local offset = os.time() - tick()
function GetTime()
return tick() + offset
end
GetTime will return UTC time with the same precision as tick(). It will be off by at most 1 second from actual UTC, due to the difference in precision between os.time() and tick().
The problem is latency. By the time you get the response, the reported time will be out of sync by however long it took for the response to be received. There are techniques like NTP for synchronizing clocks, but that’s mostly likely overkill for your use-case.
Maybe this is a better version of that since timezones are separated in hours right?
local function RoundNumber(Num,RoundNum)
return math.floor((Num/RoundNum)+.5)*RoundNum
end
local TimeOffset=RoundNumber(os.time()-tick(),3600)
local function GetTime()
return tick()+TimeOffset
end
edit: There are some timezones that are not separated by hours, just change the number 3600 to 900
local function RoundNumber(Num,RoundNum)
return math.floor((Num/RoundNum)+.5)*RoundNum
end
local TimeOffset=RoundNumber(os.time()-tick(),900)
local function GetTime()
return tick()+TimeOffset
end