X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/fc1495ab3996d2ded45e6e5ef71d1d256f5eede0..a9a36cc48e1b18d63c1b1f240c9347191ec8d67f:/unitproc/bktemp-dateShort?ds=inline diff --git a/unitproc/bktemp-dateShort b/unitproc/bktemp-dateShort index 425cf85..5757eae 100644 --- a/unitproc/bktemp-dateShort +++ b/unitproc/bktemp-dateShort @@ -1,18 +1,45 @@ #!/bin/bash - # Desc: Template to display date +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 dateShort(){ # Desc: Date without separators (YYYYmmdd) - # Usage: dateShort + # Usage: dateShort ([str date]) + # Version: 1.1.2 + # Input: arg1: 'date'-parsable timestamp string (optional) # Output: stdout: date (ISO-8601, no separators) - TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. - DATE_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day. - echo "$DATE_CURRENT_SHORT"; -} # Get date without separators + # Depends: bash 5.0.3, date 8.30, yell() + local argTime timeCurrent timeInput dateCurrentShort + + argTime="$1"; + # Get Current Time + timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 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 + timeInput="$timeCurrent"; + else + ## F: Time argument exists, validate time + if date --date="$argTime" 1>/dev/null 2>&1; then + ### T: Time argument is valid; use it + timeInput="$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 + dateCurrentShort="$(date -d "$timeInput" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day. + echo "$dateCurrentShort"; +} # Get YYYYmmdd #==BEGIN sample code== -echo "The current day is:$(dateShort)" +echo "The current day is :$(dateShort)"; +echo "Contact lost with STS-107 on:$(dateShort "2003-02-01T08:59:15-05:00")"; +echo "Bitcoin started on :$(dateShort "@1231006505")"; #==END sample code== # Author: Steven Baltakatei Sandoval