style(unitproc/):Shorten names of template prefix (bktemp -> bkt)
[BK-2020-03.git] / unitproc / bkt-dateShort
1 #!/bin/bash
2 # Desc: Template to display date
3
4 yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique
5 die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370
6 try() { "$@" || die "cannot $*"; } #o
7 dateShort(){
8 # Desc: Date without separators (YYYYmmdd)
9 # Usage: dateShort ([str date])
10 # Version: 1.1.2
11 # Input: arg1: 'date'-parsable timestamp string (optional)
12 # Output: stdout: date (ISO-8601, no separators)
13 # Depends: bash 5.0.3, date 8.30, yell()
14 local argTime timeCurrent timeInput dateCurrentShort
15
16 argTime="$1";
17 # Get Current Time
18 timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
19 # Decide to parse current or supplied date
20 ## Check if time argument empty
21 if [[ -z "$argTime" ]]; then
22 ## T: Time argument empty, use current time
23 timeInput="$timeCurrent";
24 else
25 ## F: Time argument exists, validate time
26 if date --date="$argTime" 1>/dev/null 2>&1; then
27 ### T: Time argument is valid; use it
28 timeInput="$argTime";
29 else
30 ### F: Time argument not valid; exit
31 yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
32 fi;
33 fi;
34 # Construct and deliver separator-les date string
35 dateCurrentShort="$(date -d "$timeInput" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day.
36 echo "$dateCurrentShort";
37 } # Get YYYYmmdd
38
39 #==BEGIN sample code==
40 echo "The current day is :$(dateShort)";
41 echo "Contact lost with STS-107 on:$(dateShort "2003-02-01T08:59:15-05:00")";
42 echo "Bitcoin started on :$(dateShort "@1231006505")";
43 #==END sample code==
44
45 # Author: Steven Baltakatei Sandoval
46 # License: GPLv3+