From 6f152673f2bb3b4c499a7ee82e280c3f4b80560d Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Thu, 31 Mar 2022 02:54:04 +0400 Subject: [PATCH] Added ToClientTimezone() function to convert time to client's local time. --- pkg/utils/dwelling.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/utils/dwelling.go b/pkg/utils/dwelling.go index c696f78..789e995 100644 --- a/pkg/utils/dwelling.go +++ b/pkg/utils/dwelling.go @@ -1,6 +1,10 @@ package utils -import "strings" +import ( + "net/http" + "strings" + "time" +) // MainSite returns homepage address depending on network used. func MainSite(host string) string { @@ -12,3 +16,18 @@ func MainSite(host string) string { return "https://arav.top" } + +// 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) + } else { + return t.UTC() + } +}