diff options
author | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2025-10-06 00:48:48 +0300 |
---|---|---|
committer | Robin Haberkorn <rhaberkorn@fmsbw.de> | 2025-10-06 00:48:48 +0300 |
commit | ad9e7cd5117c965222aae708f660e56d537914fc (patch) | |
tree | 580006656ec76513500170e5646e92e7819c6c0b /gtimelog-stats.lua | |
download | snippets-ad9e7cd5117c965222aae708f660e56d537914fc.tar.gz |
imported all of my Github gists from https://gist.github.com/rhaberkorn
Diffstat (limited to 'gtimelog-stats.lua')
-rwxr-xr-x | gtimelog-stats.lua | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/gtimelog-stats.lua b/gtimelog-stats.lua new file mode 100755 index 0000000..abf3bff --- /dev/null +++ b/gtimelog-stats.lua @@ -0,0 +1,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)) |