Lua程序设计(第4版)课后练习 (第12章)

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())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lua程序设计第4》是一本非常经典的Lua编程书籍,它介绍了Lua程序设计的基本概念和技巧,并提供了大量的实例和练习,适合初学者和有一定编程基础的人阅读。 这本书的PDF本非常方便,可以在电子设备上随时随地阅读。拥有PDF格式的书籍,读者可以通过搜索、书签、标注等功能,更好地管理和查找自己感兴趣的内容。此外,PDF本还可以进行页面放大、缩小、翻转等操作,适应不同设备和阅读需求。对于学习Lua编程的人来说,这本书的PDF本无疑是很有帮助的。 《Lua程序设计第4》从基础语法、数据类型、运算符等内容开始介绍Lua的基础知识,然后逐步深入到表、函数、模块等高级特性,还介绍了面向对象编程和异常处理等更高级的主题。通过阅读这本书,读者可以系统地学习Lua的各种语言特性和编程技巧,掌握Lua编程的基本原理和方法。 在阅读过程中,读者可以参考书中的实例代码进行练习,并通过实践来加深对Lua编程的理解和掌握。此外,书中还提供了一些练习题,可以帮助读者巩固所学知识,培养编程思维和解决问题的能力。 总之,《Lua程序设计第4》是一本很有价值的Lua编程书籍,提供了全面而深入的学习内容,适合想要学习Lua编程的读者阅读。PDF本的书籍具有便携性和便捷性,非常方便读者随时随地进行学习。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值