1

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

我正在增加时间,这是我能够做到的。而且我试图让总数显示在几个小时:分钟:秒内,而不是几天。

我的剧本……

#!/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

我怎样才能让它像这样格式化?

我通过初始化 total like 采取了错误的方法datetime.datetime(1, 1, 1, 0, 0)吗?

4

3 回答 3

2

对于任何对这个 timewarrior 报告感兴趣的人,这是我的最终工作代码。将它放在scriptname位于 timewarrior 扩展目录中,然后调用喜欢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))
于 2020-08-19T13:42:04.380 回答
2

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

例如:

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))
于 2020-08-12T20:22:29.657 回答
0

通过total几秒钟的时间来timedelta运行,datetime如下所示:

total = your_total.timestamp()
total_time = datetime.timedelta(seconds=total) 
str(total_time) 
于 2020-08-12T20:21:02.790 回答