3 # Desc: Set time zone environment variable TZ
5 yell
() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
6 die
() { yell
"$*"; exit 111; }
7 try
() { "$@" || die
"cannot $*"; }
9 # Desc: Set time zone environment variable TZ
10 # Usage: setTimeZoneEV arg1
12 # Input: arg1: 'date'-compatible timezone string (ex: "America/New_York")
13 # TZDIR env var (optional; default: "/usr/share/zoneinfo")
15 # exit code 0 on success
16 # exit code 1 on incorrect number of arguments
17 # exit code 2 if unable to validate arg1
18 # Depends: yell(), printenv 8.30, bash 5.0.3
19 # Tested on: Debian 10
20 local tzDir returnState argTimeZone
23 if ! [[ $# -eq 1 ]]; then
24 yell
"ERROR:Invalid argument count.";
28 # Read TZDIR env var if available
29 if printenv TZDIR
1>/dev
/null
2>&1; then
30 tzDir
="$(printenv TZDIR)";
32 tzDir
="/usr/share/zoneinfo";
36 if ! [[ -f "$tzDir"/"$argTimeZone" ]]; then
37 yell
"ERROR:Invalid time zone argument.";
40 # Export ARG1 as TZ environment variable
41 TZ
="$argTimeZone" && export TZ
&& returnState
="true";
44 # Determine function return code
45 if [ "$returnState" = "true" ]; then
48 } # Exports TZ environment variable
50 #==BEGIN sample code==
51 date --iso-8601=seconds
; echo "==============="; sleep 2
53 date --iso-8601=seconds
54 cmd1
="setTimeZoneEV America/New_York"
55 echo "Running:$cmd1"; $cmd1; echo "Exit code:$?"
56 date --iso-8601=seconds
; echo "==============="; sleep 2
58 date --iso-8601=seconds
59 cmd2
="setTimeZoneEV Asia/Tokyo"
60 echo "Running:$cmd2"; $cmd2; echo "Exit code:$?"
61 date --iso-8601=seconds
; echo "==============="; sleep 2
63 date --iso-8601=seconds
65 echo "Running:$cmd3"; $cmd3; echo "Exit code:$?"
66 date --iso-8601=seconds
; echo "==============="; sleep 2
68 date --iso-8601=seconds
69 cmd4
="setTimeZoneEV Pacific/Lemuria"
70 echo "Running:$cmd4"; $cmd4; echo "Exit code:$?"
71 date --iso-8601=seconds
; echo "==============="; sleep 2
73 try setTimeZoneEV Atlantic
/Atlantis
76 # Author: Steven Baltakatei Sandoval