From ed6fd26ce884c49b02497728a99e90b92dd65f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 1 Nov 2021 15:20:57 +0100 Subject: [PATCH] common/htime: Fix time.Format with Go layouts Fixes #9107 --- common/htime/time.go | 8 ++++++-- common/htime/time_test.go | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/common/htime/time.go b/common/htime/time.go index 5071b08585b..9ab249d3835 100644 --- a/common/htime/time.go +++ b/common/htime/time.go @@ -123,9 +123,13 @@ func (f TimeFormatter) Format(t time.Time, layout string) string { dayIdx := t.Weekday() s = strings.ReplaceAll(s, longMonthNames[monthIdx], f.ltr.MonthWide(t.Month())) - s = strings.ReplaceAll(s, shortMonthNames[monthIdx], f.ltr.MonthAbbreviated(t.Month())) + if !strings.Contains(s, f.ltr.MonthWide(t.Month())) { + s = strings.ReplaceAll(s, shortMonthNames[monthIdx], f.ltr.MonthAbbreviated(t.Month())) + } s = strings.ReplaceAll(s, longDayNames[dayIdx], f.ltr.WeekdayWide(t.Weekday())) - s = strings.ReplaceAll(s, shortDayNames[dayIdx], f.ltr.WeekdayAbbreviated(t.Weekday())) + if !strings.Contains(s, f.ltr.WeekdayWide(t.Weekday())) { + s = strings.ReplaceAll(s, shortDayNames[dayIdx], f.ltr.WeekdayAbbreviated(t.Weekday())) + } return s } diff --git a/common/htime/time_test.go b/common/htime/time_test.go index dc351ffddaf..6cb32340966 100644 --- a/common/htime/time_test.go +++ b/common/htime/time_test.go @@ -17,8 +17,8 @@ import ( "testing" "time" - translators "github.com/gohugoio/localescompressed" qt "github.com/frankban/quicktest" + translators "github.com/gohugoio/localescompressed" ) func TestTimeFormatter(t *testing.T) { @@ -27,6 +27,12 @@ func TestTimeFormatter(t *testing.T) { june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06") june06 = june06.Add(7777 * time.Second) + jan06, _ := time.Parse("2006-Jan-02", "2018-Jan-06") + jan06 = jan06.Add(32 * time.Second) + + mondayNovemberFirst, _ := time.Parse("2006-Jan-02", "2021-11-01") + mondayNovemberFirst = mondayNovemberFirst.Add(33 * time.Second) + c.Run("Norsk nynorsk", func(c *qt.C) { f := NewTimeFormatter(translators.GetTranslator("nn")) @@ -73,6 +79,37 @@ func TestTimeFormatter(t *testing.T) { c.Assert(f.Format(june06, "Mon Mon"), qt.Equals, "Wed Wed") }) + c.Run("Weekdays German", func(c *qt.C) { + tr := translators.GetTranslator("de") + f := NewTimeFormatter(tr) + + // Issue #9107 + for i, weekDayWideGerman := range []string{"Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"} { + date := mondayNovemberFirst.Add(time.Duration(i*24) * time.Hour) + c.Assert(tr.WeekdayWide(date.Weekday()), qt.Equals, weekDayWideGerman) + c.Assert(f.Format(date, "Monday"), qt.Equals, weekDayWideGerman) + } + + for i, weekDayAbbreviatedGerman := range []string{"Mo.", "Di.", "Mi.", "Do.", "Fr.", "Sa.", "So."} { + date := mondayNovemberFirst.Add(time.Duration(i*24) * time.Hour) + c.Assert(tr.WeekdayAbbreviated(date.Weekday()), qt.Equals, weekDayAbbreviatedGerman) + c.Assert(f.Format(date, "Mon"), qt.Equals, weekDayAbbreviatedGerman) + } + }) + + c.Run("Months German", func(c *qt.C) { + tr := translators.GetTranslator("de") + f := NewTimeFormatter(tr) + + // Issue #9107 + for i, monthWideNorway := range []string{"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli"} { + date := jan06.Add(time.Duration(i*24*31) * time.Hour) + c.Assert(tr.MonthWide(date.Month()), qt.Equals, monthWideNorway) + c.Assert(f.Format(date, "January"), qt.Equals, monthWideNorway) + } + + }) + } func BenchmarkTimeFormatter(b *testing.B) {