chore(unitproc):timeDuration:Update depends info
[BK-2020-03.git] / unitproc / bktemp-setTimeZoneEV
1 #!/bin/bash
2
3 # Desc: Set time zone environment variable TZ
4
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 $*"; }
8 setTimeZoneEV(){
9 # Desc: Set time zone environment variable TZ
10 # Usage: setTimeZoneEV arg1
11 # Version 0.1.1
12 # Input: arg1: 'date'-compatible timezone string (ex: "America/New_York")
13 # TZDIR env var (optional; default: "/usr/share/zoneinfo")
14 # Output: exports TZ
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, bash 5
19 # Tested on: Debian 10
20 local tzDir returnState argTimeZone
21
22 argTimeZone="$1"
23 if ! [[ $# -eq 1 ]]; then
24 yell "ERROR:Invalid argument count.";
25 return 1;
26 fi
27
28 # Read TZDIR env var if available
29 if printenv TZDIR 1>/dev/null 2>&1; then
30 tzDir="$(printenv TZDIR)";
31 else
32 tzDir="/usr/share/zoneinfo";
33 fi
34
35 # Validate TZ string
36 if ! [[ -f "$tzDir"/"$argTimeZone" ]]; then
37 yell "ERROR:Invalid time zone argument.";
38 return 2;
39 else
40 # Export ARG1 as TZ environment variable
41 TZ="$argTimeZone" && export TZ && returnState="true";
42 fi
43
44 # Determine function return code
45 if [ "$returnState" = "true" ]; then
46 return 0;
47 fi
48 } # Exports TZ environment variable
49
50 #==BEGIN sample code==
51 date --iso-8601=seconds; echo "==============="; sleep 2
52
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
57
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
62
63 date --iso-8601=seconds
64 cmd3="setTimeZoneEV";
65 echo "Running:$cmd3"; $cmd3; echo "Exit code:$?"
66 date --iso-8601=seconds; echo "==============="; sleep 2
67
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
72
73 try setTimeZoneEV Atlantic/Atlantis
74 #==END sample code==
75
76 # Author: Steven Baltakatei Sandoval
77 # License: GPLv3+
78
79