1
0

Added ToClientTimezone() function to convert time to client's local time.

This commit is contained in:
Alexander Andreev 2022-03-31 02:54:04 +04:00
parent a21a361167
commit 6f152673f2
Signed by: Arav
GPG Key ID: 1327FE8A374CC86F

View File

@ -1,6 +1,10 @@
package utils package utils
import "strings" import (
"net/http"
"strings"
"time"
)
// MainSite returns homepage address depending on network used. // MainSite returns homepage address depending on network used.
func MainSite(host string) string { func MainSite(host string) string {
@ -12,3 +16,18 @@ func MainSite(host string) string {
return "https://arav.top" 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()
}
}