summaryrefslogtreecommitdiff
path: root/time/time.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2021-01-24 15:20:19 -0500
committerDave Henderson <dhenderson@gmail.com>2021-01-24 15:20:19 -0500
commit2ce20bdde18b062b19261a850e7342bb28087eec (patch)
tree0359b9d95d53b1f4141e537f637f468c9b3800bf /time/time.go
parent96ef4852be0b0c8db5c0ede7dbe6738e0df1eb86 (diff)
Support changing TZ env var in Zone functions
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'time/time.go')
-rw-r--r--time/time.go19
1 files changed, 17 insertions, 2 deletions
diff --git a/time/time.go b/time/time.go
index 58b18eca..9fb0841f 100644
--- a/time/time.go
+++ b/time/time.go
@@ -3,16 +3,31 @@ package time
import (
"time"
+
+ "github.com/hairyhenderson/gomplate/v3/env"
)
// ZoneName - a convenience function for determining the current timezone's name
func ZoneName() string {
- n, _ := time.Now().Zone()
+ n, _ := zone()
return n
}
// ZoneOffset - determine the current timezone's offset, in seconds east of UTC
func ZoneOffset() int {
- _, o := time.Now().Zone()
+ _, o := zone()
return o
}
+
+func zone() (string, int) {
+ // re-read TZ env var in case it's changed since the process started.
+ // This may happen in certain rare instances when this is being called as a
+ // library, or in a test. It allows for a bit more flexibility too, as
+ // changing time.Local is prone to data races.
+ tz := env.Getenv("TZ", "Local")
+ loc, err := time.LoadLocation(tz)
+ if err != nil {
+ loc = time.Local
+ }
+ return time.Now().In(loc).Zone()
+}