Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: parse broken rfc1123 format #152

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions nedlibreader/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func parseTime(dateString string) (t time.Time, err error) {
if err == nil {
return
}
t, err = parseRFC1123BrokenYear(dateString)
if err == nil {
return
}
return time.Time{}, fmt.Errorf("failed to parse string as time.Time: '%s'", dateString)
}

Expand Down Expand Up @@ -102,6 +106,12 @@ func parseRFC850BrokenYear(value string) (time.Time, error) {
return time.Parse(time.RFC850, value)
}

func parseRFC1123BrokenYear(value string) (time.Time, error) {
// Replace 31 Jul 103 With 31 Jul 2003
value = regexp.MustCompile(`(\d{2} [A-Za-z]{3}) 1(\d{2})`).ReplaceAllString(value, "${1} 20${2}")
return time.Parse(time.RFC1123, value)
}

// parse date and time string on the format: "lø, 19 jul 2003 04:45:41 CET" to a time.Time
func parseCustomNorwegianTime(dateString string) (t time.Time, err error) {
days := map[string]time.Weekday{
Expand Down
6 changes: 6 additions & 0 deletions nedlibreader/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
RFC1123_NO_LEADING_ZERO = "RFC1123_NO_LEADING_ZERO"
RFC1123_SECONDS_OUT_OF_RANGE = "RFC1123_SECONDS_OUT_OF_RANGE"
RFC1123_NORSK = "RFC1123_NORSK"
RFC1123_BROKEN_YEAR = "RFC1123_BROKEN_YEAR"
RFC822 = "RFC822"
RFC822Z = "RFC822Z"
RFC850 = "RFC850"
Expand All @@ -38,6 +39,7 @@ var timeParseFuncs = map[string]func(string) (time.Time, error){
RFC1123Z: func(value string) (time.Time, error) { return time.Parse(time.RFC1123Z, value) },
RFC1123_NORSK: parseCustomNorwegianTime,
RFC1123_SECONDS_OUT_OF_RANGE: parseRFC1123SecondsOutOfRange,
RFC1123_BROKEN_YEAR: parseRFC1123BrokenYear,
RFC850: func(value string) (time.Time, error) { return time.Parse(time.RFC850, value) },
RFC850_BROKEN_YEAR: parseRFC850BrokenYear,
RFC822: func(value string) (time.Time, error) { return time.Parse(time.RFC822, value) },
Expand Down Expand Up @@ -101,6 +103,10 @@ var timeTests = map[string][]struct {
{"Sun, 3 Aug 2003 03:34:00 GMT", time.Date(2003, time.August, 3, 3, 34, 0, 0, time.UTC)},
{"Sun, 3 Aug 2003 05:43:49 CES", time.Date(2003, time.August, 3, 7, 43, 49, 0, locCET)},
},
RFC1123_BROKEN_YEAR: {
{"Thu, 31 Jul 103 13:00:03 GMT", time.Date(2003, time.July, 31, 13, 0, 3, 0, time.UTC)},
{"Thu, 31 Jul 103 16:45:34 GMT", time.Date(2003, time.July, 31, 16, 45, 34, 0, time.UTC)},
},
RFC1123_SECONDS_OUT_OF_RANGE: {
{"Thu, 17 Jul 2003 12:15:60 GMT", time.Date(2003, time.July, 17, 12, 15, 60, 0, time.UTC)},
{"Fri, 10 Jan 2003 08:40:60 GMT", time.Date(2003, time.January, 10, 8, 40, 60, 0, time.UTC)},
Expand Down
Loading