Last active
November 27, 2022 15:49
-
-
Save litefeel/0f6233f4e00403b05f6e9cd786869c12 to your computer and use it in GitHub Desktop.
time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local latency = 0 | |
local ServerTime = { | |
localTime = os.time(), | |
serverTime = os.time(), | |
-- 服务器时间-客户端时间 | |
} | |
function ServerTime.setServerTime(time) | |
latency = time - os.time() | |
-- localTime = os.time() | |
-- serverTime = time or os.time() | |
end | |
function ServerTime.getServerTime() | |
return os.time() + latency | |
end | |
return ServerTime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ServerTime = require('ServerTime') | |
local TimeUtil = { | |
ONE_DAY = 24 * 3600, | |
} | |
-- can not over 4294.967296 seconds | |
-- old clock | |
function TimeUtil.clock(old) | |
if not old then | |
return os.clock() | |
end | |
local now = os.clock() | |
if now < old then | |
-- 4294.967296 = math.pow(2, 32)/CLOCKS_PER_SEC | |
now = now + 4294.967296 | |
end | |
return now | |
end | |
-- format default %M:%S | |
function TimeUtil.formatCD(cd, format) | |
if cd < 0 then cd = 0 end | |
local date = { | |
year = 1972, month = 0, day = 0, hour = 0, min = 0, sec = 0, isdst = false, | |
} | |
local time = os.time(date) | |
-- 不同时区、是否夏令时间都会导致时间不是0000 | |
if os.date("%H%M", time) ~= "0000" then | |
date.isdst = true | |
time = os.time(date) | |
end | |
return os.date(format or "%M:%S", time + cd) | |
end | |
function TimeUtil.format(format, time) | |
return os.date(format or "%M:%S", time or ServerTime.getServerTime()) | |
end | |
-- format Y-M-D | |
function TimeUtil.parseYMD(str, sep) | |
local t = CommonFunc._stringSplit(str , sep or '-') | |
local date = { year = tonumber(t[1]), month = tonumber(t[2]), day = tonumber(t[3]), hour = 0, min = 0, sec = 0 } | |
return os.time(date) | |
end | |
-- format H:M:S | |
function TimeUtil.parseCDHMS(str, sep) | |
local t = CommonFunc._stringSplit(str, sep or ':') | |
local cd = tonumber(t[1]) * 3600 | |
if t[2] then | |
cd = cd + tonumber(t[2]) * 60 | |
end | |
if t[3] then | |
cd = cd + tonumber(t[3]) | |
end | |
return cd | |
end | |
function TimeUtil.inTime(time1, time2, now) | |
now = now or ServerTime.getServerTime() | |
return time1 < now and time2 > now | |
end | |
function TimeUtil.inTime2(date1, date2, time1, time2, now) | |
now = now or ServerTime.getServerTime() | |
local nowTime = TimeUtil.todayTime(now) | |
local nowDate = TimeUtil.date(now) | |
date1 = TimeUtil.date(date1) | |
date2 = TimeUtil.date(date2) | |
time1 = time1 > TimeUtil.ONE_DAY and TimeUtil.todayTime(time1) or time1 | |
time2 = time2 > TimeUtil.ONE_DAY and TimeUtil.todayTime(time2) or time2 | |
if time2 == 0 then | |
time2 = TimeUtil.ONE_DAY | |
date2 = date2 - TimeUtil.ONE_DAY | |
end | |
if date1 > nowDate or nowDate > date2 then | |
return false | |
end | |
if time1 <= time2 then | |
return time1 <= nowTime and nowTime <= time2 | |
end | |
return time1 <= nowTime or nowTime <= time2 | |
end | |
function TimeUtil.todayTime(time) | |
local dateT = os.date("*t", time) | |
return dateT.hour * 3600 + dateT.min * 60 + dateT.sec | |
end | |
function TimeUtil.date(time) | |
local dateT = os.date("*t", time) | |
dateT.hour = 0 | |
dateT.min = 0 | |
dateT.sec = 0 | |
return os.time(dateT) | |
end | |
return TimeUtil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment