Files
dotfiles/.local/bin/timetracking

68 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
EARLIEST_MOD_WEEK="$(date "+%V" --date="14 days ago")"
EARLIEST_MOD_WEEK="${EARLIEST_MOD_WEEK#0}"
typeset -A WEEKS
if [[ -t 0 ]]; then
times="$(xclip -sel c -o)"
else
times="$(cat)"
fi
times="$(<<<"$times" grep $'\t' | tail -n +2)"
YEAR="$(tail -3 <<<"$times" | head -1 | grep -Eo "[0-9]{4}" | head -1)"
sum=0
# get relevant parts, format to ISO 8601'ish format and calculate difference
IFS=$'\n'
for line in $times; do
# gather times
times="$(<<<"$line" \
grep -Eo $'[0-9. \t:]{18}' \
| sed -E 's/.(..)\.(..)\.(.{4}) \t(.*)/\3-\2-\1 \4/'
)"
end="$(<<<"$times" tail -1)"
# check for valid end date & time in this year
[[ $end ]] && [[ $end != $'\t' ]] || continue
[[ ${end%%-*} = $YEAR ]] || continue
start="$(<<<"$times" head -1)"
# convert to UNIX timestamp
end="$(date --date="$end" "+%s")"
start="$(date --date="$start" "+%s")"
diff=$((end - start))
# subtract pauses
(( diff <= 32400 )) || : $((diff -= 900))
(( diff <= 21600 )) || : $((diff -= 1800))
: $((sum += diff))
# sum the hours in the same week
week=$(date "+%V" --date="@$start")
: $((WEEKS[$week] += $diff))
done
# print
for week in "${!WEEKS[@]}"; do
time="${WEEKS[$week]}"
hrs=$(( time / 60 / 60 ))
mins=$(( (time % (60 * 60)) / 60 ))
# last 2 weeks are modifiable
week="${week#0}"
(( week >= EARLIEST_MOD_WEEK )) && modifiable="*" || modifiable=" "
# 20h limit
(( time <= 72000 )) && warn= || warn=" !!"
printf "%s%s %02d:%02d%s\n" "$week" "$modifiable" "$hrs" "$mins" "$warn"
done | sort -n
: $(( sum -= ${#WEEKS[@]} * 15 * 60 * 60))
hrs=$(( sum / 60 / 60 ))
mins=$(( (sum % (60 * 60)) / 60 ))
printf "\ntotal:\n%02d:%02d\n" "$hrs" "$mins"