chore(user/bkmml):Cleanup comment
[BK-2020-03.git] / user / bkdatediff
1 #!/usr/bin/env bash
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"
5 # Version 0.0.1
6
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
10 check_depends() {
11 if ! command -v date 1>/dev/random 2>&1; then die "FATAL:Missing:date"; fi;
12 return 0;
13 }; # check dependencies
14 show_usage() {
15 cat <<'EOF'
16 USAGE:
17 bkdatediff [STRING1] [STRING2]
18
19 STRING1, STRING2
20 `date` parsable strings. (i.e. variable STR in `date --date='$STR'`)
21
22 EXAMPLE:
23 bkdatediff "2023-01-10T08:36:42+00" "2023-01-10T12:30:13+00"
24 EOF
25 };
26 main() {
27 local t1 t2;
28 check_depends;
29 # check args
30 if [[ ! $# -eq 2 ]]; then show_usage; die "FATAL:Wrong number of args:$#"; fi;
31
32 # get unix epoch
33 t1="$(must date --date="$1" +%s)";
34 t2="$(must date --date="$2" +%s)";
35
36 # print diff in seconds
37 printf "%s\n" "$((t2 - t1))";
38 }; # main program
39
40 main "$@";