]> zdv2.bktei.com Git - BK-2020-03.git/blobdiff - unitproc/bkt-dateDuration
fix(user/bkdatev):Fix --date='' functionality for right/UTC
[BK-2020-03.git] / unitproc / bkt-dateDuration
index 0b09e66ef135738ee61dd967429cda13622224c2..10be78b2d4abbc033e6a40b78eb8f5858fc574f6 100755 (executable)
@@ -6,32 +6,34 @@ must() { "$@" || die "cannot $*"; } # runs args as command, reports args if comm
 dateDuration() {
     # Desc: Given two date strings, output seconds duration
     # Example: dateDuration 2012-06-30 2012-07-01  # Yields 86401 because leap second
 dateDuration() {
     # Desc: Given two date strings, output seconds duration
     # Example: dateDuration 2012-06-30 2012-07-01  # Yields 86401 because leap second
-    # Version 0.0.1
+    # Version 0.0.4
     # Depends: GNU Coreutils 9.4 (date, sed), GNU Bash 5.2.21
     # Depends: GNU Coreutils 9.4 (date, sed), GNU Bash 5.2.21
-    # References: [1] “"right" tz database (zoneinfo) files and GPS-based NTP” https://www.ucolick.org/~sla/leapsecs/right+gps.html
+    # References: [1] “Understanding the "right" time zone database” https://kenta.blogspot.com/2016/03/sqfzcxay-understanding-right-time-zone.html
 
 
+    local date1 date2 re unixEpoch1 unixEpoch2 duration;
+
+    path_tz="/usr/share/zoneinfo/right/UTC";
+    if [[ ! -f "$path_tz" ]]; then yell "WARNING:Time zone 'right/UTC' file not found at ${path_tz}. Try 'sudo apt install tzdata-legacy'."; fi;
+    if [[ $# -ne 2 ]]; then die "FATAL:Need two date arguments. Got $#"; fi;
     date1="$1";
     date2="$2";
     # Check args
     if [[ -z "$date1" ]]; then die "FATAL:Invalid first date provided:${date1}"; fi;
     if [[ -z "$date2" ]]; then die "FATAL:Invalid second date provided:${date2}"; fi;
     date1="$1";
     date2="$2";
     # Check args
     if [[ -z "$date1" ]]; then die "FATAL:Invalid first date provided:${date1}"; fi;
     if [[ -z "$date2" ]]; then die "FATAL:Invalid second date provided:${date2}"; fi;
-    if [[ $# -gt 2 ]]; then die "FATAL:Too many arguments."; fi;
+    
     ## Convert @-specified unix epoch into right unix epoch (rue)
     ## Convert @-specified unix epoch into right unix epoch (rue)
-    re='@.*';
+    re='^@.*';
     if [[ "$date1" =~ $re ]] || [[ "$date2" =~ $re ]]; then
         die "FATAL:@-specified unix epoch detected. Why are you using me?";
     fi;
 
     if [[ "$date1" =~ $re ]] || [[ "$date2" =~ $re ]]; then
         die "FATAL:@-specified unix epoch detected. Why are you using me?";
     fi;
 
-    # Use right unix epoch to account for leap seconds.
-    export TZ=right/UTC; # See [1]
-
-    # Convert date strings into Unix epoch
-    unixEpoch1="$(must date --date="$date1" +%s)";
-    unixEpoch2="$(must date --date="$date2" +%s)";
+    # Convert date strings into Unix epoch. # See [1]
+    unixEpoch1="$(TZ=right/UTC date --date="$date1" +%s)" || die "Problem with $(declare -p date1)";
+    unixEpoch2="$(TZ=right/UTC date --date="$date2" +%s)" || die "Problem with $(declare -p date2)";
 
     # Calculate duration
     duration="$((unixEpoch2 - unixEpoch1))";
 
     # Calculate duration
     duration="$((unixEpoch2 - unixEpoch1))";
-    duration="$(sed -e 's/^-//' <<<"$duration"; )";
+    duration=${duration#-};
 
     printf "%s\n" "$duration";
 };
 
     printf "%s\n" "$duration";
 };
@@ -39,3 +41,5 @@ dateDuration() {
 # Examples
 dateDuration 2026-06-30 2026-07-01; # 86400 seconds
 dateDuration 2012-06-30 2012-07-01; # 86401 seconds (leap second)
 # Examples
 dateDuration 2026-06-30 2026-07-01; # 86400 seconds
 dateDuration 2012-06-30 2012-07-01; # 86401 seconds (leap second)
+dateDuration 2000-01-01 1999-12-31; # 86400 seconds
+dateDuration 2012-06-30T23:59:59+00 2012-06-30T23:59:60; # 1 (leap second)