#!/bin/bash # # punchclock - a KISS utility to track time with minimal effort # # Copyright 2023 Jan Koppe # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. set -euo pipefail LOGFILE="${XDG_DATA_HOME:-$HOME/.local/share}/punchclock/log" VERBOSE=0 FORMAT_SECONDS=0 # Function to print usage instructions function usage { echo "" echo "Usage: $0 [-s|--seconds] [-l|--logfile path] [-v|--verbose] command" echo "" echo "A KISS utility to track time with minimal effort." echo "" echo "Options:" echo " -s, --seconds Output times in seconds." echo " -l, --logfile Use the logfile at this location." echo " -v, --verbose Give verbose output about what's happening." echo "" echo "Commands:" echo " status Show the last saved tracking status, start or stop." echo " statusbar Shows a symbol for tracking status and todays sum. For use in statusbars." echo " start Start tracking." echo " stop Stop tracking." echo " sum Sum of time tracked today." echo " toggle Toggle tracking." exit 1 } function init { [[ "${VERBOSE}x" == "1x" ]] && echo "verbose: Ensuring logfile directory exists." local LOGFILE_DIR="${LOGFILE%$(basename $LOGFILE)}" [[ -d "$LOGFILE_DIR" ]] || mkdir -p "${LOGFILE%$(basename $LOGFILE)}" [[ "${VERBOSE}x" == "1x" ]] && echo "verbose: Ensuring logfile exists." [[ -f "$LOGFILE" ]] || touch "$LOGFILE" } function _get_status { local STATE=$(tail -n 1 $LOGFILE | cut -d';' -f2) case "$STATE" in "start") echo $STATE ;; "stop") echo $STATE ;; *) _file_corrupt "unknown state: $STATE" ;; esac } function _set_status { echo "$(date -u +%Y-%m-%dT%H:%M:%S%Z);$1" >> $LOGFILE } function _file_corrupt { echo "Attention: It seems that the contents of the logfile are corrupt." echo " Please check what's in there and repair it." echo " reason: $1" exit 1 } function _get_active_seconds_for_day { local INPUT="${@:-$(date -u +%Y-%m-%d)}" local DATE=$(date -u -d "$INPUT" +%Y-%m-%d) local LAST_STATE="stop" local EPOCH_LAST=0 local EPOCH_TOTAL=0 while read LINE do if [[ "$LINE" =~ ^"$DATE" ]] then # Make sure we're alternating between different states and first line is a start local STATE=$(echo "$LINE" | cut -d';' -f2) [[ "$LAST_STATE" == "$STATE" ]] && _file_corrupt "repeated state: $LINE" LAST_STATE=$STATE # extract the unix epoch timestamp for this data point local TIMESTAMP=$(echo "$LINE" | cut -d';' -f1) local EPOCH_TIMESTAMP=$(date --date="$TIMESTAMP" -u +%s) if [[ "$STATE" == "stop" ]] then # if we're ending an interval, calculate the interval length and sum it up EPOCH_INTERVAL=$(( $EPOCH_TIMESTAMP - $EPOCH_LAST )) EPOCH_TOTAL=$(( $EPOCH_TOTAL + $EPOCH_INTERVAL )) else # if we're not ending an interval, remember the start epoch timestamp EPOCH_LAST=$EPOCH_TIMESTAMP fi fi done < $LOGFILE if [[ "$LAST_STATE" == "start" ]] then # we're in an active interval still, so let's just assume that we finish right now to include the interval up until now. local EPOCH_TIMESTAMP=$(date -u +%s) EPOCH_INTERVAL=$(( $EPOCH_TIMESTAMP - $EPOCH_LAST )) EPOCH_TOTAL=$(( $EPOCH_TOTAL + $EPOCH_INTERVAL )) fi echo $EPOCH_TOTAL } function _seconds_to_hours_and_minutes { # intentionally does not combine hours to days - in a summary this is usually what you want. local HOURS=$(printf "%02d" $(( $1/(60*60) ))) local MINUTES=$(printf "%02d" $(echo "scale=0;($1 % (60*60))/60" | bc)) echo "$HOURS:$MINUTES" } function _sum_for_day { local SECONDS=$(_get_active_seconds_for_day $@) if [[ "${FORMAT_SECONDS}x" == "1x" ]] then echo "$SECONDS" else echo "$(_seconds_to_hours_and_minutes $SECONDS)" fi } function _print_sum_for_multiple_days { local START=$(date -u +%Y-%m-%d -d "$1") local DAYS=$2 local TOTAL_SECONDS=0 echo -e "\033[1mDATE\t\tHOURS\tTOTAL\033[0m" for DAY in $(seq 0 $DAYS) do local DATE=$(date -u -d "$START + $DAY days" +%Y-%m-%d) local SECONDS=$(_get_active_seconds_for_day $DATE) local TOTAL_SECONDS=$(( $TOTAL_SECONDS + $SECONDS )) echo -e "$DATE\t$(_seconds_to_hours_and_minutes $SECONDS)\t$(_seconds_to_hours_and_minutes $TOTAL_SECONDS)" done echo -e "\t\t\t\033[1m$(_seconds_to_hours_and_minutes $TOTAL_SECONDS)\033[0m" } function _sum_for_multiple_days { # like _print_sum_for_multiple_days, but only returns total seconds local START=$(date -u +%Y-%m-%d -d "$1") local DAYS=$2 local TOTAL_SECONDS=0 for DAY in $(seq 0 $DAYS) do local DATE=$(date -u -d "$START + $DAY days" +%Y-%m-%d) local SECONDS=$(_get_active_seconds_for_day $DATE) local TOTAL_SECONDS=$(( $TOTAL_SECONDS + $SECONDS )) done echo $TOTAL_SECONDS } function _print_sum_for_weeks { local YEAR=${1:-$(date -u +%Y)} local TOTAL_SECONDS=0 echo -e "\033[1mDATE\t\tHOURS\tTOTAL\033[0m" for WEEK in {01..52} do local MONDAY_WEEKDAY="$YEAR-W$WEEK-01" # date does not accept ISO8601 calendar weeks as input, # we need to convert to regular ISO8601 %Y-%m-%d first local MONDAY_DATE=$(dateconv -f %Y-%m-%d $MONDAY_WEEKDAY) local SECONDS=$(_sum_for_multiple_days $MONDAY_DATE 6) local TOTAL_SECONDS=$(( $TOTAL_SECONDS + $SECONDS )) echo -e "$YEAR-W$WEEK\t$(_seconds_to_hours_and_minutes $SECONDS)\t$(_seconds_to_hours_and_minutes $TOTAL_SECONDS)" done echo -e "\t\t\t\033[1m$(_seconds_to_hours_and_minutes $TOTAL_SECONDS)\033[0m" } function _get_number_of_days_in_year { local YEAR=${1:-$(date -u +"%Y")} if [[ $(expr $YEAR % 400) -eq 0 ]] || [[ $(expr $YEAR % 100) -eq 0 ]] || [[ $(expr $YEAR % 4) -eq 0 ]] then echo 366 else echo 365 fi } function command_sum { if [[ $# -gt 0 ]] then case $1 in "year-weeks") shift _print_sum_for_weeks $@ ;; "year-days") shift local START=${1:-$(date -u +%Y)}-01-01 local DAYS=$(( $(_get_number_of_days_in_year $(date -u -d "$START" +%Y)) -1 )) _print_sum_for_multiple_days $START $DAYS ;; "month") shift local START=${1:-$(date -u +%Y-%m)}-01 local DAYS=$(( $(date -d "$START + 1 month - 1 days" +%d) - 1 )) _print_sum_for_multiple_days $START $DAYS ;; "week") shift local MONDAY_WEEKDAY=${1:-$(date -u +%Y-W%V)-01} # date does not accept ISO8601 calendar weeks as input, # we need to convert to regular ISO8601 %Y-%m-%d first local MONDAY_DATE=$(dateconv -f %Y-%m-%d $MONDAY_WEEKDAY) echo ; echo -e "Summary for week \033[1m${MONDAY_WEEKDAY}\033[0m" ; echo _print_sum_for_multiple_days $MONDAY_DATE 6 ;; "days") shift _print_sum_for_multiple_days $@ ;; *) _sum_for_day "$@" ;; esac else _sum_for_day fi } function command_statusbar { local ACTIVE="$(_seconds_to_hours_and_minutes $(_get_active_seconds_for_day))" case "$(_get_status)" in "stop") echo "▱ $ACTIVE" ;; "start") echo "▰ $ACTIVE" ;; esac } function command_status { echo "status: $(_get_status)" } function command_start { [[ $(_get_status) == "start" ]] || _set_status "start" } function command_stop { [[ $(_get_status) == "stop" ]] || _set_status "stop" } function command_toggle { STATE=$(_get_status) case "$STATE" in "stop") _set_status "start" ;; "start") _set_status "stop" ;; esac } # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -v|--verbose) VERBOSE=1 shift ;; -l|--logfile) LOGFILE=$2 shift shift ;; -s|--seconds) FORMAT_SECONDS=1 shift ;; -h|--help) usage shift ;; status) init command_status COMMAND=status shift break ;; statusbar) init command_statusbar COMMAND=statusbar shift break ;; start) init command_start COMMAND=start shift break ;; stop) init command_stop COMMAND=stop shift break ;; toggle) init command_toggle COMMAND=toggle shift break ;; sum) init shift command_sum $@ COMMAND=sum break ;; *) echo "Invalid option or command: $1" usage ;; esac done