Last active
July 8, 2022 15:43
-
-
Save rhaberkorn/5728733 to your computer and use it in GitHub Desktop.
Small helper script parsing gtimelog's timelog.txt to summarize the time spent on specific tasks. It takes a SNOBOL4/Lua pattern and optional start date and prints the sum of all timelog entries after the start date matching the pattern.Requires CSNOBOL4 or Lua 5.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/lua5.2 | |
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/snobol4 -b | |
# ./gtimelog-stats.sno <pattern> <begin> <end> | |
-INCLUDE 'time.sno' | |
input(.log, 100,, host(4, "HOME") "/.local/share/gtimelog/timelog.txt") | |
data('entry(entry_date_str,entry_date,entry_msg)') | |
param.pattern = eval(host(2, host(3))) :f(end) | |
param.begin = (mktime(strptime(host(2, host(3) + 1), "%d.%m.%Y")), 0) | |
param.end = (mktime(strptime(host(2, host(3) + 2), "%d.%m.%Y")), | |
+ tv_sec(gettimeofday())) | |
* Length of day in seconds | |
day_length = 24 * 60 * 60 | |
entries = table() | |
entries[0] = 0 | |
next line = log :f(next.end) | |
line pos(0) (break(" ") . date " " arb) . date_time ": " rem . msg :f(next) | |
entries[entries[0] = entries[0] + 1] = entry(date, strptime(date_time, "%Y-%m-%d %R"), msg) :(next) | |
next.end | |
i = 2 | |
* Skip slack lines and arrived lines | |
l entry_msg(entries[i]) "**" rpos(0) :s(l.c) | |
* Skip entries not containing <keywords> and before <begin> or after <end> | |
lt(mktime(entry_date(entries[i])), param.begin) :s(l.c) | |
ident(entry_date_str(entries[i]), entry_date_str(entries[i - 1])) :s(l.f) | |
eq(stats.sum_day) :s(l.f) | |
* Print stats of last day | |
* FIXME: does not always print last day | |
output = entry_date_str(entries[i - 1]) ": " | |
+ (stats.sum_day / (60 * 60)) " hours, " | |
+ (remdr(stats.sum_day, 60 * 60) / 60) " minutes, " | |
+ remdr(stats.sum_day, 60) " seconds" | |
stats.sum_day = 0 | |
* Skip "arrived" lines | |
l.f entry_msg(entries[i]) pos(0) "arrived" :s(l.c) | |
ge(mktime(entry_date(entries[i])), param.end + day_length) :s(print_total) | |
replace(entry_msg(entries[i]), &ucase, &lcase) pos(0) param.pattern :f(l.c) | |
* output = tm_yday(entry_date(entries[i])) ": " mktime(entry_date(entries[i])) ": " entry_msg(entries[i]) | |
stats.sum_day = stats.sum_day + mktime(entry_date(entries[i])) - | |
+ mktime(entry_date(entries[i - 1])) | |
stats.sum = stats.sum + mktime(entry_date(entries[i])) - | |
+ mktime(entry_date(entries[i - 1])) | |
l.c le(i = i + 1, entries[0]) :s(l) | |
print_total | |
output = "Sum: " (stats.sum / (60 * 60)) " hours, " | |
+ (remdr(stats.sum, 60 * 60) / 60) " minutes, " | |
+ remdr(stats.sum, 60) " seconds" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment