212 lines
6.4 KiB
Lua
212 lines
6.4 KiB
Lua
|
------------------------------------------------
|
|||
|
--作者: 杨全福
|
|||
|
--日期: 2019-05-8
|
|||
|
--文件: Time.lua
|
|||
|
--模块: Time
|
|||
|
--描述: 时间相关函数
|
|||
|
------------------------------------------------
|
|||
|
--local socket = require "socket"
|
|||
|
local TimeUtils = CS.Thousandto.Core.Base.TimeUtils
|
|||
|
|
|||
|
--程序启动到现在的时间
|
|||
|
local L_RealtimeSinceStartup = 0.0
|
|||
|
--当前帧消耗的时间
|
|||
|
local L_DeltaTime = 0.0
|
|||
|
local L_IsInit = false
|
|||
|
local L_FrameCount = 0
|
|||
|
--开服时间[时间戳 秒]
|
|||
|
local L_OpenServerTime = 0
|
|||
|
--开服第一天0点到开服时间点的总秒数
|
|||
|
local L_ZeroToOpenServerSec = 0
|
|||
|
--服务器时区(秒)
|
|||
|
local L_ServerZoneOffset = 0;
|
|||
|
--客户端本地时区(秒)
|
|||
|
local L_ClientZoneOffset = 0;
|
|||
|
--时区差(秒:服务器时区-客户端本地时区) :在用os.date必须加上时区差
|
|||
|
local L_ZoneOffset = 0;
|
|||
|
|
|||
|
local L_NextDayUTCTime = 0;
|
|||
|
local Time = {}
|
|||
|
|
|||
|
--只在main.lua初始化的时候调用,其他地方不允许调用
|
|||
|
function Time.Start(startTime)
|
|||
|
if L_IsInit ~= false then
|
|||
|
return
|
|||
|
end
|
|||
|
L_RealtimeSinceStartup = startTime
|
|||
|
L_IsInit = true
|
|||
|
L_FrameCount = CS.UnityEngine.Time.frameCount
|
|||
|
L_ClientZoneOffset = os.difftime(os.time(), os.time(os.date("!*t", os.time())));
|
|||
|
end
|
|||
|
|
|||
|
--只在main.lua update的时候调用,其他地方不允许调用
|
|||
|
function Time.SetDeltaTime(deltaTime,realtimeSinceStartup,frameCount)
|
|||
|
L_RealtimeSinceStartup = realtimeSinceStartup;
|
|||
|
L_FrameCount = frameCount
|
|||
|
L_DeltaTime = deltaTime
|
|||
|
end
|
|||
|
|
|||
|
--获取程序启动到现在的时间
|
|||
|
function Time.GetRealtimeSinceStartup()
|
|||
|
return L_RealtimeSinceStartup
|
|||
|
end
|
|||
|
|
|||
|
--获取单帧消耗的时间
|
|||
|
function Time.GetDeltaTime()
|
|||
|
return L_DeltaTime
|
|||
|
end
|
|||
|
|
|||
|
--获取单帧消耗的时间
|
|||
|
function Time.GetFrameCount()
|
|||
|
return L_FrameCount
|
|||
|
end
|
|||
|
|
|||
|
--引用CS中TimeUtils类的函数StampToDateTime,把秒转换为以1970-1-1为基础的日期
|
|||
|
function Time.StampToDateTime(timeStamp,format)
|
|||
|
return TimeUtils.StampToDateTime(timeStamp,format)
|
|||
|
end
|
|||
|
|
|||
|
function Time.StampToDateTimeNotZone(timeStamp,format)
|
|||
|
return TimeUtils.StampToDateTimeNotZone(timeStamp,format)
|
|||
|
end
|
|||
|
|
|||
|
--获取相差多少天,参数为无时区时间戳
|
|||
|
function Time.GetDayOffset(startTime, curTime)
|
|||
|
return TimeUtils.GetDayOffsetNotZone(startTime + L_ServerZoneOffset, curTime + L_ServerZoneOffset)
|
|||
|
end
|
|||
|
|
|||
|
--获取相差多少天,参数为有时区时间戳
|
|||
|
function Time.GetDayOffsetNotZone(startTime, curTime)
|
|||
|
return TimeUtils.GetDayOffsetNotZone(startTime, curTime)
|
|||
|
end
|
|||
|
|
|||
|
--获取服务器当前时间的秒数[时间戳 秒]
|
|||
|
function Time.GetNowSeconds()
|
|||
|
return math.floor(GameCenter.HeartSystem.ServerTime)
|
|||
|
end
|
|||
|
|
|||
|
--获取服务器当前时间[时间戳 秒]
|
|||
|
function Time.ServerTime()
|
|||
|
return GameCenter.HeartSystem.ServerTime
|
|||
|
end
|
|||
|
|
|||
|
---------------------------------------------------------------------------------------------
|
|||
|
--服务器发来的开服时间
|
|||
|
function Time.ResOpenServerTime(time)
|
|||
|
L_ServerZoneOffset = GameCenter.HeartSystem.ServerZoneOffset
|
|||
|
L_ZoneOffset = L_ServerZoneOffset - L_ClientZoneOffset;
|
|||
|
L_OpenServerTime = time * 0.001
|
|||
|
local _t = os.date("*t", math.floor(L_OpenServerTime) + L_ZoneOffset)
|
|||
|
L_ZeroToOpenServerSec = _t.hour * 3600 + _t.min * 60 + _t.sec
|
|||
|
end
|
|||
|
|
|||
|
--获取开服时间[时间戳 秒]
|
|||
|
function Time.GetOpenServerTime()
|
|||
|
return L_OpenServerTime
|
|||
|
end
|
|||
|
|
|||
|
--获取时区差(秒:服务器时区-客户端本地时区)
|
|||
|
function Time.GetZoneOffset()
|
|||
|
return L_ZoneOffset
|
|||
|
end
|
|||
|
|
|||
|
--把秒数据切割为天,时,分,秒
|
|||
|
function Time.SplitTime(timeData)
|
|||
|
local d = math.floor(timeData / 86400)
|
|||
|
timeData = timeData % 86400;
|
|||
|
local h = math.floor(timeData / 3600)
|
|||
|
timeData = timeData % 3600;
|
|||
|
local m = math.floor(timeData / 60)
|
|||
|
local s = math.floor(timeData % 60)
|
|||
|
return d,h,m,s
|
|||
|
end
|
|||
|
|
|||
|
-- {
|
|||
|
-- day = 13,
|
|||
|
-- hour = 16,
|
|||
|
-- isdst = false,
|
|||
|
-- min = 33,
|
|||
|
-- month = 11,
|
|||
|
-- sec = 59,
|
|||
|
-- wday = 6, [1-7 星期7到星期6, 西方国家的第一天是周天]
|
|||
|
-- yday = 318,
|
|||
|
-- year = 2020,
|
|||
|
-- }
|
|||
|
--获取当前时间(os.date返回值)
|
|||
|
function Time.GetNowTable()
|
|||
|
return os.date("*t", Time.GetNowSeconds() + L_ZoneOffset)
|
|||
|
end
|
|||
|
|
|||
|
--获取本月天数
|
|||
|
function Time.GetThisMonthTotalDays()
|
|||
|
local _t = Time.GetNowTable()
|
|||
|
local days = os.date("%d",os.time({year=_t.year, month=_t.month+1, day=0}))
|
|||
|
if days then
|
|||
|
return days
|
|||
|
end
|
|||
|
return 0
|
|||
|
end
|
|||
|
|
|||
|
--获取当前月份
|
|||
|
function Time.GetThisMonth()
|
|||
|
local _t = Time.GetNowTable()
|
|||
|
return tonumber(_t.month)
|
|||
|
end
|
|||
|
|
|||
|
--获取当前开服第几天
|
|||
|
function Time.GetOpenSeverDay()
|
|||
|
local _time = GameCenter.HeartSystem.ServerTime - Time.GetOpenServerTime() + L_ZeroToOpenServerSec
|
|||
|
return math.floor(_time/86400) + 1
|
|||
|
end
|
|||
|
|
|||
|
--获取开服第几天 openTime开服时间
|
|||
|
function Time.GetOpenSeverDayByOpenTime(openTime)
|
|||
|
local _t = os.date("*t", math.floor(openTime) + L_ZoneOffset)
|
|||
|
local _zeroToOpenServerSec = _t.hour * 3600 + _t.min * 60 + _t.sec
|
|||
|
local _time = GameCenter.HeartSystem.ServerTime - openTime + _zeroToOpenServerSec
|
|||
|
return math.floor(_time/86400) + 1
|
|||
|
end
|
|||
|
|
|||
|
--获取输入的时间是开服第几天 time:时间戳 秒
|
|||
|
function Time.GetToOpenSeverDay(time)
|
|||
|
return math.floor((time - Time.GetOpenServerTime() + L_ZeroToOpenServerSec)/86400) + 1
|
|||
|
end
|
|||
|
|
|||
|
--获取时间(时间戳 秒)
|
|||
|
function Time.GetTime(second, minute, hour, day, month, year)
|
|||
|
local _t = os.date("*t",Time.GetNowSeconds() + L_ZoneOffset);
|
|||
|
return os.time({year = year or _t.year, month = month or _t.month, day = day or _t.day, hour = hour or _t.hour, min = minute or _t.min, sec = second or _t.sec})
|
|||
|
end
|
|||
|
|
|||
|
--获取时间(时间戳 秒)minute整数类型1440>minute>=0
|
|||
|
function Time.GetTimeByMinute(minute)
|
|||
|
local _t = Time.GetNowTable()
|
|||
|
return os.time(
|
|||
|
{
|
|||
|
year = _t.year,
|
|||
|
month = _t.month,
|
|||
|
day = _t.day,
|
|||
|
hour = 0,
|
|||
|
min = minute,
|
|||
|
sec = 0
|
|||
|
}
|
|||
|
)
|
|||
|
end
|
|||
|
|
|||
|
--获取当前时间的秒数(0-86399)
|
|||
|
function Time.GetCurSeconds()
|
|||
|
local _t = os.date("*t",Time.GetNowSeconds() + L_ZoneOffset);
|
|||
|
return _t.hour * 3600 + _t.min * 60 + _t.sec
|
|||
|
end
|
|||
|
|
|||
|
--获取到下一天0点的总秒数
|
|||
|
function Time.GetToNextDaySeconds()
|
|||
|
local _curTime = Time.GetNowSeconds();
|
|||
|
if L_NextDayUTCTime == 0 or L_NextDayUTCTime < _curTime or L_NextDayUTCTime - _curTime > 86400 then
|
|||
|
local _t = os.date("*t", _curTime + L_ZoneOffset);
|
|||
|
L_NextDayUTCTime = _curTime - _t.hour * 3600 - _t.min * 60 - _t.sec + 86400;
|
|||
|
end
|
|||
|
return L_NextDayUTCTime - _curTime;
|
|||
|
end
|
|||
|
|
|||
|
return Time
|