Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua 4 script to convert seconds elapsed to days, hours, minutes, seconds

Tags:

time

lua

lua-4.0

I need a Lua 4 script that converts seconds elapsed since seconds = 0 into a D:HH:MM:SS formatted string. The methods I've looked at try to convert the number to a calendar date and time, but I just need the elapsed time since 0. It's okay if the day value increments into the hundreds or thousands. How do I write such a script?

like image 958
posfan12 Avatar asked Sep 16 '25 15:09

posfan12


1 Answers

This is similar to the other answers, but is shorter. The return line uses a format string in to display the result in D:HH:MM:SS format.

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
like image 144
Nicholas Chin Avatar answered Sep 18 '25 08:09

Nicholas Chin