local odate = os.date
local HOUR_S = 3600 --1小时(秒)
local DAY_S = 86400 --1天(秒)
local DAY_FLUSH_S = 5 * 3600 --每日跨天时间(每日5点跨天)
--获取一个类型的时间版本号
local function edition(period, time, offset, utc)
local value = 0
if not time or time <= 0 then
time = os.time()
end
time = time + (utc*HOUR_S) - (offset or 0)
local t = odate("*t", time)
if period == "hour" then
value = time // HOUR_S
elseif period == "day" then
value = time // DAY_S
elseif period == "week" then
--19700101是星期四,周日为每周第一天
value = ((time // DAY_S) + 4) // 7
elseif period == "month" then
value = t.year * 100 + t.month
elseif period == "year" then
value = t.year
end
return value
end
-- 北京时间(时区:UTC+8:00)
-- 示例1
local day_version1 = edition("day", str_to_time("2024/01/06 15:51:53"), DAY_FLUSH_S, 8)
local day_version2 = edition("day", str_to_time("2024/01/06 16:05:00"), DAY_FLUSH_S, 8)
print(string.format("beijing same_day=%s(t1_weekday_version1=%s day_version2=%s)", day_version1==day_version2,day_version1, day_version2)) -- 输出 false 19728,19729
-- 示例2
day_version1 = edition("day", str_to_time("2024/01/06 04:58:53"), DAY_FLUSH_S, 8)
day_version2 = edition("day", str_to_time("2024/01/06 05:05:00"), DAY_FLUSH_S, 8)
print(string.format("beijing same_day=%s(t1_weekday_version1=%s day_version2=%s)", day_version1==day_version2,day_version1, day_version2)) -- 输出 true 19727,19728
-- 美国东部时间(时区:UTC-5:00)
-- 示例1
day_version1 = edition("day", str_to_time("2024/01/06 15:51:53"), DAY_FLUSH_S, -5)
day_version2 = edition("day", str_to_time("2024/01/06 16:05:00"), DAY_FLUSH_S, -5)
print(string.format("beijing same_day=%s(t1_weekday_version1=%s day_version2=%s)", day_version1==day_version2,day_version1, day_version2)) -- 输出 false 19728,19729
-- 示例2
day_version1 = edition("day", str_to_time("2024/01/06 04:58:53"), DAY_FLUSH_S, -5)
day_version2 = edition("day", str_to_time("2024/01/06 05:05:00"), DAY_FLUSH_S, -5)
print(string.format("beijing same_day=%s(t1_weekday_version1=%s day_version2=%s)", day_version1==day_version2,day_version1, day_version2)) -- 输出 true 19727,19728
11-20
997

10-30
4522
