如何将秒转换为小时、分钟和秒?

时间:2021-01-24 02:47:49

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?

是否有一种简单的方法将秒转换成python中的这种格式?

7 个解决方案

#1


495  

or you can do

或者你可以做

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

#2


506  

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:

通过使用divmod()函数,该函数只做一个除法来生成商和余数,您可以非常快速地得到结果,只需进行两个数学运算:

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

#3


48  

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:

我几乎想不出一个简单的方法(至少我不记得语法),但是可以使用时间。strftime,它可以对格式进行更多的控制:

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

gmtime用于将秒转换为strftime()所需的特殊元组格式。

#4


28  

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

and:

和:

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

without ':0>8':

没有“:0 > 8”:

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

and:

和:

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

but:

但是:

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

#5


8  

This is my quick trick:

这是我的小窍门:

import humanfriendlyhumanfriendly.format_timespan(secondsPassed)

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

访问:https://humanfriendly.readthedocs.io/en/latest/ humanfriendly。format_timespanfor更多信息。

#6


6  

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

如果需要获取datetime。时间值,你可以使用这个技巧:

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

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

不能将timedelta添加到time,但可以将它添加到datetime。

UPD: Yet another variation of the same technique:

UPD:同样技术的另一种变化:

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.

而不是1,你可以用大于0的数。这里我们使用的事实是,datetime.fromordinal总是返回datetime对象,而time组件为零。

#7


2  

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']

#1


495  

or you can do

或者你可以做

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

#2


506  

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:

通过使用divmod()函数,该函数只做一个除法来生成商和余数,您可以非常快速地得到结果,只需进行两个数学运算:

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

#3


48  

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:

我几乎想不出一个简单的方法(至少我不记得语法),但是可以使用时间。strftime,它可以对格式进行更多的控制:

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

gmtime用于将秒转换为strftime()所需的特殊元组格式。

#4


28  

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

and:

和:

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

without ':0>8':

没有“:0 > 8”:

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

and:

和:

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

but:

但是:

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

#5


8  

This is my quick trick:

这是我的小窍门:

import humanfriendlyhumanfriendly.format_timespan(secondsPassed)

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

访问:https://humanfriendly.readthedocs.io/en/latest/ humanfriendly。format_timespanfor更多信息。

#6


6  

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

如果需要获取datetime。时间值,你可以使用这个技巧:

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

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

不能将timedelta添加到time,但可以将它添加到datetime。

UPD: Yet another variation of the same technique:

UPD:同样技术的另一种变化:

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.

而不是1,你可以用大于0的数。这里我们使用的事实是,datetime.fromordinal总是返回datetime对象,而time组件为零。

#7


2  

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']