Creating a date

Basic types / Date and time

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

-- Month counts from 1, the hour defaults to 12
local noon = os.time({year = 1945, month = 5,
                      day = 9})
-- noon = 1945-05-09 12:00

-- Midnight and other times are asked for
local midnight = os.time({year = 1945, month = 5,
                          day = 9, hour = 0})
-- midnight = 1945-05-09 00:00

local moment = os.time({year = 1945, month = 5,
                        day = 9, hour = 1,
                        min = 30})
-- moment = 1945-05-09 01:30

-- Trap: a day that does not exist moves on
local no_day = os.time({year = 1945, month = 2,
                        day = 30})
-- no_day = 1945-03-02 12:00

print("noon = " .. os.date(fmt, noon))
print("midnight = " .. os.date(fmt, midnight))
print("moment = " .. os.date(fmt, moment))
print("no_day = " .. os.date(fmt, no_day))

-- Year, month and day are required
local ok, err = pcall(os.time,
                      {year = 1945, month = 5})
-- ok = false
-- err = field 'day' missing in date table
print("ok = " .. tostring(ok))
print("err = " .. err)