Skip to content

Instantly share code, notes, and snippets.

@xalvarez
Last active November 23, 2020 07:54
Show Gist options
  • Save xalvarez/517b61a696be2c6a3a0b0f22a51fc71c to your computer and use it in GitHub Desktop.
Save xalvarez/517b61a696be2c6a3a0b0f22a51fc71c to your computer and use it in GitHub Desktop.
Calculate time spent on an activity which starts and ends the same day
#!/bin/bash -e
# This script calculates time spent on an activity which starts and ends the same day.
APPLICATION_NAME=$0
printHelp() {
echo "usage: $APPLICATION_NAME <start_time> <end_time> [<break_in_minutes>]"
echo " start_time e.g. 8:00. Also valid: 08:00, 8"
echo " end_time e.g. 17:00. Also valid: 17"
echo " break_in_minutes e.g. Default: 50"
echo "Example call: $APPLICATION_NAME 8:30 17:30"
exit 1
}
if [[ -z "$2" ]]; then
printHelp
fi
START_TIME=$1
END_TIME=$2
BREAK_IN_MINUTES=${3:-50}
START_DATE=$(date -d "$START_TIME $BREAK_IN_MINUTES minutes" '+%s')
END_DATE=$(date -d "$END_TIME" '+%s')
SECONDS_BETWEEN_DATES=$(($END_DATE-$START_DATE))
RESULT_HOURS=$(($SECONDS_BETWEEN_DATES/3600))
RESULT_MINUTES=$(($SECONDS_BETWEEN_DATES%3600/60))
echo "Elapsed time:"
printf '%0dh %0dm' $RESULT_HOURS $RESULT_MINUTES
echo " (${RESULT_HOURS}$(echo "scale=2; $RESULT_MINUTES/60" | bc -l )h)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment