1
0
Fork 0

Added ToClientTimezone() util func.

This commit is contained in:
Alexander Andreev 2023-02-05 04:41:52 +04:00
parent 9e3a7fddcc
commit afb3774646
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F
1 changed files with 21 additions and 0 deletions

21
pkg/util/util.go Normal file
View File

@ -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()
}