Lua通過某一時間點獲取時間戳


-- * @description: 通過某一時間點獲取時間
-- * @params: @futureDays:0代表的意思是當天,1是明天,@_hour:指的24格式的時間,傳入2就是凌晨2點
-- * @return: 時間戳
function Tool:GetFutureTime(futureDays, _hour)
    local curTimestamp = os.time()
    local dayTimestamp = 24 * 60 * 60
    local newTime = curTimestamp + dayTimestamp * futureDays
    local newDate = os.date("*t", newTime)
    --這里返回的是你指定的時間點的時間戳
    return os.time({ year = newDate.year, month = newDate.month, day = newDate.day, hour = _hour, minute = newDate.minute, second = newDate.second })
end
self:GetFutureTime(1, 0)

打印結果:1607443200

進行拓展獲取剩余時間:

-- * @description: 轉換為天時間
-- * @params: @_time:秒數
-- * @return:返回剩余時間,格式如:05:07:16:33
-- * @date: 2020-12-7 11:10
function ToolKit:FormatUnixTime(_time)
    if _time and _time >= 0 then
        ---一天的秒數86400
        local day = math.floor(_time / 60 / 60 / 24)
        --小於10 需要十位補0
        local hour = math.floor(_time / 3600) % 24
        local minute = math.floor(_time / 60) % 60
        local second = math.floor(_time % 60)
        local dayStr = day < 10 and "0" .. day or day
        local hourStr = hour < 10 and "0" .. hour or hour
        local minuteStr = minute < 10 and "0" .. minute or minute
        local secondStr = second < 10 and "0" .. second or second
        return dayStr .. "" .. hourStr .. "" .. minuteStr .. "" .. secondStr
    end
end

通過string.format進行優化后:

-- * @description: 轉換為天時間
-- * @params: @_time:秒數
-- * @return:返回剩余時間,格式如:05:07:16:33
-- * @date: 2020-12-7 11:10
-- * @author: 雲更
function ToolKit:FormatUnixTime(_time)
    if _time and _time >= 0 then
        --一天的秒數86400
        local day = math.floor(_time / 86400)
        local hour = math.fmod(math.floor(_time / 3600), 24);
        local minute = math.fmod(math.floor(_time / 60), 60)
        local second = math.fmod(_time, 60)
        --小於10 需要十位補0 ,%02d 格式化為至少2位十進制整數 
        return string.format("%02d:%02d:%02d:%02d", day, hour, minute, second)
    end
end

 拓展:獲取通過某一時間點獲取時間(獲取隔周的時間)

-- * @description: 轉換為小時時間
-- * @params: @_time:秒數
-- * @return:返回剩余時間,格式如:07:16:33
-- * @date: 2020-12-7 11:10
function ToolKit:FormatHourUnixTime(_time)
    if _time and _time >= 0 then
        local hour = math.fmod(math.floor(_time / 3600), 24);
        local minute = math.fmod(math.floor(_time / 60), 60)
        local second = math.fmod(_time, 60)
        --小於10 需要十位補0 ,%02d 格式化為至少2位十進制整數 
        return string.format("%02d:%02d:%02d", hour, minute, second)
    end
end
-- * @description: 獲得明天零點的時間戳
-- * @params: @_timerStamp:當前的時間
-- * @return: 明天零點的時間戳
-- * @date: 2020-12-15 16:27
function ToolKit:GetNextDayZeroTimestamp(_timerStamp)
    --獲得當前的時間
    local timeStamp = _timerStamp
    if not timeStamp then
        timeStamp = os.time()
    end
    --獲得時間格式
    local formatTime = os.date("*t", timeStamp)
    formatTime.hour = 23
    formatTime.min = 59
    formatTime.sec = 59
    --獲得第二天零點的時間戳
    local nextTimestamp = os.time(formatTime) + 1
    return nextTimestamp
end

-- * @description: 獲得當天零點的時間戳
-- * @params: @_timerStamp:當前的時間
-- * @return: 明天零點的時間戳
-- * @date: 2020-12-15 16:27
function ToolKit:GetCurrentDayZeroTimestamp(_timerStamp)
    --獲得當前的時間
    local timeStamp = _timerStamp
    if not timeStamp then
        timeStamp = os.time()
    end
    --獲得時間格式
    local formatTime = os.date("*t", timeStamp)
    formatTime.hour = 0
    formatTime.min = 0
    formatTime.sec = 0
    --獲得第二天零點的時間戳
    local curTimestamp = os.time(formatTime)
    return curTimestamp
end

-- * @description: 通過某一時間點獲取時間(獲取隔周的時間戳)
-- * @params: @updateTime:更新時間(幾點0-24)
-- * @return: @residueStr:剩余天數(05:16:14:22(天:時:分:秒));@diffTimestamp:剩余時間
-- * @date: 2020-12-15 10:45
function ToolKit:GetFutureWeekTime(updateTime)
    if not updateTime then
        updateTime = 0
    end
    --獲得當前服務器的時間
    local curTimestamp = os.time()
    --獲得時間格式  {"day":15,"hour":15,"isdst":false,"min":34,"month":12,"sec":13,"wday":3,"yday":350,"year":2020}
    local curTimeFormat = os.date("*t", curTimestamp)
    local nextTimestamp = 0
    if curTimeFormat.hour < updateTime then
        --當時間小於更新時間
        nextTimestamp = ToolKit:GetCurrentDayZeroTimestamp(curTimestamp) + updateTime * 3600
    else
        --獲得第二天零點的時間戳
        nextTimestamp = ToolKit:GetNextDayZeroTimestamp(curTimestamp) + updateTime * 3600
    end
    --剩余天數
    local residueDay = 0
    --到本周末還有多少時間
    if curTimeFormat.wday == 1 then
        --周日
        residueDay = curTimeFormat.hour < updateTime and 6 or 7
    elseif curTimeFormat.wday == 2 then
        --周一
        residueDay = curTimeFormat.hour < updateTime and 7 or 1
    else
        residueDay = curTimeFormat.hour < updateTime and curTimeFormat.wday - 2 or curTimeFormat.wday - 1
    end
    residueDay = 7 - residueDay
    --轉換顯示格式
    local residueStr = ""
    local diffTimestamp = os.difftime(nextTimestamp, curTimestamp)
    local temp = ToolKit:FormatHourUnixTime(diffTimestamp)
    if residueDay > 0 then
        residueStr = string.format("%02d:%s", residueDay, temp)
    else
        residueStr = ToolKit:FormatUnixTime(diffTimestamp)
    end
    return residueStr, diffTimestamp
end

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM