Converting milliseconds to human time

rurpy at yahoo.com rurpy at yahoo.com
Fri Jan 6 18:35:17 EST 2006


"Max Erickson" <maxerickson at gmail.com> wrote in message
news:mailman.131.1136579584.27775.python-list at python.org...
> the hard way(in that you have to do it yourself):
>
> def prntime(ms):
> s=ms/1000
> m,s=divmod(s,60)
> h,m=divmod(m,60)
> d,h=divmod(h,24)
> return d,h,m,s

Or abstracted...

def decd (n, base):
        """
        Decompose numeric value 'n' into components k[0:n]
        such that n = sum (k[N-i]*base[M-i]) for i=0...N
        where N is the length of k and M is the length of
        base.

        Examples:

        To convert 310255 seconds to [days, hours, minutes, seconds]:

            decd (310255, [3600*24, 3600, 60, 1])
            [3, 14, 10, 55]

        To convert 86.182 hours to [days, hours, minutes, seconds]:

            decd (86.182, [24, 1, 1./60, 1./3600])
            [3.0, 14.0, 10.0, 55.0]

        To convert 78 (decimal) to binary:

            decd (78, [128, 64, 32, 16, 8, 4, 2, 1])
            [0, 1, 0, 0, 1, 1, 1, 0]

        To break a decimal number into digits:
            decd (463, [1000, 100, 10, 1])
            [0, 4, 6, 3]

        """
        r = []
        for b in base:
            d, n = divmod (n, b)
            r.append (d)
        return r




More information about the Python-list mailing list