Merge branch 'feature/freqWrite/BK-2020-03' into develop
[BK-2020-03.git] / unitproc / bktemp-setTimeZoneEV
CommitLineData
d8708eb1
SBS
1#!/bin/bash
2
3# Desc: Set time zone environment variable TZ
4
5yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370
6die() { yell "$*"; exit 111; }
7try() { "$@" || die "cannot $*"; }
8setTimeZoneEV(){
9 # Desc: Set time zone environment variable TZ
10 # Usage: setTimeZoneEV arg1
ffcae1aa 11 # Version 0.1.1
d8708eb1
SBS
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
00e89a7c
SBS
18 # Depends: yell, printenv, bash 5
19 # Tested on: Debian 10
ffcae1aa
SBS
20 local tzDir returnState argTimeZone
21
22 argTimeZone="$1"
d8708eb1
SBS
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
ffcae1aa 36 if ! [[ -f "$tzDir"/"$argTimeZone" ]]; then
d8708eb1
SBS
37 yell "ERROR:Invalid time zone argument.";
38 return 2;
39 else
40 # Export ARG1 as TZ environment variable
ffcae1aa 41 TZ="$argTimeZone" && export TZ && returnState="true";
d8708eb1
SBS
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==
51date --iso-8601=seconds; echo "==============="; sleep 2
52
53date --iso-8601=seconds
54cmd1="setTimeZoneEV America/New_York"
55echo "Running:$cmd1"; $cmd1; echo "Exit code:$?"
56date --iso-8601=seconds; echo "==============="; sleep 2
57
58date --iso-8601=seconds
59cmd2="setTimeZoneEV Asia/Tokyo"
60echo "Running:$cmd2"; $cmd2; echo "Exit code:$?"
61date --iso-8601=seconds; echo "==============="; sleep 2
62
63date --iso-8601=seconds
64cmd3="setTimeZoneEV";
65echo "Running:$cmd3"; $cmd3; echo "Exit code:$?"
66date --iso-8601=seconds; echo "==============="; sleep 2
67
68date --iso-8601=seconds
69cmd4="setTimeZoneEV Pacific/Lemuria"
70echo "Running:$cmd4"; $cmd4; echo "Exit code:$?"
71date --iso-8601=seconds; echo "==============="; sleep 2
23e14375
SBS
72
73try setTimeZoneEV Atlantic/Atlantis
d8708eb1
SBS
74#==END sample code==
75
76# Author: Steven Baltakatei Sandoval
77# License: GPLv3+
78
79