Murga-Projects Forums
converting seconds to %H:%M:%S - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: converting seconds to %H:%M:%S (/showthread.php?tid=404)


converting seconds to %H:%M:%S - mikshaw - 01-08-2011 02:55 AM

Maybe this has already been asked? I don't recall.
It seems like there's something wrong here...when I attempt to use os.date() to convert seconds into hours:minutes:seconds, the minutes and seconds work, but hours always displays 19 instead of 00.

The first button gets the current Posix time string.
The second button gets a new time string (assuming you paused after clicking the first button), subtracts the first time string from that, and then is supposed to convert the difference into %T format (a.k.a. %H:%M:%S). That conversion is where my problem lies...it looks like this should have been easy.


Code:
#!/usr/bin/env murgaLua

win = fltk:Fl_Window(400, 300, "os.date(), os.time()")

start_button = fltk:Fl_Button(10, 10, 200, 25, "start")
starttime_display = fltk:Fl_Box(220, 10, 200, 25, "start time")
start_button:callback(
  function()
    starttime = os.time()
    starttime_display:label(starttime)
    endtime_display:label("end time")
  end
)

end_button = fltk:Fl_Button(10, 40, 200, 25, "stop")
endtime_display = fltk:Fl_Box(220, 40, 200, 25, "end time")
end_button:callback(
  function()
    endtime = os.time()
    elapsed = endtime - starttime
    endtime_display:label(endtime)
    elapsed_display:label(os.date("%T", elapsed))
--    elapsed_display:label(os.date("%H:%M:%S", elapsed))
  end
)

elapsed_display = fltk:Fl_Box(220, 80, 200, 200, "time elapsed")

win:show()
Fl:run()


Trying to be better at using whitespace...even though it adds a few bytes, it's easier to read =o)


RE: converting seconds to %H:%M:%S - mikshaw - 01-08-2011 03:18 AM

Apparently the os.date() function doesn't do what I thought it did. I found this function posted on a message board:

Code:
function SecondsToClock(sSeconds)
local nSeconds = tonumber(sSeconds)
if nSeconds == 0 then
return "00:00:00";
else
nHours = string.format("%02.f", math.floor(nSeconds/3600));
nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours*60)));
nSecs = string.format("%02.f", math.floor(nSeconds - nHours*3600 - nMins *60));
return nHours..":"..nMins..":"..nSecs
end
end