feat(unitproc):datetimeShort:Use 1st argument as input date
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Fri, 3 Jul 2020 18:17:13 +0000 (18:17 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Fri, 3 Jul 2020 18:17:13 +0000 (18:17 +0000)
unitproc/bktemp-dateTimeShort

index c4130242fd12296df54a704e6fad0f17c3320322..b469d327683153c0ab1a719e615b1636138929c2 100644 (file)
@@ -2,19 +2,46 @@
 
 # Desc: Template to display date and time.
 
+yell() { echo "$0: $*" >&2; }      #o Yell, Die, Try Three-Fingered Claw technique
+die() { yell "$*"; exit 111; }     #o Ref/Attrib: https://stackoverflow.com/a/25515370
+try() { "$@" || die "cannot $*"; } #o
 dateTimeShort(){
     # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
-    # Usage: dateTimeShort
-    # Version 1.0.0
+    # Usage: dateTimeShort ([str date])
+    # Version 1.1.0
+    # Input: arg1: 'date'-parsable timestamp string (optional)
     # Output: stdout: timestamp (ISO-8601, no separators)
+    # Depends: yell
     local TIME_CURRENT TIME_CURRENT_SHORT
+
+    argTime="$1";
+    # Get Current Time
     TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
-    TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"; # Produce separator-less current timestamp with resolution 1 second.
+    # Decide to parse current or supplied date
+    ## Check if time argument empty
+    if [[ -z "$argTime" ]]; then
+       ## T: Time argument empty, use current time
+       TIME_INPUT="$TIME_CURRENT";
+    else
+       ## F: Time argument exists, validate time
+       if date --date="$argTime" 1>/dev/null 2>&1; then
+           ### T: Time argument is valid; use it
+           TIME_INPUT="$argTime";
+       else
+           ### F: Time argument not valid; exit
+           yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
+       fi
+    fi
+    # Construct and deliver separator-les date string
+    TIME_CURRENT_SHORT="$(date -d "$TIME_INPUT" +%Y%m%dT%H%M%S%z)";
     echo "$TIME_CURRENT_SHORT";
 } # Get date&time without separators
 
 #==BEGIN sample code==
-echo "The current day and time is:$(dateTimeShort)"
+echo "The current day and time is :$(dateTimeShort)";
+echo "Contact lost with STS-107 on:$(dateTimeShort "2003-02-01T08:59:15-05:00")";
+echo "Bitcoin started on          :$(dateTimeShort "@1231006505")";
+
 #==END sample code==
 
 # Author: Steven Baltakatei Sandoval