我目前有这段代码
local day = os.date('%A')
local timesubtract = 8 --GMT -8 hours
local hour = os.date('%H')
local newtime = (day-timesubtract)
显然它不起作用。我过去3-4个小时一直在论坛上筛选帖子,但没有运气
基本上,我需要拨打特定时区的星期几。例如,今天是星期六,但是在世界其他地方它可能仍然是星期五,如果偏移设置在" timesubtract"它将为该时区调用一周中正确的日期。
答案 0 :(得分:4)
您可以使用os.time获取以秒为单位的时间(从过去的特定点开始),添加您的偏移量(以秒为单位)并将其格式化为字符串(或表,无论您喜欢什么){{3 }}
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)
答案 1 :(得分:0)
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"))
事实证明我在思考问题。此代码提供了我所需的确切功能。
答案 2 :(得分:0)
另一种更短的方法
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())
答案 3 :(得分:-1)
使用诸如luatz之类的库,它允许您在不同的时区执行复杂的日期数学运算。