Lua 关于时间的相关的方法

关于时间的相关的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function print_r ( t )  
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end

local nowTime = os.time()
print("当前时间戳:"..nowTime)

local nowDate = os.date("%Y-%m-%d %H:%M:%S", nowTime)
print("转换成时间:"..nowDate)

local nowTable = os.date("*t", nowTime)
print("时间戳转为table:")
print_r(nowTable)

print("table转为时间戳:".. os.time(nowTable))

print("自定义时间:"..os.time({year=2020, month=1, day=1,hour=0,min=0,sec=0}))

print("获取时间戳对应的日期")
--获取时间戳对应的天数(天数规则不是自然天,是(hour)4点前当前一天算)
function GetDayKeyByUnixTime(unixTime,hour)
if hour == nil then hour = 0 end
local retStr = os.date("%Y-%m-%d %H:%M:%S",unixTime)
local time = unixTime
local data = os.date("*t",time)
--dump(data)

--(hour)4点前按前一天算
if data.hour < hour then
time = time - 24*60*60
end

local data2 = os.date("*t",time)
--dump(data2)
data2.hour = 0
data2.min = 0
data2.sec = 0

local time2 = os.time(data2)

local dayKey = os.date("%Y%m%d",time2)
local timeBase = time2

--天数key,日期格式字符串,天数key 0点的时间戳
return dayKey,retStr,timeBase
end
local time1 = os.time({year=2020,month=1,day=1})
local time2 = os.time({year=2020,month=1,day=10})

local dayKey,str = GetDayKeyByUnixTime(time1,4)
print("dayKey = "..dayKey.." str = "..str)
dayKey,str = GetDayKeyByUnixTime(time2,4)
print("dayKey = "..dayKey.." str = "..str)

--两个时间的天数差 --时间戳1 时间戳2 多少点开始算第二天
function NumberOfDaysInterval(unixTime1,unixTime2,dayFlagHour)
if dayFlagHour == nil then dayFlagHour = 0 end
local key1,str1,time1 = GetDayKeyByUnixTime(unixTime1,dayFlagHour)
local key2,str2,time2 = GetDayKeyByUnixTime(unixTime2,dayFlagHour)

local sub = math.abs(time2 - time1)/(24*60*60)
print(str1.." 与 "..str2.."相差的天数:"..sub)

return sub
end

local sub = NumberOfDaysInterval(time1,time2,0)
print(sub)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
当前时间戳:1611828502
转换成时间:2021-01-28 18:08:22
时间戳转为table:
table: 0x13c89a0 {
[hour] => 18
[min] => 8
[wday] => 5
[day] => 28
[month] => 1
[year] => 2021
[sec] => 22
[yday] => 28
[isdst] => false
}

table转为时间戳:1611828502
自定义时间:1577808000
获取时间戳对应的日期
dayKey = 20200101 str = 2020-01-01 12:00:00
dayKey = 20200110 str = 2020-01-10 12:00:00
2020-01-01 12:00:00 与 2020-01-10 12:00:00相差的天数:9
9