Help: Convert Seconds into Hour

Hello guys,

I’ve been searching the forums about this problem of mine but I can’t find anything. My problem is how can I convert seconds into hours, then use it as a countdown timer. Just like in those games which uses energy refilling method. 

So far this is what I have:

local function checkSlotTime() local curTime = os.time () print ("os time: " .. curTime) local slotLastUse = upSlotUsed -- this is the time when the user last used the slot machine. print ("slot last used: " .. slotLastUse) local timeSince = (curTime - slotLastUse) if timeSince \>= 21600 then -- 6 hours cooldown print ("slot is ready") else print ("slot is NOT ready") end end 

what I want to achieve is convert the “slotLastUse” into hours if hours is not applicable, convert into minutes, if minutes is not applicable then, convert it  into seconds. Then display how long the user will have to wait in order to used the slot machine.

I get the logic to it, but I can’t get it into codes, because time and dates freaks me out. I cannot fully understand it well. 

Thanks in advance and for the help

Jam

Hi, I think a relativly simple deviding should do the trick.

[lua]

local modf = math.modf

function wrap_time(time_amount)

   local start_seconds = time_amount --start_seconds = 20000

   local start_minutes = modf(start_seconds/60) --start_minutes = 333

   local seconds = start_seconds - start_minutes*60 --seconds = 20

   local start_hours = modf(start_minutes/60) --start_hours = 5

   local minutes = start_minutes - start_hours*60 --minutes = 33

   local start_days = modf(start_hours/24) --start_days = 0

   local hours = start_hours - start_days*24 --hours = 5

   local wrapped_time = {days=start_days, hours=hours, minutes=minutes, seconds=seconds}

   return wrapped_time --returns 0, 5, 33, 20

end

[/lua]

Here’s Rob’s tute on deal with time:

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

thanks for the torbenratzlaff,

If it doesn’t bug you, how can I print in terminal the returned value? when I do print (wrapped_time) it print the address of the function on the memory

Thanks for the reply Panc, I did read Rob’s tutorial, but I didn’t see  on how to convert a string of seconds into hh:mm:ss format.

You can use the following:

[lua]

print( table.concat(wrap_time(20000), ", ") )

[/lua]

it doesn’t do anything :confused:

Try the print anything function I use: http://pastebin.com/2TwNNshh

Usage:

pr(wrap_time(20000), “wrapped time thingie”)

it returns syntax error on the terminal, expeting “)”. if put the function on a variable which can solve the error, but still it doesn’t return anything on the terminal.

P.S.

Is there another method regarding my problem, which is convert seconds string into hh:mm:ss format? because I was expecting a short block of code. And I can’t find any references about converting a string of seconds to hh:mm:ss format

I’m sorry, my fault. table.concat does not work with that kind of table -.-

But the function does what it should. Use this to let it print the values correct. (tested)

local time = wrap\_time(20000) print(time.days, time.hours, time.minutes, time.seconds)

Solved!! :slight_smile: Found a solution on other forums

It goes like this:

local function timeCount(numSec) local nSeconds = numSec if nSeconds == 0 then coolTime.text = "00:00:00"; else local nHours = string.format("%02.f", math.floor(nSeconds/3600)); local nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours\*60))); local nSecs = string.format("%02.f", math.floor(nSeconds - nHours\*3600 - nMins \*60)); coolTime.text = nHours..":"..nMins..":"..nSecs end end

Thanks for your time and effort guys :slight_smile:

Jam

Hi, I think a relativly simple deviding should do the trick.

[lua]

local modf = math.modf

function wrap_time(time_amount)

   local start_seconds = time_amount --start_seconds = 20000

   local start_minutes = modf(start_seconds/60) --start_minutes = 333

   local seconds = start_seconds - start_minutes*60 --seconds = 20

   local start_hours = modf(start_minutes/60) --start_hours = 5

   local minutes = start_minutes - start_hours*60 --minutes = 33

   local start_days = modf(start_hours/24) --start_days = 0

   local hours = start_hours - start_days*24 --hours = 5

   local wrapped_time = {days=start_days, hours=hours, minutes=minutes, seconds=seconds}

   return wrapped_time --returns 0, 5, 33, 20

end

[/lua]

Here’s Rob’s tute on deal with time:

http://www.coronalabs.com/blog/2013/01/15/working-with-time-and-dates-in-corona/

thanks for the torbenratzlaff,

If it doesn’t bug you, how can I print in terminal the returned value? when I do print (wrapped_time) it print the address of the function on the memory

Thanks for the reply Panc, I did read Rob’s tutorial, but I didn’t see  on how to convert a string of seconds into hh:mm:ss format.

You can use the following:

[lua]

print( table.concat(wrap_time(20000), ", ") )

[/lua]

it doesn’t do anything :confused:

Try the print anything function I use: http://pastebin.com/2TwNNshh

Usage:

pr(wrap_time(20000), “wrapped time thingie”)

it returns syntax error on the terminal, expeting “)”. if put the function on a variable which can solve the error, but still it doesn’t return anything on the terminal.

P.S.

Is there another method regarding my problem, which is convert seconds string into hh:mm:ss format? because I was expecting a short block of code. And I can’t find any references about converting a string of seconds to hh:mm:ss format

I’m sorry, my fault. table.concat does not work with that kind of table -.-

But the function does what it should. Use this to let it print the values correct. (tested)

local time = wrap\_time(20000) print(time.days, time.hours, time.minutes, time.seconds)