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

Well, that was another way indeed. Why didn’t I think of that. :man_facepalming: Thank you!!

2 Likes

It was very painful to write the logic for, since daylight savings time starts on the second sunday of March and ends on the first sunday of November! I actually came across this thread while searching for a way to get DST. There are ways of also doing this a lot more easily by using an API, but the problem with APIs is that they arent always reliable (occasional outages in service). But by using the raw math and logic you can calculate it by using the trusty os.date :cowboy_hat_face:

2 Likes

Right, I wanted to chime in here to present my solution:

function isDaylightSavings()
    local NOW = DateTime.now()
    local localtime = NOW.ToLocalTime()
    local localhour = localtime.Hour
    local unixhour = math.floor((tick()%86400)/60/60)
    return math.abs(unixhour-localhour) >= 1
end

This solution uses the fact that DateTime.ToLocalTime takes into account daylight savings, but a simple calculation from Unix time do not.

1 Like