#!/bin/bash
#                       /usr/local/bin/now
# https://crystalfaeries.net/posix/bin/now
# celeste:crystalfaery NOW 2018-07-02 18:59:38+00:00
# rewrote to require only BusyBox's partial implementation of date vs GNU's.
# rewrote to accomodate TermUX's coreutils version of cut
# LEGACY COMPATABILITY:	accept ANY argument to sanitize output for file paths
# We SHOULD settle on a choice of argument/switch letter!
date -u:	Mon Jul  2 18:47:38 UTC 2018
DoW=$(date -u | sed 's/  / /g;s/ /	/g' | cut -f1)
echo "DoW	$DoW"	1>&2
Mon=$(date -u | sed 's/  / /g;s/ /	/g' | cut -f2 -s)
echo "Mon	$Mon"	1>&2
case	"${Mon}" in
Jan)	Month="01";;
Feb)	Month="02";;
Mar)	Month="03";;
Apr)	Month="04";;
May)	Month="05";;
Jun)	Month="06";;
Jul)	Month="07";;
Aug)	Month="08";;
Sep)	Month="09";;
Oct)	Month="10";;
Nov)	Month="11";;
Dec)	Month="12";;
*)	Month="00"
	echo	"Invalid Month Abbreviation: ${Mon}"	1>&2
	exit	13;;	# Initially Strict on 3-char abbreviations
esac
echo "Month	$Month"	1>&2
Day=$(date -u | sed 's/  / /g;s/ /	/g' | cut -f3 -s)
echo "Day	$Day"	1>&2
case "${Day}" in
1)	Day="0${Day}";;
2)	Day="0${Day}";;
3)	Day="0${Day}";;
4)	Day="0${Day}";;
5)	Day="0${Day}";;
6)	Day="0${Day}";;
7)	Day="0${Day}";;
8)	Day="0${Day}";;
9)	Day="0${Day}";;
*)	;;
esac
echo "Day	$Day"	1>&2
Time=$(date -u | sed 's/  / /g;s/ /	/g' | cut -f4 -s)
echo "Time	$Time"	1>&2
Sane=$(echo "${Time}" | sed 's/  / /g;s/:/-/g')
echo "Sane	$Sane"	1>&2
TZ=$(date -u | sed 's/  / /g;s/ /	/g' | cut -f5 -s)
echo "TZ	$TZ"	1>&2
Year=$(date -u | sed 's/  / /g;s/ /	/g' | cut -f6 -s)
echo "Year	$Year"	1>&2

if [ $# == 0 ]
then	# default format:
#	date -u --rfc-3339=seconds
	Now="${Year}-${Month}-${Day} ${Time}+00:00"

else	# sanitize for use in a filename:
#	date -u --rfc-3339=seconds \
#|	sed 's/:[0-9][0-9]+00:00// ; s/ /./g ; s/:/-/'
	Now="${Year}-${Month}-${Day}.${Sane}.${TZ}"

fi
echo	"${Now}"
exit	$?
