这是indexloc提供的服务,不要输入任何密码
Skip to content
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
11 changes: 10 additions & 1 deletion mail/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package mail

import (
"net/mail"
"regexp"
"time"

"github.com/emersion/go-message"
)

const dateLayout = "Mon, 02 Jan 2006 15:04:05 -0700"

// TODO: this is a blunt way to strip any trailing CFWS (comment). A sharper
// one would strip multiple CFWS, and only if really valid according to
// RFC5322.
var commentRE = regexp.MustCompile(`[ \t]+\(.*\)$`)

// A Header is a mail header.
type Header struct {
message.Header
Expand All @@ -31,7 +37,10 @@ func (h *Header) SetAddressList(key string, addrs []*Address) {

// Date parses the Date header field.
func (h *Header) Date() (time.Time, error) {
return mail.ParseDate(h.Get("Date"))
//TODO: remove this once https://go-review.googlesource.com/c/go/+/117596/
// is merged
date := commentRE.ReplaceAllString(h.Get("Date"), "")
return mail.ParseDate(date)
}

// SetDate formats the Date header field.
Expand Down
17 changes: 17 additions & 0 deletions mail/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,20 @@ func TestHeader(t *testing.T) {
t.Errorf("Expected header subject to be %v, but got %v", subject, got)
}
}

func TestCFWSDates(t *testing.T) {
tc := []string{
"Mon, 22 Jul 2019 13:57:29 -0500 (GMT-05:00)",
"Mon, 22 Jul 2019 13:57:29 -0500",
"Mon, 2 Jan 06 15:04:05 MST (Some random stuff)",
"Mon, 2 Jan 06 15:04:05 MST",
}
var h mail.Header
for _, tt := range tc {
h.Set("Date", tt)
_, err := h.Date()
if err != nil {
t.Errorf("Failed to parse time %q: %v", tt, err)
}
}
}