我现在有这个代码
local day = os.date('%A')
local timesubtract = 8 --GMT -8 hours
local hour = os.date('%H')
local newtime = (day-timesubtract)显然,它不起作用。在过去的3-4个小时里,我一直在筛选论坛上的帖子,但一无所获
基本上,我需要调用特定时区的星期几。例如,今天是星期六,但是在世界上的其他地方可能仍然是星期五,如果偏移量设置为"timesubtract“,它将调用该时区的正确星期几。
发布于 2015-08-15 16:47:43
您可以使用os.time获得以秒为单位的时间(从过去的某个特定点开始),添加您的偏移量(以秒为单位),并使用os.date将其格式化为字符串(或表,随您喜欢)。
print(os.date("%c")) -- Print current time (08/15/15 10:45:55)
local seconds_since_xxx = os.time() -- Get current time in seconds
seconds_since_xxx = seconds_since_xxx - (60 * 60 * 11) -- Subtract 11 hours from time
print(os.date("%c", seconds_since_xxx)) -- Print calculated time (08/14/15 23:45:55)发布于 2015-08-15 17:03:39
function format_time(timestamp, format, tzoffset, tzname)
if tzoffset == "local" then -- calculate local time zone (for the server)
local now = os.time()
local local_t = os.date("*t", now)
local utc_t = os.date("!*t", now)
local delta = (local_t.hour - utc_t.hour)*60 + (local_t.min - utc_t.min)
local h, m = math.modf( delta / 60)
tzoffset = string.format("%+.4d", 100 * h + 60 * m)
end
tzoffset = tzoffset or "GMT-8"
format = format:gsub("%%z", tzname or tzoffset)
if tzoffset == "GMT-8" then
tzoffset = "-0800"
end
tzoffset = tzoffset:gsub(":", "")
local sign = 1
if tzoffset:sub(1,1) == "-" then
sign = -1
tzoffset = tzoffset:sub(2)
elseif tzoffset:sub(1,1) == "+" then
tzoffset = tzoffset:sub(2)
end
tzoffset = sign * (tonumber(tzoffset:sub(1,2))*60 +
tonumber(tzoffset:sub(3,4)))*60
return os.date(format, timestamp + tzoffset)
end
print (format_time(os.time(), "%A"))所以事实证明我想得太多了。这段代码提供了我需要的确切函数。
发布于 2015-08-15 20:29:44
另一种更短的方法
local offset= 8
function getServerDay()
local timeNow = os.date("*t")
timeNow.day = timeNow.day - 1
local timeYesterday = os.time(timeNow)
return tonumber(os.date("%H")) >= offset and os.date("%A") or os.date("%A", timeYesterday)
end
print(getServerDay())https://stackoverflow.com/questions/32022898
复制相似问题