1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Entries date should contains user timezone (API)

This commit is contained in:
Frédéric Guillot 2018-03-01 21:24:58 -08:00
parent f110384f11
commit 0c7039de0e
5 changed files with 146 additions and 17 deletions

47
timezone/timezone.go Normal file
View file

@ -0,0 +1,47 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package timezone
import (
"time"
)
// Convert converts provided date time to actual timezone.
func Convert(tz string, t time.Time) time.Time {
userTimezone := getLocation(tz)
if t.Location().String() == "" {
// In this case, the provided date is already converted to the user timezone by Postgres,
// but the timezone information is not set in the time struct.
// We cannot use time.In() because the date will be converted a second time.
t = time.Date(
t.Year(),
t.Month(),
t.Day(),
t.Hour(),
t.Minute(),
t.Second(),
t.Nanosecond(),
userTimezone,
)
} else if t.Location() != userTimezone {
t = t.In(userTimezone)
}
return t
}
// Now returns the current time with the given timezone.
func Now(tz string) time.Time {
return time.Now().In(getLocation(tz))
}
func getLocation(tz string) *time.Location {
loc, err := time.LoadLocation(tz)
if err != nil {
loc = time.Local
}
return loc
}