123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- local skynet = require "skynet"
- local moduleData = require("data.module")
- local root = {
- SEC_PER_HOUR = 3600,
- SEC_PER_DAY = 86400,
- SEC_PER_WEEK = 7 * 86400,
- REFRESH_HOUR = 5
- }
- -- 获取当前时间 单位秒
- function root.currentTime()
- return math.floor(skynet.time())
- end
- -- 获取玩家当前时间
- function root.now(uid)
- local currTime = skynet_time()
- if not IS_TEST then
- return currTime
- end
- local deltaTime = moduleData:hget_int(uid, "user", "time:sys")
- if not is_empty(deltaTime) then
- return currTime + deltaTime
- end
- return currTime
- end
- function root.toString(t)
- return os.date("%Y-%m-%d %H:%M:%S", t)
- end
- function root.toDate(t)
- return os.date("%Y-%m-%d", t or root.currentTime())
- end
- function root.toTab(t)
- return os.date("*t", t)
- end
- function root.toInt(tab)
- return os.time(tab)
- end
- function root.getYday(t)
- return os.date("%j", t)
- end
- -- 获取时间的0点时间戳
- function root.getCday(ctime)
- local a = root.toTab(ctime)
- a.hour, a.min, a.sec = 0, 0, 0
- return root.toInt(a)
- end
- -- 将时间格式化秒 %Y-%m-%d %H:%M:%S
- function root.getSecond(t)
- local function split(str, pat)
- local t = {}
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t, cap)
- end
- last_end = e + 1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local a = split(t, " ")
- local b = split(a[1], "-")
- local c = split(a[2], ":")
- return root.toInt({year = b[1], month = b[2], day = b[3], hour = c[1], min = c[2], sec = c[3]})
- end
- function root.getDayCount(year, month)
- local year = tonumber(year)
- local month = tonumber(month)
- if not year or not month then
- return 0
- end
- if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 then
- return 31
- end
- if month == 2 then
- if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
- return 29
- else
- return 28
- end
- end
- return 30
- end
- function root.getTime(year, month, day, hour, min, sec)
- local timeStruct = {}
- timeStruct.sec = tonumber(sec) or 0
- timeStruct.min = tonumber(min) or 0
- timeStruct.hour = tonumber(hour) or 0
- timeStruct.day = tonumber(day) or 0
- timeStruct.month = tonumber(month) or 0
- timeStruct.year = tonumber(year) or 0
- return root.toInt(timeStruct)
- end
- -- 能否日刷新
- function root.dailyRefresh(atime, btime, hour, min, sec)
- local atime = tonumber(atime) or 0
- local btime = tonumber(btime) or root.currentTime()
- local hour, min, sec = tonumber(hour) or 0, tonumber(min) or 0, tonumber(sec) or 0
- local aTimeTab, bTimeTab = root.toTab(atime), root.toTab(btime)
- bTimeTab.hour, bTimeTab.min, bTimeTab.sec = hour, min, sec
- aTimeTab.hour, aTimeTab.min, aTimeTab.sec = hour, min, sec
- -- 超过1天
- if btime - atime >= 24 * 60 * 60 then
- return true
- end
- -- 1天之内
- if btime >= root.toInt(aTimeTab) and root.toInt(aTimeTab) >= atime then
- return true
- end
- if btime >= root.toInt(bTimeTab) and root.toInt(bTimeTab) >= atime then
- return true
- end
- return false
- end
- -- 能否周刷新 [wday=2 是周一]
- function root.WeekRefresh(atime, btime, wday, hour, min, sec)
- atime = tonumber(atime) or 0
- btime = tonumber(btime) or root.currentTime()
- wday, hour, min, sec = tonumber(wday) or 2, tonumber(hour) or 0, tonumber(min) or 0, tonumber(sec) or 0
- local aTimeTab, bTimeTab = root.toTab(atime), root.toTab(btime)
- bTimeTab.day = bTimeTab.day + (wday - bTimeTab.wday)
- aTimeTab.day = aTimeTab.day + (wday - aTimeTab.wday)
- bTimeTab.hour, bTimeTab.min, bTimeTab.sec = hour, min, sec
- aTimeTab.hour, aTimeTab.min, aTimeTab.sec = hour, min, sec
- -- 超过七天
- if btime - atime >= 7 * 24 * 60 * 60 then
- return true
- end
- -- 七天之内
- if btime >= root.toInt(aTimeTab) and root.toInt(aTimeTab) >= atime then
- return true
- end
- if btime >= root.toInt(bTimeTab) and root.toInt(bTimeTab) >= atime then
- return true
- end
- return false
- end
- -- 能否月刷新
- function root.can_month_refresh(atime, btime, in_day, hour, min, sec)
- local atime = tonumber(atime) or 0
- local btime = tonumber(btime) or root.currentTime()
- local bTimeTab, aTimeTab = root.toTab(btime), root.toTab(atime)
- local day, hour, min, sec = tonumber(in_day) or 1, tonumber(hour) or 0, tonumber(min) or 0, tonumber(sec) or 0
- aTimeTab.day, aTimeTab.hour, aTimeTab.min, aTimeTab.sec = day, hour, min, sec
- bTimeTab.day, bTimeTab.hour, bTimeTab.min, bTimeTab.sec = day, hour, min, sec
- -- 超过一月
- local month = (bTimeTab.year - aTimeTab.year) * 12
- if bTimeTab.month + month > aTimeTab.month + 1 then
- return true
- end
- -- 一月之内
- if btime >= root.toInt(aTimeTab) and root.toInt(aTimeTab) >= atime then
- return true
- end
- if btime >= root.toInt(bTimeTab) and root.toInt(bTimeTab) >= atime then
- return true
- end
- return false
- end
- -- 两个时间相距天数
- function root.getDisDays(t1, t2)
- if t1 == nil or t2 == nil then
- return 0
- end
- local d1 = root.getYday(t1)
- local d2 = root.getYday(t2)
- return math.abs(d1 - d2)
- end
- function root.is_same_day(t1, t2)
- if t1 == nil or t2 == nil then
- return false
- end
- local d1 = root.getYday(t1)
- local d2 = root.getYday(t2)
- if d1 == d2 then
- return true
- end
- return false
- end
- -- 是否同一天(凌晨5点)
- function root.is_same_c5_day(t1, t2)
- if t1 == nil or t2 == nil then
- return false
- end
- local d1 = root.getYday(t1 - 5 * 3600)
- local d2 = root.getYday(t2 - 5 * 3600)
- if d1 == d2 then
- return true
- end
- return false
- end
- -- 获取当前天数(自1970年)
- function root.getDaysOfTimestampExt(timestamp, diffTime)
- timestamp = (timestamp or root.currentTime()) - (diffTime or 0)
- return math.floor((timestamp + (8 * 3600)) / (3600 * 24))
- end
- -- 获取获取相差的天数
- function root.getDisDaysC5(t1, t2)
- if t1 == nil or t2 == nil then
- return 0
- end
- local d1 = root.getDaysOfTimestampExt(t1, 5 * 3600)
- local d2 = root.getDaysOfTimestampExt(t2, 5 * 3600)
- return math.abs(d1 - d2)
- end
- -- 获取获取相差的天数
- function root.getDisDaysExt(t1, t2)
- if t1 == nil or t2 == nil then
- return 0
- end
- local d1 = root.getDaysOfTimestampExt(t1)
- local d2 = root.getDaysOfTimestampExt(t2)
- return math.abs(d1 - d2)
- end
- -- 获取下一个凌晨5点时间戳
- function root.get_next_c5_time(currTime)
- currTime = currTime or skynet_time()
- local startTime = root.getDayStartTime(currTime) + 5 * 3600
- if currTime >= startTime then
- return startTime + 24 * 3600
- end
- return startTime
- end
- -- 获取下周一开始时间 5点
- function root.c5_get_next_week_start_time(currTime)
- local date = os.date("*t", currTime)
- -- log.info("获取下周一5点时间 date[%s]", tostring(date))
- if date.wday == 1 and date.hour < 5 then
- return root.getTime(date.year, date.month, date.day, 5, 0, 0)
- end
- local diffDays
- local wday = date.wday - 1
- if wday == 0 then
- wday = 7
- end
- diffDays = 8 - wday
- -- log.info("diffDays = %s", diffDays)
- return root.getDayStartTime(currTime, diffDays) + 5 * 3600
- end
- -- 获取下个月1号开始时间 5点
- function root.c5_get_next_month_start_time(currTime)
- local date = os.date("*t", currTime)
- -- log.info("获取下周一5点时间 date[%s]", tostring(date))
- if date.day == 1 and date.hour < 5 then
- return root.getTime(date.year, date.month, date.day, 5, 0, 0)
- end
- date.month = date.month + 1
- return root.getTime(date.year, date.month, date.day, 5, 0, 0)
- end
- -- 两个时间间隔(凌晨5点)
- function root.get_c5_dis_days(t1, t2)
- if t1 == nil or t2 == nil then
- return 0
- end
- local d1 = root.getYday(t1 - 5 * 3600)
- local d2 = root.getYday(t2 - 5 * 3600)
- return math.abs(d1 - d2)
- end
- function root.isSameWeek(t1, t2)
- local dateA = os.date("*t", t1)
- local dateB = os.date("*t", t2)
- local diff = math.abs(dateA.yday - dateB.yday)
- if diff == 0 then
- return true
- end
- local beforeDt = 0
- if dateA.yday < dateB.yday then
- beforeDt = dateA
- else
- beforeDt = dateB
- end
- if diff > 7 then
- return false
- end
- local wday = beforeDt.wday - 1
- if wday == 0 then
- wday = 7
- end
- if wday + diff > 7 then
- return false
- end
- return true
- end
- function root.isSameMonth(t1, t2)
- if t1 == nil or t2 == nil then
- return false
- end
- local n1 = os.date("*t", t1)
- local n2 = os.date("*t", t2)
- if n1.year == n2.year and n1.month == n2.month then
- return true
- end
- return false
- end
- -- 获取当天结束时间
- function root.getDayEndTime(currTime, days)
- local daySec = 24 * 3600
- local addDay = days or 1
- local timeout = math.floor((currTime + 8 * 3600) / daySec) * daySec + daySec * addDay - 8 * 3600 - 1
- return timeout
- end
- -- 获取当天开始时间
- function root.getDayStartTime(currTime, days)
- local daySec = 24 * 3600
- local addDay = days or 0
- local timeout = math.floor((currTime + 8 * 3600) / daySec) * daySec + daySec * addDay - 8 * 3600
- return timeout
- end
- -- 获取当前周开始时间[周日开始]
- function root.getWeekStartTime(currTime)
- local t = root.toTab(currTime)
- local startTime = root.getDayStartTime(currTime)
- return startTime - (t.wday - 1) * root.SEC_PER_DAY
- end
- -- 获取当天周结束时间[周六结束]
- function root.getWeekEndTime(currTime)
- local startTime = root.getWeekStartTime(currTime)
- return startTime + root.SEC_PER_WEEK - 1
- end
- -- 获取当前周的指定时间的时间戳[从周日起]
- -- day周日为0,周六为6
- function root.getTimeByWeek(currTime, day, h, m, s)
- day = day or 0
- h = h or 0
- m = m or 0
- s = s or 0
- local startTime = root.getWeekStartTime(currTime)
- local addTime = day * root.SEC_PER_DAY + h * root.SEC_PER_HOUR + m * 60 + s
- return startTime + addTime
- end
- -- 获取现在时间是几星期几.day周日为0,周六为6
- function root.getWeekDay(currTime)
- local tab = os.date("*t", currTime or os.time())
- return tab.wday - 1
- end
- -- 获取当天0点时间戳
- function root.getUnixtimeToday(t1)
- local nt = os.date("*t", t1)
- return os.time({year = nt.year, month = nt.month, day = nt.day, hour = 0, min = 0, sec = 0})
- end
- -- 获取明天0点时间戳
- function root.getUnixtimeTomorrow(t1)
- return root.getUnixtimeToday(t1) + 86400
- end
- -- 明天零点倒计时
- function root.countdownToTomorrow(t1)
- return root.getUnixtimeTomorrow(t1) - (t1 or os.time())
- end
- -- 跨分钟数
- function root.get_across_minutes(startTime, endTime)
- if startTime == nil or endTime == nil then
- return 0
- end
- local s = math.floor(startTime / 60)
- local e = math.floor(endTime / 60)
- if endTime > e * 60 then
- e = e + 1
- end
- log.info(
- "get_across_minutes startTime[%s] s[%s] endTime[%s] e[%s]",
- tostring(startTime),
- tostring(s),
- tostring(endTime),
- tostring(e)
- )
- return math.abs(e - s)
- end
- -- 跨小时数(rate每多少整小时,可以是每整1小时,也可以是每整2小时)
- function root.get_across_hours(startTime, endTime, rate)
- if startTime == nil or endTime == nil then
- return 0
- end
- if not rate then
- rate = 3600
- end
- local s = math.floor(startTime / rate)
- local e = math.floor(endTime / rate)
- log.info(
- "get_across_hours startTime[%s] s[%s] endTime[%s] e[%s]",
- tostring(startTime),
- tostring(s),
- tostring(endTime),
- tostring(e)
- )
- return math.abs(e - s)
- end
- -- 跨天数
- function root.c5_get_cross_days(startTime, endTime)
- if startTime == nil or endTime == nil then
- return 0
- end
- local days = 0
- local nextTime = root.get_next_c5_time(startTime)
- while nextTime < endTime do
- days = days + 1
- nextTime = nextTime + 24 * 3600
- end
- return days
- end
- --#region ----------------------------------------- 时间的扩展:功能代码块
- -- 为什么重新实现?
- -- 部分功能实现得过于复杂,没必要过于遇合,需要考虑到lua的性能消耗.
- -- 获取当天0点时间戳
- function root.getTodayHourTimestamp(hour)
- local t_date = os.date("*t")
- return os.time(
- {
- year = t_date.year,
- month = t_date.month,
- day = t_date.day,
- hour = hour or root.REFRESH_HOUR,
- min = 0,
- sec = 0
- }
- )
- end
- -- 跨天数
- -- 获取某个时间戳跨越了多少天(5点为分界线)
- function root.getDaysOfTimestamp(timestamp)
- -- 错误的时间参数返回1天
- if not timestamp or timestamp == 0 then
- return 1
- end
- local dt = root.getTodayHourTimestamp()
- if timestamp > dt then
- -- 以一天来计算,可能
- -- return 888
- return math.ceil((timestamp - dt) / root.SEC_PER_DAY)
- else
- -- 逻辑刷新前面要加1.
- return math.ceil((dt - timestamp) / root.SEC_PER_DAY) + 1
- end
- end
- -- 是否已经跨天了
- function root.isOtherDay(uid, actData)
- -- 刷新时间
- local currTime = root.now(uid or 0)
- if actData.nextUT == nil or currTime >= actData.nextUT then
- actData.nextUT = root.get_next_c5_time(currTime)
- return true
- end
- return false
- end
- --#endregion
- return root
|