Have seconds, give me %h:%m:%s

Started by
2 comments, last by mdias 18 years, 7 months ago
How to convert milliseconds to format '%h:%m:%s'. E.g. I have 90 secs - '00:01:30', 60*60*40+90 secs - '40:01:30'. Maybe using strftime() function?
ai-blog.org: AI is discussed here.
Advertisement
To get minutes, divide seconds by 60, so (int)90/60 = 1
To get the seconds, get the remainder of 90/60 by using the modulus operator, so 90%60 = 30.

To get hours, hrm, divide seconds by 3600.

Edit: Sorry, I thought you said you had seconds, just convert the milliseconds into seconds by dividing by 1000.
Given s seconds:

m minutes = s/60
h hours = s/3600
ms milliseconds = s*1000

Solving these equations with respect to various variables gives you a way to find any two variables given one variable:

s = m*60
s = h*3600
s = ms/1000

m = (h*3600)/60
m = (ms/1000)/60

h = (m*60)/3600
h = (ms/1000)/3600

etc.

EDIT: Sorry, I think I misinterpreted your question. You have s seconds, and you want to express the total time in the format h/m/s_m, m being relative to h, s_m relative to m, correct?

Well, given s seconds, the number of total hours elapsed is

h = floor(s/3600)

The number of minutes relative to h is then:

m = floor((s - floor(s/3600) * 3600)/60)

And the number of seconds relative to m:

s_m = s - 60 * floor((s - floor(s/3600))/60)

So, your output will be:

[h]/[m]/[s_m]

all calculated with respect only to s.

[Edited by - nilkn on September 10, 2005 11:18:14 AM]
seconds = (miliseconds/1000) % 60;minutes = ((miliseconds/1000)/60) % 60;hours = ((miliseconds/1000)/60)/60;

This topic is closed to new replies.

Advertisement