]> zdv2.bktei.com Git - BK-2020-03.git/commitdiff
feat(unitproc/bkt-dateDuration):Count seconds between two dates
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sun, 26 Apr 2026 23:34:45 +0000 (23:34 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Sun, 26 Apr 2026 23:34:45 +0000 (23:34 +0000)
unitproc/bkt-dateDuration [new file with mode: 0755]
unitproc/bkt-timeDuration

diff --git a/unitproc/bkt-dateDuration b/unitproc/bkt-dateDuration
new file mode 100755 (executable)
index 0000000..0b09e66
--- /dev/null
@@ -0,0 +1,41 @@
+#!/bin/bash
+
+yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
+die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
+must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
+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
+    # 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
+
+    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)
+    re='@.*';
+    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)";
+
+    # Calculate duration
+    duration="$((unixEpoch2 - unixEpoch1))";
+    duration="$(sed -e 's/^-//' <<<"$duration"; )";
+
+    printf "%s\n" "$duration";
+};
+
+# Examples
+dateDuration 2026-06-30 2026-07-01; # 86400 seconds
+dateDuration 2012-06-30 2012-07-01; # 86401 seconds (leap second)
index 1db408b8c101a203a026a03ab16486b2544a23f8..7786723446f6fe66c84e20c895d636bbe78e93a9 100644 (file)
@@ -2,9 +2,9 @@
 
 # Desc: Template to indicate time duration in ISO-8601 format
 
 
 # Desc: Template to indicate time duration in ISO-8601 format
 
-yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
-die() { yell "$*"; exit 111; }
-try() { "$@" || die "cannot $*"; }
+yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
+die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
+must() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
 timeDuration(){
     # Desc: Given seconds, output ISO-8601 duration string
     # Ref/Attrib: ISO-8601:2004(E), §4.4.4.2 Representations of time intervals by duration and context information
 timeDuration(){
     # Desc: Given seconds, output ISO-8601 duration string
     # Ref/Attrib: ISO-8601:2004(E), §4.4.4.2 Representations of time intervals by duration and context information