X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/0f48af59d8759f8a46dfacf58758476ddbe2a3cf..0e1bef1674d633af4a75f32d7c7ccd68c5fe2524:/unitproc/bktemp-dateTimeShort diff --git a/unitproc/bktemp-dateTimeShort b/unitproc/bktemp-dateTimeShort index dc470e0..2a31d1f 100644 --- a/unitproc/bktemp-dateTimeShort +++ b/unitproc/bktemp-dateTimeShort @@ -2,17 +2,48 @@ # 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 + # Usage: dateTimeShort ([str date]) + # Version 1.1.1 + # Input: arg1: 'date'-parsable timestamp string (optional) # Output: stdout: timestamp (ISO-8601, no separators) - 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. - echo "$TIME_CURRENT_SHORT"; -} # Get date&time without separators + # Depends: yell + local argTime timeCurrent timeInput timeCurrentShort + + 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 + timeCurrentShort="$(date -d "$timeInput" +%Y%m%dT%H%M%S%z)"; + echo "$timeCurrentShort"; +} # Get YYYYmmddTHHMMSS±zzzz #==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")"; +testDate="2020-07-07T02:30:11,690097074+00:00"; +echo "This string was generated using 'date --iso-8601=ns':$testDate"; +echo "Using dateTimeShort, it appears as:$(dateTimeShort "$testDate")"; #==END sample code== # Author: Steven Baltakatei Sandoval