diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-11-01 15:20:57 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-11-01 15:45:59 +0100 |
commit | ed6fd26ce884c49b02497728a99e90b92dd65f1f (patch) | |
tree | 58b5a17ad4eaeb3a283801a4dc26aa8c250a880d /common | |
parent | 7fa66425aa0a918b4bf5eb9a21f6e567e0a7e876 (diff) |
common/htime: Fix time.Format with Go layouts
Fixes #9107
Diffstat (limited to 'common')
-rw-r--r-- | common/htime/time.go | 8 | ||||
-rw-r--r-- | 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 5071b0858..9ab249d38 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 dc351ffdd..6cb323409 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) { |