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