]> zdv2.bktei.com Git - BK-2020-03.git/blob - unitproc/bkt-dateDuration
fix(user/bkdatev):Make tzdata-legacy 'right/UTC' TZ optional
[BK-2020-03.git] / unitproc / bkt-dateDuration
1 #!/bin/bash
2
3 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
4 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
5 must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
6 dateDuration() {
7 # Desc: Given two date strings, output seconds duration
8 # Example: dateDuration 2012-06-30 2012-07-01 # Yields 86401 because leap second
9 # Version 0.0.4
10 # Depends: GNU Coreutils 9.4 (date, sed), GNU Bash 5.2.21
11 # References: [1] “Understanding the "right" time zone database” https://kenta.blogspot.com/2016/03/sqfzcxay-understanding-right-time-zone.html
12
13 local date1 date2 re unixEpoch1 unixEpoch2 duration;
14
15 path_tz="/usr/share/zoneinfo/right/UTC";
16 if [[ ! -f "$path_tz" ]]; then yell "WARNING:Time zone 'right/UTC' file not found at ${path_tz}. Try 'sudo apt install tzdata-legacy'."; fi;
17 if [[ $# -ne 2 ]]; then die "FATAL:Need two date arguments. Got $#"; fi;
18 date1="$1";
19 date2="$2";
20 # Check args
21 if [[ -z "$date1" ]]; then die "FATAL:Invalid first date provided:${date1}"; fi;
22 if [[ -z "$date2" ]]; then die "FATAL:Invalid second date provided:${date2}"; fi;
23
24 ## Convert @-specified unix epoch into right unix epoch (rue)
25 re='^@.*';
26 if [[ "$date1" =~ $re ]] || [[ "$date2" =~ $re ]]; then
27 die "FATAL:@-specified unix epoch detected. Why are you using me?";
28 fi;
29
30 # Convert date strings into Unix epoch. # See [1]
31 unixEpoch1="$(TZ=right/UTC date --date="$date1" +%s)" || die "Problem with $(declare -p date1)";
32 unixEpoch2="$(TZ=right/UTC date --date="$date2" +%s)" || die "Problem with $(declare -p date2)";
33
34 # Calculate duration
35 duration="$((unixEpoch2 - unixEpoch1))";
36 duration=${duration#-};
37
38 printf "%s\n" "$duration";
39 };
40
41 # Examples
42 dateDuration 2026-06-30 2026-07-01; # 86400 seconds
43 dateDuration 2012-06-30 2012-07-01; # 86401 seconds (leap second)
44 dateDuration 2000-01-01 1999-12-31; # 86400 seconds
45 dateDuration 2012-06-30T23:59:59+00 2012-06-30T23:59:60; # 1 (leap second)