在 Python 中添加持续时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2410454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:35:54  来源:igfitidea点击:

Adding up time durations in Python

pythondatetimetime

提问by Sam Brightman

I would like to add up a series of splits in Python. The times begin as strings like "00:08:30.291". I can't seem to find the right way to use the Python objects or API to make this convenient/elegant. It seems that the time object doesn't use microseconds, so I'm using datetime's strptime to parse the strings, successfully. But then datetimes don't seem to add, and I really prefer not to overflow into days (i.e. 23 + 2 hours = 25 hours). I can use datetime.time but they don't add either. Timedeltas would seem appropriate but seem a little awkward to convert from/to other things. Perhaps I am missing something obvious here. I would like to be able to:

我想在 Python 中添加一系列拆分。时间以诸如“00:08:30.291”之类的字符串开头。我似乎找不到正确的方法来使用 Python 对象或 API 来使其方便/优雅。似乎时间对象不使用微秒,所以我使用日期时间的 strptime 成功解析字符串。但随后日期时间似乎并没有增加,我真的不希望溢出到几天(即 23 + 2 小时 = 25 小时)。我可以使用 datetime.time 但他们也不添加。Timedeltas 看起来很合适,但从/转换到其他东西似乎有点尴尬。也许我在这里遗漏了一些明显的东西。我希望能够:

for timestring in times:
    t = datetime.strptime("%H:%M:%S.%f", timestring).time
    total_duration = total_duration + t
print total_duration.strftime("%H:%M:%S.%f")

回答by SilentGhost

What you're working with is time differences, that's why using datetime.timedeltais only appropriate here:

您正在处理的是时差,这就是为什么使用datetime.timedelta仅适用于此处:

>>> import datetime
>>> d1 = datetime.datetime.strptime("00:08:30.291", "%H:%M:%S.%f")
>>> d1
datetime.datetime(1900, 1, 1, 0, 8, 30, 291000)
>>> d2
datetime.datetime(1900, 1, 1, 0, 2, 30, 291000)
>>> dt1 = datetime.timedelta(minutes=d1.minute, seconds=d1.second, microseconds=d1.microsecond)
>>> dt2 = datetime.timedelta(minutes=d2.minute, seconds=d2.second, microseconds=d2.microsecond)
>>> fin = dt1 + dt2
>>> fin
datetime.timedelta(0, 660, 582000)
>>> str(fin)
'0:11:00.582000'

Also, please don't use such names as sumfor your variables, you're shadowing built-in.

另外,请不要使用这样的名称作为sum您的变量,您正在隐藏内置。

回答by banderlog013

import numpy as np

# read file with one duration per line
with open('clean_times.txt', 'r') as f:
    x = f.read()

# Convert string to list of '00:02:12.31'
# I had to drop last item (empty string)
tmp = x.split('\n')[:-1]

# get list of ['00', 02, '12.31']
tmp = [i.split(':') for i in tmp.copy()]

# create numpy array with floats
np_tmp = np.array(tmp, dtype=np.float)

# sum via columns and divide
# hours/24 minutes/60 milliseconds/1000
# X will be a float array [days, hours, seconds]
# Something like `array([ 0.        , 15.68333333,  7.4189    ])`
X = np_tmp.sum(axis=0) / np.array([24, 60, 1000])

I was hapy here, but if you need fancy string like '15:41:07.518'as output, continue reading

我在这里很高兴,但如果你需要花哨的字符串'15:41:07.518'作为输出,请继续阅读

# X will be a float array [hours, hours, seconds]
X = np_tmp.sum(axis=0) / np.array([1, 60, 1000])

# ugly part
# Hours are integer parts
H = int(X[0]) + int(X[1])
# Minutes are  hour fractional part and integer minutes part
tmp_M = (X[0] % 1 + X[1] % 1) * 60
M = int(tmp_M)
# Seconds are minutes fractional part and integer seconds part
tmp_S = tmp_M % 1 * 60 + X[2]
S = int(tmp_S)
# Milliseconds are seconds fractional part
MS = int(tmp_S % 1 * 1000)

# merge string for output
# Something like '15:41:07.518'
result = f'{H:02}:{M:02}:{S:02}.{MS:03}'