| 1 | #!/bin/bash |
| 2 | |
| 3 | # Desc: Template to display date and time. |
| 4 | |
| 5 | yell() { echo "$0: $*" >&2; } #o Yell, Die, Try Three-Fingered Claw technique |
| 6 | die() { yell "$*"; exit 111; } #o Ref/Attrib: https://stackoverflow.com/a/25515370 |
| 7 | try() { "$@" || die "cannot $*"; } #o |
| 8 | dateTimeShort(){ |
| 9 | # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz) |
| 10 | # Usage: dateTimeShort ([str date]) |
| 11 | # Version 1.1.0 |
| 12 | # Input: arg1: 'date'-parsable timestamp string (optional) |
| 13 | # Output: stdout: timestamp (ISO-8601, no separators) |
| 14 | # Depends: yell |
| 15 | local TIME_CURRENT TIME_CURRENT_SHORT |
| 16 | |
| 17 | argTime="$1"; |
| 18 | # Get Current Time |
| 19 | TIME_CURRENT="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
| 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 |
| 24 | TIME_INPUT="$TIME_CURRENT"; |
| 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 |
| 29 | TIME_INPUT="$argTime"; |
| 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 |
| 36 | TIME_CURRENT_SHORT="$(date -d "$TIME_INPUT" +%Y%m%dT%H%M%S%z)"; |
| 37 | echo "$TIME_CURRENT_SHORT"; |
| 38 | } # Get date&time without separators |
| 39 | |
| 40 | #==BEGIN sample code== |
| 41 | echo "The current day and time is :$(dateTimeShort)"; |
| 42 | echo "Contact lost with STS-107 on:$(dateTimeShort "2003-02-01T08:59:15-05:00")"; |
| 43 | echo "Bitcoin started on :$(dateTimeShort "@1231006505")"; |
| 44 | |
| 45 | #==END sample code== |
| 46 | |
| 47 | # Author: Steven Baltakatei Sandoval |
| 48 | # License: GPLv3+ |