Convert seconds in minutes, hours, days, years ...

Danilo S(egan dsegan at STOPSPAM.gmx.net
Thu Jan 16 08:47:30 EST 2003


Loko wrote:
> Hello
> 
> Isn't there an already package that contains functions to convert a
> number of seconds into year, days, hours, minutes and seconds ?
> 
If I understand correctly, you need the time module:

 >>> import time
 >>> time.localtime(10000000000)
(2014, 9, 7, 6, 50, 8, 6, 250, 1)
 >>> time.gmtime(10000000000)
(2014, 9, 7, 4, 50, 8, 6, 250, 0)
 >>> help(time)

The actual number of seconds is a number of seconds since the start of 
year 1970.

If I misunderstood, and you needed to convert seconds to fraction of 
hours, days, and so forth (but just one of them), you use the builtin 
float type:

 >>> seconds=3445561.0
 >>> mins=seconds/60
 >>> hours=mins/60
 >>> days=hours/24
 >>> years=days/365.2422
 >>> (seconds,mins,hours,days,years)
(3445561, 57426.01666666667, 957.10027777777782, 39.879178240740742, 0.10918557122024986)


> Do I have to write it ?
> 
I guess not.

Regards,
Danilo






More information about the Python-list mailing list