Skip to content
Snippets Groups Projects
Commit 30d4b898 authored by Frédéric Guillot's avatar Frédéric Guillot
Browse files

Avoid "pq: time zone displacement out of range" errors

parent aadbd5ad
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ package date // import "miniflux.app/reader/date"
import (
"errors"
"fmt"
"math"
"strconv"
"strings"
"time"
......@@ -315,11 +316,13 @@ func Parse(rawInput string) (t time.Time, err error) {
switch layout {
case time.RFC822, time.RFC850, time.RFC1123:
if t, err = parseLocalTimeDates(layout, processedInput); err == nil {
t = checkTimezoneRange(t)
return
}
}
if t, err = time.Parse(layout, processedInput); err == nil {
t = checkTimezoneRange(t)
return
}
}
......@@ -347,3 +350,14 @@ func parseLocalTimeDates(layout, ds string) (t time.Time, err error) {
return time.ParseInLocation(layout, ds, loc)
}
// https://en.wikipedia.org/wiki/List_of_UTC_offsets
// Offset range: westernmost (−12:00) to the easternmost (+14:00)
// Avoid "pq: time zone displacement out of range" errors
func checkTimezoneRange(t time.Time) time.Time {
_, offset := t.Zone()
if math.Abs(float64(offset)) > 14*60*60 {
t = t.UTC()
}
return t
}
......@@ -187,3 +187,16 @@ func TestParseWeirdDateFormat(t *testing.T) {
}
}
}
func TestParseDateWithTimezoneOutOfRange(t *testing.T) {
date, err := Parse("2023-05-29 00:00:00-23:00")
if err != nil {
t.Errorf(`Unable to parse date: %v`, err)
}
_, offset := date.Zone()
if offset != 0 {
t.Errorf(`The offset should be reinitialized to 0 instead of %v because it's out of range`, offset)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment