How do I convert seconds to hours, minutes and sec

2018-12-31 16:26发布

问题:

I have a function that returns information in seconds, but I need to store that information in hours:minutes:seconds.

Is there an easy way to convert the seconds to this format in Python?

回答1:

Or you can do:

>>> import datetime
>>> str(datetime.timedelta(seconds=666))
\'0:11:06\'


回答2:

By using the divmod() function, which does only a single division to produce both the quotient and the remainder, you can have the result very quickly with only two mathematical operations:

m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print \"%d:%02d:%02d\" % (h, m, s)


回答3:

I can hardly name that an easy way (at least I can\'t remember the syntax), but it is possible to use time.strftime, which gives more control over formatting:

>>> import time
>>> time.strftime(\"%H:%M:%S\", time.gmtime(666))
\'00:11:06\'

gmtime is used to convert seconds to special tuple format that strftime() requires.



回答4:

>>> import datetime
>>> \"{:0>8}\".format(datetime.timedelta(seconds=66))
>>> \"{:0>8}\".format(str(datetime.timedelta(seconds=66)))  # Python3
>>> \'00:01:06\' # good

and:

>>> \"{:0>8}\".format(datetime.timedelta(seconds=666777))
>>> \"{:0>8}\".format(str(datetime.timedelta(seconds=666777)))  # Python3
>>> \'7 days, 17:12:57\' # nice

without \':0>8\':

>>> \"{}\".format(datetime.timedelta(seconds=66))
>>> \'0:01:06\' # not HH:MM:SS

and:

>>> import time
>>> time.strftime(\"%H:%M:%S\", time.gmtime(666777))
>>> \'17:12:57\' # wrong

but:

>>> \"{:0>8}\".format(datetime.timedelta(seconds=620000))
>>> \"{:0>8}\".format(str(datetime.timedelta(seconds=620000)))  # Python3
>>> \'7 days, 4:13:20\' # bummer


回答5:

This is my quick trick:

import humanfriendly
humanfriendly.format_timespan(secondsPassed)

visit: https://humanfriendly.readthedocs.io/en/latest/#humanfriendly.format_timespan for more info.



回答6:

If you need to get datetime.time value, you can use this trick:

my_time = (datetime(1970,1,1) + timedelta(seconds=my_seconds)).time()

You cannot add timedelta to time, but can add it to datetime.

UPD: Yet another variation of the same technique:

my_time = (datetime.fromordinal(1) + timedelta(seconds=my_seconds)).time()

Instead of 1 you can use any number greater than 0. Here we use the fact that datetime.fromordinal will always return datetime object with time component being zero.



回答7:

This is how I got it.

def sec2time(sec, n_msec=3):
    \'\'\' Convert seconds to \'D days, HH:MM:SS.FFF\' \'\'\'
    if hasattr(sec,\'__len__\'):
        return [sec2time(s) for s in sec]
    m, s = divmod(sec, 60)
    h, m = divmod(m, 60)
    d, h = divmod(h, 24)
    if n_msec > 0:
        pattern = \'%%02d:%%02d:%%0%d.%df\' % (n_msec+3, n_msec)
    else:
        pattern = r\'%02d:%02d:%02d\'
    if d == 0:
        return pattern % (h, m, s)
    return (\'%d days, \' + pattern) % (d, h, m, s)

Some examples:

$ sec2time(10, 3)
Out: \'00:00:10.000\'

$ sec2time(1234567.8910, 0)
Out: \'14 days, 06:56:07\'

$ sec2time(1234567.8910, 4)
Out: \'14 days, 06:56:07.8910\'

$ sec2time([12, 345678.9], 3)
Out: [\'00:00:12.000\', \'4 days, 00:01:18.900\']


标签: python