From: 1911860538 Date: Sat, 8 Mar 2025 07:47:55 +0000 (+0000) Subject: net/mail: use sync.OnceValue to build dateLayouts X-Git-Tag: go1.25rc1~765 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=39b783780a471961ae381a9fc37a5a1e468f5c21;p=gostls13.git net/mail: use sync.OnceValue to build dateLayouts Simplify buildDateLayouts with sync.OnceValue. Change-Id: Ib48ab20ee00f5e44cc1b0f6e1afe3fcd1b7dc3c7 GitHub-Last-Rev: 0866d463de1ec618d0d645b98f5e94917b8c3bde GitHub-Pull-Request: golang/go#72743 Reviewed-on: https://go-review.googlesource.com/c/go/+/656055 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Auto-Submit: Ian Lance Taylor Reviewed-by: Ian Lance Taylor --- diff --git a/src/net/mail/message.go b/src/net/mail/message.go index 21b075e78a..14f839a030 100644 --- a/src/net/mail/message.go +++ b/src/net/mail/message.go @@ -115,12 +115,7 @@ func readHeader(r *textproto.Reader) (map[string][]string, error) { // Layouts suitable for passing to time.Parse. // These are tried in order. -var ( - dateLayoutsBuildOnce sync.Once - dateLayouts []string -) - -func buildDateLayouts() { +var dateLayouts = sync.OnceValue(func() []string { // Generate layouts based on RFC 5322, section 3.3. dows := [...]string{"", "Mon, "} // day-of-week @@ -130,23 +125,27 @@ func buildDateLayouts() { // "-0700 (MST)" is not in RFC 5322, but is common. zones := [...]string{"-0700", "MST", "UT"} // zone = (("+" / "-") 4DIGIT) / "UT" / "GMT" / ... + total := len(dows) * len(days) * len(years) * len(seconds) * len(zones) + layouts := make([]string, 0, total) + for _, dow := range dows { for _, day := range days { for _, year := range years { for _, second := range seconds { for _, zone := range zones { s := dow + day + " Jan " + year + " 15:04" + second + " " + zone - dateLayouts = append(dateLayouts, s) + layouts = append(layouts, s) } } } } } -} + + return layouts +}) // ParseDate parses an RFC 5322 date string. func ParseDate(date string) (time.Time, error) { - dateLayoutsBuildOnce.Do(buildDateLayouts) // CR and LF must match and are tolerated anywhere in the date field. date = strings.ReplaceAll(date, "\r\n", "") if strings.Contains(date, "\r") { @@ -184,7 +183,7 @@ func ParseDate(date string) (time.Time, error) { if !p.skipCFWS() { return time.Time{}, errors.New("mail: misformatted parenthetical comment") } - for _, layout := range dateLayouts { + for _, layout := range dateLayouts() { t, err := time.Parse(layout, date) if err == nil { return t, nil