Commit | Line | Data |
---|---|---|
fc1495ab | 1 | #!/bin/bash |
fc1495ab SBS |
2 | # Desc: Template to display date |
3 | ||
ea368c74 SBS |
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 | |
fc1495ab SBS |
7 | dateShort(){ |
8 | # Desc: Date without separators (YYYYmmdd) | |
ea368c74 | 9 | # Usage: dateShort ([str date]) |
ee519cfd | 10 | # Version: 1.1.2 |
ea368c74 | 11 | # Input: arg1: 'date'-parsable timestamp string (optional) |
fc1495ab | 12 | # Output: stdout: date (ISO-8601, no separators) |
ee519cfd | 13 | # Depends: bash 5.0.3, date 8.30, yell() |
26961aa3 | 14 | local argTime timeCurrent timeInput dateCurrentShort |
ea368c74 SBS |
15 | |
16 | argTime="$1"; | |
17 | # Get Current Time | |
26961aa3 | 18 | timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
ea368c74 SBS |
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 | |
26961aa3 | 23 | timeInput="$timeCurrent"; |
ea368c74 SBS |
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 | |
26961aa3 | 28 | timeInput="$argTime"; |
ea368c74 SBS |
29 | else |
30 | ### F: Time argument not valid; exit | |
31 | yell "ERROR:Invalid time argument supplied. Exiting."; exit 1; | |
ee519cfd SBS |
32 | fi; |
33 | fi; | |
ea368c74 | 34 | # Construct and deliver separator-les date string |
26961aa3 SBS |
35 | dateCurrentShort="$(date -d "$timeInput" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day. |
36 | echo "$dateCurrentShort"; | |
50aec114 | 37 | } # Get YYYYmmdd |
fc1495ab SBS |
38 | |
39 | #==BEGIN sample code== | |
ea368c74 SBS |
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")"; | |
fc1495ab SBS |
43 | #==END sample code== |
44 | ||
45 | # Author: Steven Baltakatei Sandoval | |
46 | # License: GPLv3+ |