You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.7 KiB
Bash
137 lines
3.7 KiB
Bash
2 years ago
|
#!/bin/bash
|
||
|
|
||
|
# Time sheet creator
|
||
|
# Stores clock in and outs to timesheet named for the ending date of the ts
|
||
|
usage() {
|
||
|
cat <<EOF
|
||
|
Usage: timetracker [-i|o|t|v|h]
|
||
|
|
||
|
Tracks timesheets automatically
|
||
|
|
||
|
Options:
|
||
|
-i, --in clock in
|
||
|
-o, --out clock out
|
||
|
-t, --total totals the hours worked
|
||
|
-v, --visualize prints the timesheet
|
||
|
-h, --help display this message
|
||
|
EOF
|
||
|
}
|
||
|
|
||
|
# dir for timesheets
|
||
2 years ago
|
WORKING_DIR="$HOME/.local/share/timetracker"
|
||
|
TIMESHEET_DIR="$WORKING_DIR/timesheets"
|
||
|
ARCHIVE_DIR="$TIMESHEET_DIR/archive"
|
||
2 years ago
|
# existing
|
||
2 years ago
|
TIMESHEET=$(ls "$TIMESHEET_DIR" | grep ".csv")
|
||
2 years ago
|
# generated by clock in
|
||
2 years ago
|
CURRENT_SESSION="$TIMESHEET_DIR/.session"
|
||
2 years ago
|
|
||
|
createTimesheet() {
|
||
|
payperiods=$((($(date +%s) - $(date -d "Dec-30-22" +%s)) / (14*24*3600)))
|
||
|
period_end=$(date -d "Dec-30-22 + $((($payperiods + 1) * 14)) days" +%b-%d-%y)
|
||
|
TIMESHEET=$(printf "%s.csv" "$period_end")
|
||
|
# create timesheet
|
||
|
echo "Date, Time In, Time Out, Total Time (HH:MM)" > "${TIMESHEET_DIR}/${TIMESHEET}"
|
||
|
}
|
||
|
|
||
|
getTimesheet() {
|
||
|
# get Timesheet to use
|
||
|
if [[ -z "$TIMESHEET" ]] ; then
|
||
|
# no timesheet, create one
|
||
|
createTimesheet
|
||
|
else
|
||
|
# timesheet found, check we are still in period
|
||
|
period_end=$(date -d "$(echo "$TIMESHEET" | awk -F . '{print $1}') + 1 days" +%s)
|
||
|
if [[ $(date +%s) -ge $period_end ]] ; then
|
||
|
# past timesheet, moving to archive and creating new
|
||
2 years ago
|
mv "${TIMESHEET_DIR}/${TIMESHEET}" "$ARCHIVE_DIR"
|
||
2 years ago
|
createTimesheet
|
||
|
fi
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
clockIn() {
|
||
|
# clock in by creating env file
|
||
|
curdate=$(date +%s)
|
||
|
env_details=$(printf 'DATE=%s\nTIME_IN=%s\n' "$curdate" "$curdate")
|
||
|
if [[ ! -f "$CURRENT_SESSION" ]] ; then
|
||
|
# creating session
|
||
|
echo "$env_details" > "$CURRENT_SESSION"
|
||
|
else
|
||
|
echo "Already Clocked In!" >&2
|
||
|
exit 1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
clockOut() {
|
||
|
# clock out
|
||
|
if [[ -f "$CURRENT_SESSION" ]] ; then
|
||
|
# session exists, get info
|
||
|
source "$CURRENT_SESSION"
|
||
|
# printing to timesheet
|
||
|
TIME_OUT=$(date +%s)
|
||
|
TOTAL_TIME=$(($TIME_OUT - $TIME_IN + 3600 * 5))
|
||
|
echo "$TOTAL_TIME"
|
||
|
# printing to timesheet
|
||
|
printf '%s, %s, %s, %s\n' "$(date -d @$DATE "+%a %b-%d")" "$(date -d @$TIME_IN +%I:%M%P)" "$(date -d @$TIME_OUT +%I:%M%P)" "$(date -d @$TOTAL_TIME +%H:%M)" >> ${TIMESHEET_DIR}/${TIMESHEET}
|
||
|
# cleaning up session
|
||
|
rm ${CURRENT_SESSION}
|
||
|
else
|
||
|
# no session
|
||
|
echo "Not clocked in!" >&2
|
||
|
exit 1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
visualize() {
|
||
|
# visualize the current timesheet
|
||
|
column -s "," -t < ${TIMESHEET_DIR}/${TIMESHEET}
|
||
|
}
|
||
|
|
||
|
totalHours() {
|
||
|
# tally up hours for the current timesheet
|
||
|
while read -r entry; do
|
||
|
date=$(echo "$entry" | awk -F , '{print $1}' | tr -d ",\n")
|
||
|
time=$(echo "$entry" | awk -F , '{print $4}' | tr -d " ,\n")
|
||
|
if [[ "$date" != "Date" ]] ; then
|
||
|
printf 'On %s worked %s\n' "$date" "$time"
|
||
|
hours=$(($hours + 10#$(echo "$time" | awk -F : '{print $1}')))
|
||
|
mins=$(($mins + 10#$(echo "$time" | awk -F : '{print $2}')))
|
||
|
fi
|
||
|
done < ${TIMESHEET_DIR}/${TIMESHEET}
|
||
|
hours=$(($hours + $mins/60)) # overflow
|
||
|
mins=$(($mins%60))
|
||
|
printf 'Worked %d hours, %d minutes\n' "$hours" "$mins"
|
||
|
}
|
||
|
|
||
|
# get the timesheet
|
||
|
getTimesheet
|
||
|
|
||
|
# parse args
|
||
|
while [[ $# -gt 0 ]] ; do
|
||
|
case $1 in
|
||
|
-h | --help)
|
||
|
usage
|
||
|
exit 0
|
||
|
;;
|
||
|
-i | --in)
|
||
|
clockIn
|
||
|
;;
|
||
|
-o | --out)
|
||
|
clockOut
|
||
|
;;
|
||
|
-v | --visualize)
|
||
|
visualize
|
||
|
;;
|
||
|
-t | --total)
|
||
|
totalHours
|
||
|
;;
|
||
|
*)
|
||
|
echo "Error: Unrecognized" >&2
|
||
|
usage
|
||
|
exit 1
|
||
|
;;
|
||
|
esac
|
||
|
shift
|
||
|
done
|