Remove a potential padding `0` as prefix as the number is then recognized as an octal number.
57 lines
1.4 KiB
Bash
Executable File
57 lines
1.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
EARLIEST_MOD_WEEK="$(date "+%V" --date="14 days ago")"
|
|
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 -2 <<<"$times" | head -1 | grep -Eo "[0-9]{4}" | head -1)"
|
|
|
|
# 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 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#0} >= 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
|