]>
zdv2.bktei.com Git - BK-2020-03.git/blob - user/bkdatediff
8a669ab801b0458c4e4ff11324f73b146b543d06
   2 # Desc: Get time difference two between date-parsable strings in seconds 
   3 # Usage: bkdatediff [STRING] [STRING] 
   4 # Example: bkdatediff "2023-01-10T08:36:42+00" "2023-01-10T12:30:13+00" 
   7 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr 
   8 die
() { yell 
"$*"; exit 111; } # same as yell() but non-zero exit status 
   9 must
() { "$@" || die 
"cannot $*"; } # runs args as command, reports args if command fails 
  11     if ! command -v date 1>/dev
/random 
2>&1; then die 
"FATAL:Missing:date"; fi; 
  13 }; # check dependencies 
  17     bkdatediff [STRING1] [STRING2] 
  20     `date` parsable strings. (i.e. variable STR in `date --date='$STR'`) 
  23     bkdatediff "2023-01-10T08:36:42+00" "2023-01-10T12:30:13+00" 
  30     if [[ ! $# -eq 2 ]]; then show_usage
; die 
"FATAL:Wrong number of args:$#"; fi; 
  33     t1
="$(must date --date="$1" +%s)"; 
  34     t2
="$(must date --date="$2" +%s)"; 
  36     # print diff in seconds 
  37     printf "%s\n" "$((t2 - t1))";