Lua 4 脚本将经过的秒数转换为天、小时、分钟、秒



我需要一个Lua 4脚本,它将自seconds = 0以来经过的秒数转换为D:HH:MM:SS格式的字符串。我看过的方法尝试将数字转换为日历日期和时间,但我只需要自0以来经过的时间。如果日值增加到数百或数千也没关系。如何编写这样的脚本?

这与其他答案类似,但更短。返回行使用 in 格式字符串以 D:HH:MM:SS 格式显示结果。

function disp_time(time)
local days = floor(time/86400)
local hours = floor(mod(time, 86400)/3600)
local minutes = floor(mod(time,3600)/60)
local seconds = floor(mod(time,60))
return format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
end

试试这个:

function disp_time(time)
local days = floor(time/86400)
local remaining = time % 86400
local hours = floor(remaining/3600)
remaining = remaining % 3600
local minutes = floor(remaining/60)
remaining = remaining % 60
local seconds = remaining
if (hours < 10) then
hours = "0" .. tostring(hours)
end
if (minutes < 10) then
minutes = "0" .. tostring(minutes)
end
if (seconds < 10) then
seconds = "0" .. tostring(seconds)
end
answer = tostring(days)..':'..hours..':'..minutes..':'..seconds
return answer
end
cur_time = os.time()
print(disp_time(cur_time))

我找到了一个能够适应的Java示例。

function seconds_to_days_hours_minutes_seconds(total_seconds)
local time_days     = floor(total_seconds / 86400)
local time_hours    = floor(mod(total_seconds, 86400) / 3600)
local time_minutes  = floor(mod(total_seconds, 3600) / 60)
local time_seconds  = floor(mod(total_seconds, 60))
if (time_hours < 10) then
time_hours = "0" .. time_hours
end
if (time_minutes < 10) then
time_minutes = "0" .. time_minutes
end
if (time_seconds < 10) then
time_seconds = "0" .. time_seconds
end
return time_days .. ":" .. time_hours .. ":" .. time_minutes .. ":" .. time_seconds
end

我必须创建一个具有以下特征的类似函数:

  • 持续时间(秒(
  • 精度:您希望看到多少个单位(最多 7 个(
  • 分隔符:默认值为空格 (" "(
  • 它还计算世纪
  • 如果零件为零,则省略

代码如下:

function SecToStr(a,b,c) -- Time in seconds, Precision, Delimiter
local a = math.ceil( math.abs(a) ) or 0
local b = b or 7
local c = c or ' '
if a == math.huge or a == -math.huge then
return ' ∞ '
end
local d = {3153600000,31536000,2592000,86400,3600,60,1}
local e = {'C','Y','M','D','h','m','s'}
local g = {}
for i, v in ipairs(e) do
local h = math.floor( a / d[i] )
if h ~= h then
return table.concat(g, c)
end
if b > 0 and ( h > 0 or #g > 0) then
a = a - h * d[i]
if h > 0 then
table.insert(g, h..v)
end
b = h==0 and b or b - 1
end
end
return table.concat(g, c)
end

我希望它会有所帮助。

最新更新