You are not logged in.

#1 2009-08-09 06:01:42

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

[Solved] (Python) Convert seconds to hh:mm:ss format?

Is there a function to convert the integer 80 to the string '01:20'? Thanks.

Last edited by Boris Bolgradov (2009-08-09 06:08:41)

Offline

#2 2009-08-09 06:08:24

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: [Solved] (Python) Convert seconds to hh:mm:ss format?

Never mind... I found what I need neutral

Offline

#3 2009-08-09 14:05:57

jbusch
Member
Registered: 2009-01-27
Posts: 20

Re: [Solved] (Python) Convert seconds to hh:mm:ss format?

Care to share for us less python gifted individuals?

- J

Offline

#4 2009-08-09 14:34:32

buttons
Member
From: NJ, USA
Registered: 2007-08-04
Posts: 620

Re: [Solved] (Python) Convert seconds to hh:mm:ss format?

jbusch wrote:

Care to share for us less python gifted individuals?

- J

What you want is in the time module.

time.strftime("%Y-%m-%d %H.%M.%S")

Prints '2009-08-09 10.31.33' for me.

Rest of the formatting codes: http://docs.python.org/library/time.html

Also look at the datetime module for other representations.


Cthulhu For President!

Offline

#5 2009-08-09 14:58:44

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: [Solved] (Python) Convert seconds to hh:mm:ss format?

@buttons: I think you misread my question.
@jbusch: Sure, here's the code:

def humanize_time(secs):
    mins, secs = divmod(secs, 60)
    hours, mins = divmod(mins, 60)
    return '%02d:%02d:%02d' % (hours, mins, secs)
>>> humanize_time(90)
'00:01:30'
>>> humanize_time(6513)
'01:48:33'

Offline

#6 2009-08-09 17:16:06

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: [Solved] (Python) Convert seconds to hh:mm:ss format?

In [1]: import datetime
In [2]: str(datetime.timedelta(seconds=6513))
Out[2]: '1:48:33'

Regards,

raf

Offline

#7 2009-08-09 18:18:14

Boris Bolgradov
Member
From: Bulgaria
Registered: 2008-07-27
Posts: 185

Re: [Solved] (Python) Convert seconds to hh:mm:ss format?

Hehe, very nice! Thanks! big_smile

Offline

#8 2009-08-09 19:49:51

jbusch
Member
Registered: 2009-01-27
Posts: 20

Re: [Solved] (Python) Convert seconds to hh:mm:ss format?

Very nice.  Thank you. smile

Offline

Board footer

Powered by FluxBB