12.1
判断时间合法性
local function is_valid_date(date_string)
local year, month, day, hour, min, sec = date_string:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
if year and month and day and hour and min and sec then
month = tonumber(month)
if month >= 1 and month <= 12 then --判断月份,其余类似
return true
end
end
return false
end
加一个月
local function add_month(date_string)
if not is_valid_date(date_string) then
return nil
end
local year, month, day, hour, min, sec = date_string:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
month = month + 1
if month > 12 then
month = 1
year = year + 1
end
local new_date_string = string.format("%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec)
return new_date_string
end
--print("after_add_month:" .. add_month("2022-01-01 00:00:00"))
测试用例
-- 定义测试用例
local test_cases = {
{ input = "2022-01-01 00:00:00", expected_output = "2022-02-01 00:00:00" },
{ input = "2022-02-28 23:59:59", expected_output = "2022-03-28 23:59:59" },
{ input = "2022-12-31 12:34:56", expected_output = "2023-01-31 12:34:56" }
}
-- 运行测试用例
for i, test_case in ipairs(test_cases) do
local input = test_case.input
local expected_output = test_case.expected_output
local actual_output = add_month(input)
assert(actual_output == expected_output,
string.format("case %d failed : desire %s , obtain %s", i, expected_output, actual_output))
print(actual_output)
end
-- 如果代码运行没有错误,所有测试用例都通过了
print("all cases success")
12.2
local function get_weekday(date_string)
--local weekday_names = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
local year, month, day = date_string:match("(%d+)-(%d+)-(%d+)")
local weekday_number = os.date("*t", os.time({ year = year, month = month, day = day })).wday
return weekday_number
end
local test_cases_12_2 = {
{ input = "2023-10-31" },
{ input = "2023-11-1" },
{ input = "2023-11-2" },
{ input = "2023-11-3" },
{ input = "2023-11-4" },
{ input = "2023-11-5" },
{ input = "2023-11-6" },
}
for _, case in pairs(test_cases_12_2) do
print(get_weekday(case.input))
end
12.3
local function seconds_passed(time)
-- local date_time = os.date("%Y-%m-%d %H:%M:%S", time)
-- local hour, min, sec = date_time:match("(%d+):(%d+):(%d+)")
-- return hour * 3600 + min * 60 + sec
local t = os.date("*t", time)
local hour, min, sec = t.hour, t.min, t.sec
return hour * 3600 + min * 60 + sec
end
print(seconds_passed(os.time()))
12.4
local function first_friday(year)
local now_time = { year = year, month = 1, day = 1 }
local weekday = os.date("%w", os.time(now_time)) -- 1号是星期几,0星期天,1星期一
local result = 5 - weekday
if result < 0 then
result = 7 - (weekday - 5)
end
return result + 1
end
print(first_friday(2023))
12.5
local function days_between(date1, date2)
local year1, month1, day1 = date1:match("(%d+)-(%d+)-(%d+)")
local year2, month2, day2 = date2:match("(%d+)-(%d+)-(%d+)")
local time1 = os.time({ year = year1, month = month1, day = day1 })
local time2 = os.time({ year = year2, month = month2, day = day2 })
local diff = math.abs(os.difftime(time1, time2))
local days = math.floor(diff / (24 * 60 * 60))
return days
end
print(days_between("2023-12-15", "2023-12-31"))
12.6
local function months_between(date1, date2)
local time1 = os.time(date1)
local time2 = os.time(date2)
local diff = math.abs(os.difftime(time1, time2))
local months = math.floor(diff / (24 * 60 * 60 * 30))
return months
end
print(months_between({ year = 2022, month = 10, day = 1 }, { year = 2022, month = 12, day = 10 }))
12.8
local function get_os_zonetime()
return os.date("%z")
end
print(get_os_zonetime())