- # Depends: date 8
- local currentTime epochSeconds fracNanosec epochNanosec
- currentTime="$(date +%s.%N)";
- epochSeconds="$(echo "$currentTime" | cut -d. -f1)";
- fracNanosec="$(echo "$currentTime" | cut -d. -f2)";
- epochNanosec="$(( ("$epochSeconds" * 1000000000) + ("$fracNanosec") ))";
- echo "$epochNanosec";
+ # Depends: date 8, cut 8, yell()
+ # Ref/Attrib: Force base 10 Bash arith with '10#'. https://stackoverflow.com/a/24777667
+ local argTime timeCurrent timeInput timeEpochFloat timeEpochInt
+ local timeEpochNsFrac timeEpochNs
+
+ argTime="$1";
+
+ # Get Current Time
+ timeCurrent="$(date --iso-8601=ns)"; # Produce `date`-parsable current timestamp with resolution of 1 nanosecond.
+
+ # Decide to parse current or supplied time
+ ## Check if time argument empty
+ if [[ -z "$argTime" ]]; then
+ ## T: Time argument empty, use current time
+ timeInput="$timeCurrent";
+ else
+ ## F: Time argument exists, validate time
+ if date --date="$argTime" 1>/dev/null 2>&1; then
+ ### T: Time argument is valid; use it
+ timeInput="$argTime";
+ else
+ ### F: Time argument not valid; exit
+ yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
+ fi;
+ fi;
+ # Construct and deliver nanoseconds since 1970-01-01
+ timeEpochFloat="$(date --date="$timeInput" +%s.%N)"; # Save ssss.NNNNNNNNN
+ timeEpochInt="$(echo "$timeEpochFloat" | cut -d. -f1)"; # Get ssss
+ timeEpochNsFrac="$(echo "$timeEpochFloat" | cut -d. -f2)"; # Get NNNNNNNNN
+ timeEpochNs="$(( (10#"$timeEpochInt" * 10**9) + (10#"$timeEpochNsFrac") ))";
+ echo "$timeEpochNs";