mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
fix: json encoding is failing with dates at OAD and negative timezone offset
This commit is contained in:
parent
7275bc808a
commit
c951ac2876
2 changed files with 20 additions and 2 deletions
|
@ -12,10 +12,14 @@ func Convert(tz string, t time.Time) time.Time {
|
||||||
userTimezone := getLocation(tz)
|
userTimezone := getLocation(tz)
|
||||||
|
|
||||||
if t.Location().String() == "" {
|
if t.Location().String() == "" {
|
||||||
|
if t.Before(time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)) {
|
||||||
|
return time.Date(0, time.January, 1, 0, 0, 0, 0, userTimezone)
|
||||||
|
}
|
||||||
|
|
||||||
// In this case, the provided date is already converted to the user timezone by Postgres,
|
// 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.
|
// 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.
|
// We cannot use time.In() because the date will be converted a second time.
|
||||||
t = time.Date(
|
return time.Date(
|
||||||
t.Year(),
|
t.Year(),
|
||||||
t.Month(),
|
t.Month(),
|
||||||
t.Day(),
|
t.Day(),
|
||||||
|
@ -26,7 +30,7 @@ func Convert(tz string, t time.Time) time.Time {
|
||||||
userTimezone,
|
userTimezone,
|
||||||
)
|
)
|
||||||
} else if t.Location() != userTimezone {
|
} else if t.Location() != userTimezone {
|
||||||
t = t.In(userTimezone)
|
return t.In(userTimezone)
|
||||||
}
|
}
|
||||||
|
|
||||||
return t
|
return t
|
||||||
|
|
|
@ -75,3 +75,17 @@ func TestConvertTimeWithIdenticalTimezone(t *testing.T) {
|
||||||
t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
|
t.Fatalf(`Unexpected time, got hours=%d, minutes=%d, secs=%d`, hours, minutes, secs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConvertPostgresDateTimeWithNegativeTimezoneOffset(t *testing.T) {
|
||||||
|
tz := "US/Eastern"
|
||||||
|
input := time.Date(0, 1, 1, 0, 0, 0, 0, time.FixedZone("", -5))
|
||||||
|
output := Convert(tz, input)
|
||||||
|
|
||||||
|
if output.Location().String() != tz {
|
||||||
|
t.Fatalf(`Unexpected timezone, got %q instead of %s`, output.Location(), tz)
|
||||||
|
}
|
||||||
|
|
||||||
|
if year := output.Year(); year != 0 {
|
||||||
|
t.Fatalf(`Unexpected year, got %d instead of 0`, year)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue