Sil*_*ost 666
你可以使用datetime.timedelta功能:
>>> import datetime
>>> str(datetime.timedelta(seconds=666))
'0:11:06'
Run Code Online (Sandbox Code Playgroud)
Bra*_*des 593
通过使用divmod()只有一个除法产生商和余数的函数,只需两个数学运算就可以非常快速地得到结果:
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
Run Code Online (Sandbox Code Playgroud)
然后使用字符串格式将结果转换为所需的输出:
print('{:d}:{:02d}:{:02d}'.format(h, m, s)) # Python 3
print(f'{h:d}:{m:02d}:{s:02d}') # Python 3.6+
Run Code Online (Sandbox Code Playgroud)
ana*_*nik 61
我很难说一个简单的方法(至少我不记得语法),但可以使用time.strftime,它可以更好地控制格式:
from time import strftime
from time import gmtime
strftime("%H:%M:%S", gmtime(666))
'00:11:06'
strftime("%H:%M:%S", gmtime(60*60*24))
'00:00:00'
Run Code Online (Sandbox Code Playgroud)
gmtime用于将秒转换为strftime()需要的特殊元组格式.
mar*_*ell 34
from datetime import timedelta
"{:0>8}".format(str(timedelta(seconds=66)))
# Result: '00:01:06'
"{:0>8}".format(str(timedelta(seconds=666777)))
# Result: '7 days, 17:12:57'
"{:0>8}".format(str(timedelta(seconds=60*60*49+109)))
# Result: '2 days, 1:01:49'
Run Code Online (Sandbox Code Playgroud)
和:
"{}".format(str(timedelta(seconds=66)))
# Result: '00:01:06'
"{}".format(str(timedelta(seconds=666777)))
# Result: '7 days, 17:12:57'
"{}".format(str(timedelta(seconds=60*60*49+109)))
# Result: '2 days, 1:01:49'
Run Code Online (Sandbox Code Playgroud)
没有':0> 8':
from time import gmtime
from time import strftime
# NOTE: The following resets if it goes over 23:59:59!
strftime("%H:%M:%S", gmtime(125))
# Result: '00:02:05'
strftime("%H:%M:%S", gmtime(60*60*24-1))
# Result: '23:59:59'
strftime("%H:%M:%S", gmtime(60*60*24))
# Result: '00:00:00'
strftime("%H:%M:%S", gmtime(666777))
# Result: '17:12:57'
# Wrong
Run Code Online (Sandbox Code Playgroud)
和:
from datetime import timedelta
"{:0>8}".format(str(timedelta(seconds=66)))
# Result: '00:01:06'
"{:0>8}".format(str(timedelta(seconds=666777)))
# Result: '7 days, 17:12:57'
"{:0>8}".format(str(timedelta(seconds=60*60*49+109)))
# Result: '2 days, 1:01:49'
Run Code Online (Sandbox Code Playgroud)
但:
"{}".format(str(timedelta(seconds=66)))
# Result: '00:01:06'
"{}".format(str(timedelta(seconds=666777)))
# Result: '7 days, 17:12:57'
"{}".format(str(timedelta(seconds=60*60*49+109)))
# Result: '2 days, 1:01:49'
Run Code Online (Sandbox Code Playgroud)
小智 17
这是我的快速诀窍:
from humanfriendly import format_timespan
secondsPassed = 1302
format_timespan(secondsPassed)
# '21 minutes and 42 seconds'
Run Code Online (Sandbox Code Playgroud)
访问:https: //humanfriendly.readthedocs.io/en/latest/#humanfriendly.format_timespan 获取更多信息.
Tom*_*dla 15
有点偏离主题的答案,但可能对某人有用
def time_format(seconds: int) -> str:
if seconds is not None:
seconds = int(seconds)
d = seconds // (3600 * 24)
h = seconds // 3600 % 24
m = seconds % 3600 // 60
s = seconds % 3600 % 60
if d > 0:
return '{:02d}D {:02d}H {:02d}m {:02d}s'.format(d, h, m, s)
elif h > 0:
return '{:02d}H {:02d}m {:02d}s'.format(h, m, s)
elif m > 0:
return '{:02d}m {:02d}s'.format(m, s)
elif s > 0:
return '{:02d}s'.format(s)
return '-'
Run Code Online (Sandbox Code Playgroud)
结果是:
print(time_format(25*60*60 + 125))
>>> 01D 01H 02m 05s
print(time_format(17*60*60 + 35))
>>> 17H 00m 35s
print(time_format(3500))
>>> 58m 20s
print(time_format(21))
>>> 21s
Run Code Online (Sandbox Code Playgroud)
小智 13
以下套装对我有用。
def sec_to_hours(seconds):
a=str(seconds//3600)
b=str((seconds%3600)//60)
c=str((seconds%3600)%60)
d=["{} hours {} mins {} seconds".format(a, b, c)]
return d
print(sec_to_hours(10000))
# ['2 hours 46 mins 40 seconds']
print(sec_to_hours(60*60*24+105))
# ['24 hours 1 mins 45 seconds']
Run Code Online (Sandbox Code Playgroud)
如果你需要获得datetime.time价值,你可以使用这个技巧:
my_time = (datetime(1970,1,1) + timedelta(seconds=my_seconds)).time()
Run Code Online (Sandbox Code Playgroud)
您不能添加timedelta到time,但可以将其添加到datetime.
UPD:同一技术的另一种变体:
my_time = (datetime.fromordinal(1) + timedelta(seconds=my_seconds)).time()
Run Code Online (Sandbox Code Playgroud)
而不是1你可以使用任何大于0的数字.这里我们使用的事实datetime.fromordinal总是返回组件为零的datetime对象time.
小时 (h) 由楼层除法(按 //)秒除以 3600(60 分钟/小时 * 60 秒/分钟)计算得出
分钟 (m) 由剩余秒数(小时计算的余数,按 %)除以 60(60 秒/分钟)计算得出
类似地,秒(s)按小时和分钟计算的剩余时间。
休息只是字符串格式!
def hms(seconds):
h = seconds // 3600
m = seconds % 3600 // 60
s = seconds % 3600 % 60
return '{:02d}:{:02d}:{:02d}'.format(h, m, s)
print(hms(7500)) # Should print 02h05m00s
Run Code Online (Sandbox Code Playgroud)
这就是我的方法。
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)
Run Code Online (Sandbox Code Playgroud)
一些例子:
$ 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']
Run Code Online (Sandbox Code Playgroud)
dateutil.relativedelta如果您还需要以浮点数形式访问小时、分钟和秒,则很方便。datetime.timedelta不提供类似的接口。
from dateutil.relativedelta import relativedelta
rt = relativedelta(seconds=5440)
print(rt.seconds)
print('{:02d}:{:02d}:{:02d}'.format(
int(rt.hours), int(rt.minutes), int(rt.seconds)))
Run Code Online (Sandbox Code Playgroud)
印刷
40.0
01:30:40
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
313295 次 |
| 最近记录: |