Арифметика дат

Простые типы / Дата и время

local fmt = "%Y-%m-%d %H:%M:%S"

-- Fields in, seconds out: the moment is fixed
local moment = os.time({year = 2027, month = 1,
                        day = 1, hour = 22,
                        min = 13, sec = 20})
-- moment = 2027-01-01 22:13:20

-- A moment counts in seconds
local yesterday = moment - 86400
-- yesterday = 2026-12-31 22:13:20
local tomorrow = moment + 86400
-- tomorrow = 2027-01-02 22:13:20
local in_one_hour = moment + 3600
-- in_one_hour = 2027-01-01 23:13:20

-- A month is not a fixed number of seconds:
-- change the field, os.time carries the rest
local parts = os.date("*t", moment)
parts.month = parts.month + 1
-- isdst is of the old month: os.time decides
parts.isdst = nil
local next_month = os.time(parts)
-- next_month = 2027-02-01 22:13:20

-- Trap: the 31st moves on into March
local late = {year = 2027, month = 1, day = 31}
late.month = late.month + 1
local from_jan31 = os.time(late)
-- from_jan31 = 2027-03-03 12:00:00

print("moment = " .. os.date(fmt, moment))
print("yesterday = " .. os.date(fmt, yesterday))
print("tomorrow = " .. os.date(fmt, tomorrow))
print("in_one_hour = " ..
      os.date(fmt, in_one_hour))
print("next_month = " .. os.date(fmt, next_month))
print("from_jan31 = " .. os.date(fmt, from_jan31))