Skip to content

Instantly share code, notes, and snippets.

@joeljacobs
Last active March 3, 2017 23:48
Show Gist options
  • Save joeljacobs/91ebc243fce7487b635fb01b4808e23e to your computer and use it in GitHub Desktop.
Save joeljacobs/91ebc243fce7487b635fb01b4808e23e to your computer and use it in GitHub Desktop.
Bash Timing Function. Takes <start_time> <title> [<timezone}]
#!/bin/bash
#Outputs
# <title> Start: 4:30:01pm End: 4:33:05pm Elapsed: 0:03:04
# Script behaves same as function. i.e. you can copy the function out.
time_elapsed(){
timezone=${3-"America/Denver"} #Defaults to Mountain Time
resume_time=$(date)
resume_time_seconds=$(date --date="$resume_time" +%s)
begin_time_seconds=$(date --date="$1" +%s)
elapsed_seconds=$(( $resume_time_seconds - $begin_time_seconds ))
elapsed_time=$(printf "%2g:%02d:%02d\n" $(( elapsed_seconds / 3600 )) $(( elapsed_seconds /60 )) $(( elapsed_seconds % 60)))
printf "$2 Start: %s End: %s Elapsed: %8.8s\n" "$(TZ="$timezone" date --date="$1" +%_I:%M:%S%P)" "$(TZ="$timezone" date --date="$resume_time" +%_I:%M:%S%P)" $elapsed_time
}
time_elapsed "$1" "$2" "$3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment