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