From 329f5957cb50cf9d53835474e83acbc191f8e652 Mon Sep 17 00:00:00 2001 From: Sterling Hanenkamp Date: Mon, 30 Jan 2023 22:28:47 -0600 Subject: [PATCH] handle Mon Jan 02 15:04:05 2006 MST dateparse.ParseAny can't handle that one for some reason --- Changes.md | 4 ++++ message/header/header.go | 28 ++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Changes.md b/Changes.md index 77b2c6f..5c49ab9 100644 --- a/Changes.md +++ b/Changes.md @@ -1,3 +1,7 @@ +WIP TBD + + * Bugfix: Handle another strange date I have come across in my sample data. + v2.3.0 2023-01-30 * On `header.ErrManyFields` failure, `(*header.Header).Get()` now returns the value of the first field found with the error. diff --git a/message/header/header.go b/message/header/header.go index ee23e6d..425ed30 100644 --- a/message/header/header.go +++ b/message/header/header.go @@ -2,6 +2,7 @@ package header import ( "errors" + "fmt" "net/mail" "strings" "time" @@ -54,6 +55,13 @@ const ( To = "To" ) +// Even more custom date formats, built from those seen in the wild that the +// usual parsers have trouble with. +const ( + // UnixDateWithEarlyYear is a weird one, eh? + UnixDateWithEarlyYear = "Mon Jan 02 15:04:05 2006 MST" +) + // Header wraps a Base, which does the actual storage and low-level field // manipulation. This provides several methods to make reading and manipulating // the header more convenient and some caching for complex values parsed from @@ -139,13 +147,21 @@ func (h *Header) Get(name string) (string, error) { // It either returns a parsed time or the parse error. func ParseTime(body string) (time.Time, error) { t, err := mail.ParseDate(body) - if err != nil { - t, err = dateparse.ParseAny(body) - if err != nil { - return t, err - } + if err == nil { + return t, nil } - return t, nil + + t, err = dateparse.ParseAny(body) + if err == nil { + return t, nil + } + + t, err = time.Parse(UnixDateWithEarlyYear, body) + if err == nil { + return t, nil + } + + return t, fmt.Errorf("time string %q cannot be parsed", body) } // getTime parses the header body as a date and caches the result.