-
Notifications
You must be signed in to change notification settings - Fork 1
/
insert-time-delta.py
executable file
·36 lines (29 loc) · 1 KB
/
insert-time-delta.py
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
#!/usr/bin/env python3
# Author: github.com/danielhoherd
# License: MIT
from sys import stderr, stdin
import pendulum
import typer
def timestamp_lines(separator: str = typer.Option(" ", "--separator", "-s"), file: str = typer.Argument(None)):
"""Takes a stream from stdin where the first token is a datestamp and inserts the time delta
between the previous timestamp and the current one.
"""
if file:
data = open(file)
else:
if stdin.isatty():
print("ERROR: no data piped in and no file specified!", file=stderr)
raise SystemExit(1)
data = stdin
old = None
for line in data:
line = line.strip("\n")
datestamp = line.split()[0]
new = pendulum.parse(datestamp)
if not old:
old = new
delta = (new - old).in_words()
print(f"{new.to_iso8601_string()}{separator}({delta}){separator}{line.removeprefix(datestamp)}")
old = new
if __name__ == "__main__":
typer.run(timestamp_lines)