如何将计算的时间增量显示为 python 中的时间

how to display calculated timedelta as time in python

我正在通过 timew-report python 库计算存储在 timewarrior 中的时间。

我正在计算时间,这是我能做到的。我正在尝试让总数显示在 hours:minutes:seconds 的数字内,没有天数。

我的脚本....

#!/usr/bin/python
import sys
import datetime
from datetime import timedelta
from timewreport.parser import TimeWarriorParser #https://github.com/lauft/timew-report

parser = TimeWarriorParser(sys.stdin)

total = datetime.datetime(1, 1, 1, 0, 0)
for interval in parser.get_intervals():
    duration = interval.get_duration()
    print(duration)
    total = total + duration
print(total)

...正常工作,返回:

0:01:09
0:06:03
7:00:00
0:12:52
20:00:00
0001-01-02 03:20:04

...但我不想显示 0001-01-02 03:20:04,而是显示 27:20:04

如何将其格式化为这样的格式?

datetime.datetime(1, 1, 1, 0, 0) 这样初始化 total 是我采取了错误的方法吗?

datetime 传递 total 秒到 timedelta 函数,如下所示:

total = your_total.timestamp()
total_time = datetime.timedelta(seconds=total) 
str(total_time) 

假设 interval.get_duration 每次都返回一个 datetime.timedelta 对象,您可以将它们添加到现有的 datetime.timedelta 对象,然后进行算术转换为 HH:MM:SS 格式在最后。 (您需要自己计算,因为 timedelta 的默认字符串表示形式将使用天数,如果值超过 24 小时则使用 HH:MM:SS,这是您不想要的。)

例如:

import datetime

total = datetime.timedelta(0)
for interval in parser.get_intervals():
    duration = interval.get_duration()
    total += duration

total_secs = int(total.total_seconds())
secs = total_secs % 60
mins = (total_secs // 60) % 60
hours = (total_secs // 3600)

print("{hours}:{mins:02}:{secs:02}".format(hours=hours, mins=mins, secs=secs))

对于任何对此时间战士报告感兴趣的人,这是我的最终工作代码。将其放入位于 timewarrior 扩展目录的 scriptname 中,然后像 timew scriptname tagname 一样调用以查看显示给定标签的注释和总未开票时间的时间报告(它也可以在没有标签的情况下使用以显示所有时间条目) .

#!/usr/bin/python
import sys
import datetime
from datetime import timedelta
from timewreport.parser import TimeWarriorParser #https://github.com/lauft/timew-report

parser = TimeWarriorParser(sys.stdin)

total = datetime.timedelta()
tags = ''
for interval in parser.get_intervals():
    tags = interval.get_tags()
    # this report shows only un-invoiced time, so we ignore "invoiced" time entries
    if 'invoiced' not in tags:
        # hide 'client' and 'work' tags since they clutter this report
        if 'clients' in tags:
            tags.remove('clients')
        if 'work' in tags:
            tags.remove('work')
        date = interval.get_start()
        duration = interval.get_duration()
        ant = interval.get_annotation()
        sep = ', '
        taglist = sep.join(tags)
        output = str(date.date()) + ' - ' + str(duration) + ' - ' + taglist
        if ant:
            output += ' ||| ' + str(ant)
        print(output)
        total = total + duration

print('----------------')

# We calculate the time out like this manually because we don't want numbers of hours greater than 24 to be presented as days
total_secs = int(total.total_seconds())
secs = total_secs % 60
mins = (total_secs // 60) % 60
hours = (total_secs // 3600)

# for new versions of python 3.6 and up the following could work
# print(f"{hours}:{mins:02}:{secs:02}")
# but for older python this works...
print("total = {hours}:{mins:02}:{secs:02}".format(hours=hours, mins=mins, secs=secs))