lua程序设计(第四版)练习答案自做(第十二章)

仓库

12.1

#!/usr/bin/lua
function nextmonth(num)
	temp=os.date("*t",num)
	temp.month=temp.month+1
	return os.time(temp)
end

12.2

#!/usr/bin/lua
function wday(num)
	result=os.date("%w",num)
	return math.floor(result+1)
end

12.3

#!/usr/bin/lua
function dsec(num)
	temp=os.date("*t",num)
	sum=temp.hour*3600+temp.min*60+temp.sec
	return sum
end

12.4

#!/usr/bin/lua
function firstfriday(num)	--年用四位记法
	temp={year=num,month=1,day=1}
	temp=os.date("*t",os.time(temp))
	cur=temp.wday
	if cur<=6 then
		temp.day=temp.day+6-cur
	else
		temp.day=temp.day+6
	end
	temp=os.date("*t",os.time(temp))
	return temp.yday
end

12.5

#!/usr/bin/lua
function diffday(num1,num2)
	return math.floor(os.difftime(num1,num2)//(24*60*60))
end

12.6

#!/usr/bin/lua
function diffmonth(num1,num2)
	t1=os.date("*t",num1)
	t2=os.date("*t",num2)
	mon1=t1.month-t2.month
	year1=math.abs(t1.year-t2.year)
	mon1=year1*12+mon1
	return mon1
end

12.7

#!/usr/bin/lua
t1={year=2021,month=8,day=31}
t1=os.date("*t",os.time(t1))
t2=t1
t1.day=t1.day+1
t1=os.date("*t",os.time(t1))
t1.month=t1.month+1
t1=os.date("*t",os.time(t1))



t2.month=t2.month+1
t2=os.date("*t",os.time(t2))
t2.day=t2.day+1
t2=os.date("*t",os.time(t2))
print(os.date("%c",os.time(t1)))
print(os.date("%c",os.time(t2)))

不同,如果增加的一天导致月份发生变化时。因为每个月的天数不一定相同。

12.8

#!/usr/bin/lua
function zone(num)
	return os.date("%z",num)
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值