From: Steven Baltakatei Sandoval Date: Wed, 1 Jul 2020 17:43:34 +0000 (+0000) Subject: feat(unitproc):Add setTimeZoneEV function X-Git-Tag: 0.2.0~17 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/d8708eb1722b30fd0e6c1ac1bf909f127a8fa040 feat(unitproc):Add setTimeZoneEV function Validates and sets timezone according to first argument. Otherwise returns non-zero exit code. --- diff --git a/unitproc/bktemp-setTimeZoneEV b/unitproc/bktemp-setTimeZoneEV new file mode 100644 index 0000000..6933752 --- /dev/null +++ b/unitproc/bktemp-setTimeZoneEV @@ -0,0 +1,74 @@ +#!/bin/bash + +# Desc: Set time zone environment variable TZ + +yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370 +die() { yell "$*"; exit 111; } +try() { "$@" || die "cannot $*"; } +setTimeZoneEV(){ + # Desc: Set time zone environment variable TZ + # Usage: setTimeZoneEV arg1 + # Input: arg1: 'date'-compatible timezone string (ex: "America/New_York") + # TZDIR env var (optional; default: "/usr/share/zoneinfo") + # Output: exports TZ + # exit code 0 on success + # exit code 1 on incorrect number of arguments + # exit code 2 if unable to validate arg1 + # Depends: yell, die, try + ARG1="$1" + local tzDir returnState + if ! [[ $# -eq 1 ]]; then + yell "ERROR:Invalid argument count."; + return 1; + fi + + # Read TZDIR env var if available + if printenv TZDIR 1>/dev/null 2>&1; then + tzDir="$(printenv TZDIR)"; + else + tzDir="/usr/share/zoneinfo"; + fi + + # Validate TZ string + if ! [[ -f "$tzDir"/"$ARG1" ]]; then + yell "ERROR:Invalid time zone argument."; + return 2; + else + # Export ARG1 as TZ environment variable + TZ="$ARG1" && export TZ && returnState="true"; + fi + + # Determine function return code + if [ "$returnState" = "true" ]; then + return 0; + fi +} # Exports TZ environment variable + +#==BEGIN sample code== +date --iso-8601=seconds; echo "==============="; sleep 2 + +date --iso-8601=seconds +cmd1="setTimeZoneEV America/New_York" +echo "Running:$cmd1"; $cmd1; echo "Exit code:$?" +date --iso-8601=seconds; echo "==============="; sleep 2 + +date --iso-8601=seconds +cmd2="setTimeZoneEV Asia/Tokyo" +echo "Running:$cmd2"; $cmd2; echo "Exit code:$?" +date --iso-8601=seconds; echo "==============="; sleep 2 + +date --iso-8601=seconds +cmd3="setTimeZoneEV"; +echo "Running:$cmd3"; $cmd3; echo "Exit code:$?" +date --iso-8601=seconds; echo "==============="; sleep 2 + +date --iso-8601=seconds +cmd4="setTimeZoneEV Pacific/Lemuria" +echo "Running:$cmd4"; $cmd4; echo "Exit code:$?" +date --iso-8601=seconds; echo "==============="; sleep 2 +#==END sample code== + +# Author: Steven Baltakatei Sandoval +# License: GPLv3+ + +