From afb37746466e8a2b1a856a0e8319b753a6c86c0d Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 5 Feb 2023 04:41:52 +0400 Subject: [PATCH] Added ToClientTimezone() util func. --- pkg/util/util.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 pkg/util/util.go diff --git a/pkg/util/util.go b/pkg/util/util.go new file mode 100644 index 0000000..16df29f --- /dev/null +++ b/pkg/util/util.go @@ -0,0 +1,21 @@ +package util + +import ( + "net/http" + "time" +) + +// ToClientTimezone converts given time to timezone set in a +// X-Client-Timezone header. If this header is not set, then +// converts to UTC. +func ToClientTimezone(t time.Time, r *http.Request) time.Time { + if tz := r.Header.Get("X-Client-Timezone"); tz != "" { + loc, err := time.LoadLocation(tz) + if err != nil { + return t.UTC() + } + return t.In(loc) + } + + return t.UTC() +}