summary refs log tree commit diff
path: root/.config/timewarrior/extensions/total
blob: 8742b6828e29559499808bace43d76d38a1b3282 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/env python3

from common import loadstdin, dumpstdout, parse_timedelta
from datetime import datetime, timezone, timedelta
from os import getenv
from math import ceil

RATE = int(getenv('REPORT_RATE', '0'))
BILL = parse_timedelta(getenv('REPORT_BILL', '1:00'))

def timedelta_ceil(dur: timedelta, diff: timedelta) -> timedelta:
	delta = ceil(dur.total_seconds() / diff.total_seconds()) * diff.total_seconds() - dur.total_seconds()
	return timedelta(seconds=(dur.total_seconds() + delta))


def main() -> None:
	# parse stdin json
	meta, data = loadstdin()

	# calc total
	total = timedelta(0)
	for e in data:
		startt = datetime.fromisoformat(e['start'])
		endt = datetime.fromisoformat(e['end'])
		total += endt - startt

	# calc amount
	total_billable = timedelta_ceil(total, BILL)
	total_amount = (total_billable.total_seconds() / 3600) * RATE

	# presentation
	print(f"total => {total}")
	print(f"bill  => {total_billable}")
	print(f"{total_billable} @ {RATE} EUR => {total_amount} EUR")


if __name__ == "__main__":
	main()