How to check if we're in daylight savings time?

Hello! I am working on a game that is heavily depending on the real date & time to work properly. Cool, right?

Anyways. I am trying to find out if we’re in daylight saving time at the moment, thus doing some magic with the data to still match London’s time. :slight_smile:

I found this article on the devhub which provides this example:

-- September 16th, 1998 at 11:48:10 PM:
local timestamp = 906000490
local t = os.date("*t", timestamp)
-- Produces the following table, t:
-- {year = 1998, month = 9, day = 16, yday = 259, wday = 4,
-- hour = 23, min = 48, sec = 10, isdst = false}

isdst would be a boolean describing if daylight savings time is currently active. However, when I run this code in studio and check the results, it returns false? We are currently in daylight savings time if I am not mistaken?

How would I fix this? Thanks!

image

4 Likes

I did a quick test and from my results, if you are using "!*t" for the formatting, it print false for isdst, even if Daylight Savings Time is in effect, but if you use "*t", aka local time, it’ll correctly identify it. I had this code in a script

local utc = os.date("!*t",os.time())
local loc = os.date("*t",os.time())

print(utc.isdst)
print(loc.isdst)

And I got this as the print results

image

1 Like

Adding on to your answer UTC does not have DST and therefore would always respond with false:

UTC does not change with a change of seasons, but local time or civil time may change if a time zone jurisdiction observes daylight saving time or summer time. - Wikipedia

3 Likes

Thank you! I decided to try this, in studio it indeed prints true as you’re the “server”. However on a running gameserver that also prints “false”… How would I try to find this out?

2 Likes

I think you may need to do it locally? What happens if you try this in a localscript where it can run ingame?

local loc = os.date("*t",os.time())

print(loc.isdst)
1 Like

Just tried doing print(os.date("*t",tick()).isdst) on both the client and server and got the same answer (true) not sure what you are doing differently, if you could provide more information (screenshots, etc) that would be helpful.

2 Likes

Did you run it in studio or an actual game? Works perfectly like you said in studio but try publishing it into the game.

1 Like

Indeed works! How would I accomplish to get this data from a serversided script, though? :thinking:
image

I don’t know what you must do exactly to make it work for serversided scripts, but you could make a RemoteEvent to pass in isdst and do something with that information. You’ll probably need some checks as well if you don’t want people to fake being in DST or not, but up to you

2 Likes

I don’t think doing this is the best way going forward because you don’t take to account where the player is in the world, DST does not start at the same time in every country depending on where your player is the result will not be consistant.

2 Likes

Let’s go with a manual server variable then :sweat_smile: Just some boolvalue in our game settings. Thanks for the effort, though!

2 Likes

It’s dependent on what @OP wants to do specifically, if he wants to do something when a player is in DST, then it should work just by using a RemoteEvent and passing it if they’re in DST or not. But if it’s something that is done for the entire server, even if some player are not in, then the only real way is to do the manual method as @OP mentioned as I was typing it haha

3 Likes

Glad you eventually found a workaround for this after our efforts to help you! If you have anymore issues don’t be afraid to make another post!

2 Likes

Hey xJon_as!
After my last post I kept going at the problem and I think I found a weird solution here is what I did:

lcal utc_t = os.date("!*t", os.time()) -- Can't get DST from
local result = os.date("*t", os.time(utc_t)) -- Can get DST from

Hope whatever you are working on goes well :grinning_face_with_smiling_eyes:.
(edit: seems to not work outside of studio)

3 Likes

Would you be able to explain how exactly this works? :slight_smile:

1 Like

I tested it out right now using what @LearnCSharp, and I’m not sure if I did anything wrong but it wasn’t working for me,

(Server Script)
image

My code

local utc = os.date("!*t",os.time())
local loc = os.date("*t",os.time())
local utcloc = os.date("*t",os.time(utc))

print("UTC: ",utc.isdst)
print("Local: ",loc.isdst)
print("UTC Local: ",utcloc.isdst)

I even tried commenting out the loc lines and it still didn’t work for me. I probably did something wrong so I also tried directly copying and pasting what they did and adding pritn statements, and they sitll both printed false

I think at this point your best bet is to manually set a variable since I don’t think it’s possible for a server script to be able to get DST

2 Likes

Sorry!
This thing has been quite a headache especially with different behavior between studio and the live game, I agree by now just using a value that is manually set would be the best way going forward.

3 Likes

Last time I checked, the entire world stopped using daylight savings time.

3 Likes

I wish that was true, but we’re not there yet sadly.

2 Likes

Here ya go! When there’s a will, there’s a way.

local weekdayNumber = tonumber(os.date("%w"))
local daysSinceSunday
if weekdayNumber == 7 then
daysSinceSunday = 0
else
daysSinceSunday = weekdayNumber
end
local monthNumber = tonumber(os.date("%m")) 
local dayOfMonth = tonumber(os.date("%d"))	

local isDaylightSavingsTime

if (monthNumber > 3 and monthNumber < 11) or ((monthNumber == 3 and dayOfMonth >= 8) and 
(daysSinceSunday == 0 or dayOfMonth >= 14)) or ((monthNumber == 11 and dayOfMonth < 7 and 
daysSinceSunday > 0)) then
    isDaylightSavingsTime = true
else
    isDaylightSavingsTime = false
end
9 Likes