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
|
#!/usr/bin/lua5.2
-- Small helper script parsing gtimelog's timelog.txt to summarize the time spent on specific tasks.
-- It takes a Lua pattern and optional start date and prints the sum of all timelog entries after the start date matching the pattern.
local function parse_date(str)
local t = {hour = 0, min = 0}
t.day, t.month, t.year = str:match("^(%d+)%.(%d+)%.(%d+)$")
return os.time(t)
end
local pattern = arg[1] or ""
local time_begin = arg[2] and parse_date(arg[2]) or 0
local time_end = arg[3] and parse_date(arg[3]) or os.time()
local log = io.open(os.getenv("HOME").."/.local/share/gtimelog/timelog.txt")
local entries = {}
for line in log:lines() do
local t, date_str, msg = {}
date_str, t.year, t.month, t.day, t.hour, t.min, msg = line:match("^((%d+)%-(%d+)%-(%d+)) (%d+):(%d+): (.*)$")
if msg then table.insert(entries, {date_str = date_str, time = os.time(t), msg = msg}) end
end
log:close()
local sum, sum_day, arrived = 0, 0
for i, entry in ipairs(entries) do
if entry.msg:sub(-2) ~= "**" and os.difftime(entry.time, time_begin) >= 0 then
if i > 1 and entry.date_str ~= entries[i-1].date_str and sum_day > 0 then
-- print stats of last day
-- FIXME: Does not always print last day
print(string.format("%s: %d hours, %d minutes, %d seconds",
entries[i-1].date_str,
sum_day / (60*60), (sum_day % (60*60))/60, sum_day % 60))
sum_day = 0
end
if entry.msg:sub(1, 7) == "arrived" then
arrived = true
elseif arrived then
if os.difftime(entry.time, time_end + 24*60*60) >= 0 then break end
if entry.msg:lower():find("^"..pattern) then
sum_day = sum_day + os.difftime(entry.time, entries[i-1].time)
sum = sum + os.difftime(entry.time, entries[i-1].time)
end
end
end
end
print(string.format("Sum: %d hours, %d minutes, %d seconds",
sum / (60*60), (sum % (60*60))/60, sum % 60))
|