From ad9e7cd5117c965222aae708f660e56d537914fc Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Mon, 6 Oct 2025 00:48:48 +0300 Subject: imported all of my Github gists from https://gist.github.com/rhaberkorn --- gtimelog-stats.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 gtimelog-stats.lua (limited to 'gtimelog-stats.lua') 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)) -- cgit v1.2.3