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

Loko Loko at caramail.com
Fri Jan 17 03:24:31 EST 2003


Danilo S(egan <dsegan at STOPSPAM.gmx.net> wrote in message news:<b06dll$kim$1 at terra.news.tehnicom.net>...

> If I misunderstood, and you needed to convert seconds to fraction of 
> hours, days, and so forth (but just one of them)

that's what I needed.

>  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)


This is not exactly what I wanted,I want integers. Thus I wrote : 

   def Fract_Sec(s):
       temp = float()
       temp = float(s) / (60*60*24)
       d    = int(temp)
       temp = (temp - d) * 24
       h = int(temp)
       temp = (temp - h) * 60
       m = int(temp)
       temp = (temp - m) * 60
       sec = temp
       return d,h,m,sec
   #endef Fract_Sec

   s = 3445561.0
   d,h,m,s = Fract_Sec(s)
   print d,"days ",h,"hours",m,"mn",s,"sec."

   >>> 
   39 days  21 hours 6 mn 1.00000000015 sec


But I may need similar functions, fractionning level n into int(levels
n-1)+level n.
(that is, hours in days+hours, minutes into days,hours and minutes,
etc ...)

and also reverse ones (hours in minutes, in secondes).

Thoses functions are not really difficult to write but I think they
should be added into an official module.

However, I'll install v2.3 and check that datetime module.

Thanx.
Loko




More information about the Python-list mailing list