How to detect DST (whether summer time is active) with scribunto

2.2k Views Asked by At

According to the current date, How I could know if it the hour is in summer time in Lua.

For example, the value in France would be actually true (CEST is currently used). During winter it would be false.
Forget is this is too specific: I am just expecting the rules for France. But at this point, I couldn't know if Lua has a native function.

The list of available builtins functions is here.

3

There are 3 best solutions below

2
On

The table returned by os.date("*t", someday) has a field isdst representing if it's daylight saving time.

local someday = os.time{year=2013, month=6, day=20}
local t = os.date("*t", someday)
print(t.isdst)

Getting the current time is easier, calling os.time without arguments will return the current date and time.

local now = os.time()
local t = os.date("*t", now)
print(t.isdst)
3
On

os.date("%Z") returns "CEST" if your system is using Central European Summer Time, which is what France is using now. Outside summer time, it returns "CET".

3
On

In addition to the other two answers (by @lhf and @Yu Hao) which are right, note that dealing correctly with time is complicated. If I were you I would consider using a library for it, such as Penlight. Note that even Penlight is not perfect but at least people are using it so issues get fixed eventually.