I am trying to sync a time zone to a game. I have managed to get it to sync to one timezone. But it can’t account for DST. Has anyone here had experience with timezones?
What do you mean by “syncing” a time zone?
Couldnt you use os.time() instead of tick(), if thats what you mean?
os.time is in unix time
tick is in local time
I mean making the in game time line up with a real timezone.
I want it to make all clients see the time in the EST/EDT timezone.
So if the player is in EST it will show them the est time? If the player is in CST it will show them CST time? As in it is 11:30 PM UTC, and it would show 7:30 PM EST for an EST person, and 6:30 PM for CST?
If you want all the players to see EST/EDT time, I’m pretty sure UTC time is 4 hours ahead of EST so you can get the UTC time and just subtract 4 hours from that.
No, I want all users who join to see the same timezone, I have that part down. I am trying to figure out how to make the UTC offset change when DST changes.
Oh, I understand now. So you want it to change when Daylight Savings changes.
I’d suggest looking at os (roblox.com), specifically os.date. os.date will return a table and one of the things in it is isdst
which is a bool for whether DST is currently active. Then you can adjust accordingly.
I hope this helps, but I gotta eat dinner.
Oh that is much simpler than I was thinking I will try it and if it works Ill mark your answer as a solution.
Unfortunately you can’t check if dst is active unless the users timezone is actually one of the dst timezones (as far as I know). For example isdst
in os.date("!*t")
will always be false as this is in universal time which does not recognize dst. With that aside, this small code will return a dictionary:
local offset = 4 -- if dst then 4 else 5
local dt = DateTime.fromUnixTimestamp(os.time(os.date('!*t')) - 60*60*offset):ToUniversalTime()
If you can figure out a way to see if dst is not active though you can change that 4 to a 5 (EST is 5 offset and EDT is 4 offset which is why this value is either 4 or 5 if you want to show specifically the eastern timezone)
You can also use the datetime formatting instead of getting a dictionary:
local offset = 4 -- if dst then 4 else 5
local formattedString = DateTime.fromUnixTimestamp(os.time(os.date('!*t')) - 60*60*offset):FormatUniversalTime("LL LTS", "en-us")
But the tricky part is detecting if daylight savings is active because like I said isdst
will always be false if the user’s local timezone does not recognize dst (as far as I know) which means you could only detect dst on users that are actually in dst timezones which is unhelpful. You could query an api and return the value but that could get messy.
--[[
Get the current time in UTC, then check if daylight savings time is in use, then convert the UTC time to EDT or EST respectively.
--]]
function getTime()
local time = os.time()
local utcTime = os.date("!*t", time)
local isDst = os.date("*t").isdst
local utcOffset = os.date("!*t").hour - os.date("*t").hour
local dstOffset = 1
if isDst then
dstOffset = 2
end
local localTime = os.date("*t", time + (dstOffset * utcOffset * 60 * 60))
return localTime
end
I can’t understand it and can’t make it work. It says it is 5:37 9/24 when it is 21:37 9/25. Can you walk me through your script?