我有一个以秒为单位返回信息的函数,但我需要以小时:分钟:秒为单位存储该信息。

在Python中是否有一种简单的方法将秒转换为这种格式?


当前回答

我是这样得到它的。

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)

一些例子:

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

其他回答

以下是我经常使用的一种方法:(不管它有多低效)

seconds = 19346
def zeroes (num):
    if num < 10: num = "0" + num
    return num

def return_hms(second, apply_zeroes):
    sec = second % 60
    min_ = second // 60 % 60
    hrs = second // 3600
    if apply_zeroes > 0:
       sec = zeroes(sec)
       min_ = zeroes(min_)
       if apply_zeroes > 1:
           hrs = zeroes(hrs)
    return "{}:{}:{}".format(hrs, min_, sec)

print(return_hms(seconds, 1))

结果: 5:22:26

return_hms()函数的语法

return_hms()函数的用法如下:

第一个变量(秒)是你想转换成h:m:s的秒数。

第二个变量(apply_0s)是格式化:

0或更少:不应用任何零

1:当分钟和秒低于10时,将0应用于分钟和秒。

2或更多:当值小于10时,对任何值(包括小时)应用0。

我是这样得到它的。

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)

一些例子:

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

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

打印

40.0
01:30:40

这是我的小把戏:

from humanfriendly import format_timespan
secondsPassed = 1302
format_timespan(secondsPassed)
# '21 minutes and 42 seconds'

欲了解更多信息,请访问: https://humanfriendly.readthedocs.io/en/latest/api.html#humanfriendly.format_timespan

下面是一个简单的程序,它读取当前时间并将其转换为以小时、分钟和秒为单位的一天时间

import time as tm #import package time
timenow = tm.ctime() #fetch local time in string format

timeinhrs = timenow[11:19]

t=tm.time()#time.time() gives out time in seconds since epoch.

print("Time in HH:MM:SS format is: ",timeinhrs,"\nTime since epoch is : ",t/(3600*24),"days")

输出为

Time in HH:MM:SS format is:  13:32:45 
Time since epoch is :  18793.335252338384 days