aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/time2tsv
blob: 4cc369d55ced10f9eeef153c789ced1a045b3cf6 (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/awk -f

# ignore empty or comment lines
/^\s*#/ { next }
/^\s*$/ { next }

{
	owner = ""
	date = ""
	duration = 0
	description = ""
}

function max(a, b) {
	return a > b ? a : b
}

function duration2time(duration) {
	temp = 0
	num = 0
	multiplier = 60

	split(duration, char, "")
	for (i=1; i <= length(duration); i++) {
		if (char[i] == "h") {
			temp += num * 60 * 60
			multiplier = 60
			num = 0
			continue
		}
		if (char[i] == "m") {
			temp += num * 60
			multiplier = 1
			num = 0
			continue
		}
		if (char[i] == "s") {
			temp += num
			multiplier = 0
			num = 0
			continue
		}

		num *= 10
		num += char[i]
	}
	if (num > 0) {
		temp += num * multiplier
	}

	return temp
}

function stripnotes(desc) {
	out = ""
	nestlevel = 0

	split(desc, char, "")
	for (i=1; i <= length(desc); i++) {
		if (char[i] == "(") {
			nestlevel += 1
			continue
		}

		if (char[i] == ")") {
			nestlevel = max(0, nestlevel - 1)
			continue
		}

		if (nestlevel > 0) continue
		out = out char[i]
	}

	return out
}

# strip leading whitespace
/^\s+/ { gsub(/^\s+/, "", $0) }

# match owner
match($0, /^[[:alpha:]]+/) {
	owner = substr($0, RSTART, RLENGTH)
	gsub(/^[[:alpha:]]+:?\s*/, "", $0)
}

# match date
match($0, /^[0-9-]{10}/) {
	date = substr($0, RSTART, RLENGTH)
	gsub(/^[0-9-]{10}:?\s*/, "", $0)
}

# match time
match($0, /^\<([0-9]+h)?([0-9]{1,2}m)?([0-9]{1,2}s)?\>/) {
	duration = duration2time(substr($0, RSTART, RLENGTH))
	gsub(/^\<([0-9]+h)?([0-9]{1,2}m)?([0-9]{1,2}s)?\>:?\s*/, "", $0)
}

# description
{
	description = stripnotes($0)
}

# output
{
	printf("%s\t%s\t%s\t%s\n", owner, date, duration, description)
}