chore(unitproc/bkt-processArgs):Update with die()
[BK-2020-03.git] / unitproc / bkt-dateTimeShort
CommitLineData
ea8ea560
SBS
1#!/bin/bash
2
3# Desc: Template to display date and time.
4
6962f268
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
ea8ea560
SBS
8dateTimeShort(){
9 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
6962f268 10 # Usage: dateTimeShort ([str date])
04b897d4 11 # Version 1.1.1
6962f268 12 # Input: arg1: 'date'-parsable timestamp string (optional)
ea8ea560 13 # Output: stdout: timestamp (ISO-8601, no separators)
6962f268 14 # Depends: yell
04b897d4 15 local argTime timeCurrent timeInput timeCurrentShort
6962f268
SBS
16
17 argTime="$1";
18 # Get Current Time
04b897d4 19 timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
6962f268
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
04b897d4 24 timeInput="$timeCurrent";
6962f268
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
04b897d4 29 timeInput="$argTime";
6962f268
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
04b897d4
SBS
36 timeCurrentShort="$(date -d "$timeInput" +%Y%m%dT%H%M%S%z)";
37 echo "$timeCurrentShort";
d464a1cb 38} # Get YYYYmmddTHHMMSS±zzzz
ea8ea560
SBS
39
40#==BEGIN sample code==
6962f268
SBS
41echo "The current day and time is :$(dateTimeShort)";
42echo "Contact lost with STS-107 on:$(dateTimeShort "2003-02-01T08:59:15-05:00")";
43echo "Bitcoin started on :$(dateTimeShort "@1231006505")";
e9332c64
SBS
44testDate="2020-07-07T02:30:11,690097074+00:00";
45echo "This string was generated using 'date --iso-8601=ns':$testDate";
46echo "Using dateTimeShort, it appears as:$(dateTimeShort "$testDate")";
ea8ea560
SBS
47#==END sample code==
48
49# Author: Steven Baltakatei Sandoval
50# License: GPLv3+