Skip to content

Commit 06c84c6

Browse files
committed
新增求时间差函数
1 parent 9c67468 commit 06c84c6

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

SomeTest/os_time/MyTimeTest.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "timespan"
12
print("=================os.time test===============")
23
print(os.time())
34
print(os.time({day=26,month=4,year=2018}))
@@ -16,4 +17,6 @@ print(os.date("%Y"))
1617
-- 显示当前是一年中的第几周
1718
print(os.date("%U"))
1819
-- 组合格式化时间
19-
print(os.date("%Y-%m-%d, %H:%M:%S",os.time()))
20+
print(os.date("%Y-%m-%d, %H:%M:%S",os.time()))
21+
22+
print(getRemainTime("24:00:00"))

SomeTest/os_time/timespan.lua

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Split = function(szFullString,szSeprator)
2+
local nFindStartIndex = 1
3+
local nSplitIndex = 1
4+
local nSplitArray = {}
5+
while true do
6+
local nFindLastIndex = string.find(szFullString,szSeprator,nFindStartIndex)
7+
if not nFindLastIndex then
8+
nSplitArray[nSplitIndex] = string.sub(szFullString,nFindStartIndex,string.len(szFullString))
9+
break
10+
end
11+
nSplitArray[nSplitIndex] = string.sub(szFullString,nFindStartIndex,nFindLastIndex - 1)
12+
nFindStartIndex = nFindLastIndex + string.len(szSeprator)
13+
nSplitIndex = nSplitIndex + 1
14+
end
15+
return nSplitArray
16+
end
17+
18+
formatTime = function( timeStr )
19+
local splited = Split(timeStr, ":")
20+
local h = splited[1]
21+
local m = splited[2]
22+
return tonumber(h), tonumber(m)
23+
end
24+
25+
getRemainTime = function(timeStr)
26+
local h1, m1 = formatTime(timeStr)
27+
local curTime = os.date("*t", os.time())
28+
local curH = curTime.hour
29+
local curM = curTime.min
30+
31+
local remainH = h1 - curH
32+
local remainM = m1 - curM
33+
local totalRemainSeCond = remainH * 3600 + remainM * 60
34+
return totalRemainSeCond
35+
end

0 commit comments

Comments
 (0)