Working with date tables in Lua
Let dt be the "date table".
For example, the value returned by os.date("*t") is a "date table".
How to normalize the "date table"
For example, after adding 1.5 hours to the current time
local dt = os.date("*t"); dt.min = dt.min + 90
you need to normalize the table fields.
function normalize_date_table(dt) return os.date("*t", os.time(dt)) end
This function returns a new date table that is equivalent to its dt argument, regardless of the value of the dt contents: whether it contains local or GMT time.
How to convert Unix time to "local date table"
dt = os.date("*t", ux_time)
How to convert Unix time to "GMT date" table
dt = os.date("!*t", ux_time)
How to convert a "local date table" to Unix time
ux_time = os.time(dt)
How to convert "GMT date table" to Unix time
-- for this conversion we need precalculated value "zone_diff" local tmp_time = os.time() local d1 = os.date("*t", tmp_time) local d2 = os.date("!*t", tmp_time) d1.isdst = false local zone_diff = os.difftime(os.time(d1), os.time(d2)) -- zone_diff value may be calculated only once (at the beginning of your program) -- now we can perform the conversion (dt -> ux_time): dt.sec = dt.sec + zone_diff ux_time = os.time(dt)
Egor skriptunoff
source share