Formatting milliseconds to certain String

D

Deniz Dogan

Hello.
I need help with a small problem I'm having.

I want to make a function which takes an integer representing some time
in milliseconds and returns the same time but formatted as
"hours:minutes:seconds,milliseconds" with leading zeros whenever possible.

E.g. I input 185804 to the function and it returns 00:03:05,804. The
function I'm using now is defined as:

def millisToFormat(millis):
try:
hours = millis / 3600000
mins = (millis - hours * 3600000) / 60000
secs = (millis - hours * 3600000 - mins * 60000) / 1000
millis = millis - hours * 3600000 - mins * 60000 - secs * 1000
hours = str(hours)
mins = str(mins)
secs = str(secs)
millis = str(millis)
if len(hours) == 1:
hours = "0"+hours
if len(mins) == 1:
mins = "0"+mins
if len(secs) == 1:
secs = "0"+secs
if len(millis) < 3:
millis = (len(millis) - 3) * "0" + millis
return str(hours) + ":" + str(mins) + ":" + str(secs) + "," +
str(millis)
except ValueError:
return -1

Note that I am very new to Python and the language I have been using
most prior to this is Java.

--Deniz Dogan
 
D

Duncan Booth

Deniz Dogan said:
I want to make a function which takes an integer representing some
time in milliseconds and returns the same time but formatted as
"hours:minutes:seconds,milliseconds" with leading zeros whenever
possible.

E.g. I input 185804 to the function and it returns 00:03:05,804.

If you don't have to worry about more than 24 hours you could use
strftime:

import time
def fmt(t):
return time.strftime("%H:%M:%S",
time.gmtime(t/1000)) + ',%03d' % (t%1000)

print fmt(185804)

If you do, then much as you had it but using a format string and floor
division:

def fmt(t):
SEC = 1000
MIN = SEC * 60
HOUR = MIN*60
return "%02d:%02d:%02d,%03d" % (
t//HOUR,
(t%HOUR)//MIN,
(t%MIN)//SEC,
t%SEC)

Why did you write a function which returns a string normally and a
number when it failed? Use an exception to indicate an error condition,
not a number which you'll forget to check for.
 
R

Robin Becker

Deniz said:
Hello.
I need help with a small problem I'm having.

I want to make a function which takes an integer representing some time
in milliseconds and returns the same time but formatted as
"hours:minutes:seconds,milliseconds" with leading zeros whenever possible.

E.g. I input 185804 to the function and it returns 00:03:05,804. The
function I'm using now is defined as: ........

Note that I am very new to Python and the language I have been using
most prior to this is Java.

--Deniz Dogan

how about
.... hours, x = divmod(int(millis),3600000)
.... mins, x = divmod(x,60000)
.... secs, x = divmod(x,1000)
.... s = '%02d:%02d:%02d' % (hours, mins,secs)
.... if x: s += ',%03d' % x
.... return s
....
 
L

Laurent Pointal

Deniz Dogan a écrit :
Hello.
I need help with a small problem I'm having.

I want to make a function which takes an integer representing some time
in milliseconds and returns the same time but formatted as
"hours:minutes:seconds,milliseconds" with leading zeros whenever possible.

E.g. I input 185804 to the function and it returns 00:03:05,804. The
function I'm using now is defined as:

def millisToFormat(millis):
try:
hours = millis / 3600000
mins = (millis - hours * 3600000) / 60000
secs = (millis - hours * 3600000 - mins * 60000) / 1000
millis = millis - hours * 3600000 - mins * 60000 - secs * 1000
hours = str(hours)
mins = str(mins)
secs = str(secs)
millis = str(millis)
if len(hours) == 1:
hours = "0"+hours
if len(mins) == 1:
mins = "0"+mins
if len(secs) == 1:
secs = "0"+secs
if len(millis) < 3:
millis = (len(millis) - 3) * "0" + millis
return str(hours) + ":" + str(mins) + ":" + str(secs) + "," +
str(millis)
except ValueError:
return -1

Note that I am very new to Python and the language I have been using
most prior to this is Java.

A shorter solution using % operator in Python:
.... hours = millis / 3600000
.... mins = (millis - hours * 3600000) / 60000
.... secs = (millis - hours * 3600000 - mins * 60000) / 1000
.... millis = millis - hours * 3600000 - mins * 60000 - secs * 1000
.... return "%(hours)02d:%(mins)02d:%(secs)02d,%(millis)03d"%locals()
....'00:03:05,804'

See chapter "3.6.2 String Formatting Operations" in the Python Library
Reference.

http://docs.python.org/lib/typesseq-strings.html

A+

Laurent.
 
J

John Machin

Hello.
I need help with a small problem I'm having.

I want to make a function which takes an integer representing some time
in milliseconds and returns the same time but formatted as
"hours:minutes:seconds,milliseconds" with leading zeros whenever possible.

E.g. I input 185804 to the function and it returns 00:03:05,804.


| >>> def fmt_millis(millis):
| ... """millis is an integer number of milliseconds.
| ... Return str hh:mm:ss,ttt
| ... """
| ... seconds, millis = divmod(millis, 1000)
| ... minutes, seconds = divmod(seconds, 60)
| ... hours, minutes = divmod(minutes, 60)
| ... return "%02d:%02d:%02d,%03d" % (hours, minutes, seconds,
millis)
| ...
| >>> fmt_millis(0)
| '00:00:00,000'
| >>> fmt_millis(1)
| '00:00:00,001'
| >>> fmt_millis(12345678)
| '03:25:45,678'
| >>> fmt_millis(45678)
| '00:00:45,678'
| >>> fmt_millis(1230000)
| '00:20:30,000'
| >>> fmt_millis(3600000)
| '01:00:00,000'
| >>> fmt_millis(3600000*24)
| '24:00:00,000'
| >>> fmt_millis(3600000*2400)
| '2400:00:00,000'
| >>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top