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     # In : arg1: 'date'-compatible timezone string (ex: "America/New_York") 
  13     #        TZDIR env var (optional; default: "/usr/share/zoneinfo") 
  14     # Out: exports TZ (env var) 
  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: bash 5.0.3, printenv 8.30, yell() 
  19     local tzDir returnState argTimeZone
 
  22     if ! [[ $# -eq 1 ]]; then 
  23         yell 
"ERROR:Invalid argument count."; 
  27     # Read TZDIR env var if available 
  28     if printenv TZDIR 
1>/dev
/null 
2>&1; then 
  29         tzDir
="$(printenv TZDIR)"; 
  31         tzDir
="/usr/share/zoneinfo"; 
  35     if ! [[ -f "$tzDir"/"$argTimeZone" ]]; then 
  36         yell 
"ERROR:Invalid time zone argument."; 
  39     # Export ARG1 as TZ environment variable 
  40         TZ
="$argTimeZone" && export TZ 
&& returnState
="true"; 
  43     # Determine function return code 
  44     if [ "$returnState" = "true" ]; then 
  47 } # Exports TZ environment variable 
  49 #==BEGIN sample code== 
  50 date --iso-8601=seconds
; echo "==============="; sleep 2 
  52 date --iso-8601=seconds
 
  53 cmd1
="setTimeZoneEV America/New_York" 
  54 echo "Running:$cmd1"; $cmd1; echo "Exit code:$?" 
  55 date --iso-8601=seconds
; echo "==============="; sleep 2 
  57 date --iso-8601=seconds
 
  58 cmd2
="setTimeZoneEV Asia/Tokyo" 
  59 echo "Running:$cmd2"; $cmd2; echo "Exit code:$?" 
  60 date --iso-8601=seconds
; echo "==============="; sleep 2 
  62 date --iso-8601=seconds
 
  64 echo "Running:$cmd3"; $cmd3; echo "Exit code:$?" 
  65 date --iso-8601=seconds
; echo "==============="; sleep 2 
  67 date --iso-8601=seconds
 
  68 cmd4
="setTimeZoneEV Pacific/Lemuria" 
  69 echo "Running:$cmd4"; $cmd4; echo "Exit code:$?" 
  70 date --iso-8601=seconds
; echo "==============="; sleep 2 
  72 try setTimeZoneEV Atlantic
/Atlantis
 
  75 # Author: Steven Baltakatei Sandoval