common/htime: Fix time.Format with Go layouts

Fixes #9107
This commit is contained in:
Bjørn Erik Pedersen 2021-11-01 15:20:57 +01:00
parent 7fa66425aa
commit ed6fd26ce8
2 changed files with 44 additions and 3 deletions

View file

@ -123,9 +123,13 @@ func (f TimeFormatter) Format(t time.Time, layout string) string {
dayIdx := t.Weekday() dayIdx := t.Weekday()
s = strings.ReplaceAll(s, longMonthNames[monthIdx], f.ltr.MonthWide(t.Month())) 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, 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 return s
} }

View file

@ -17,8 +17,8 @@ import (
"testing" "testing"
"time" "time"
translators "github.com/gohugoio/localescompressed"
qt "github.com/frankban/quicktest" qt "github.com/frankban/quicktest"
translators "github.com/gohugoio/localescompressed"
) )
func TestTimeFormatter(t *testing.T) { func TestTimeFormatter(t *testing.T) {
@ -27,6 +27,12 @@ func TestTimeFormatter(t *testing.T) {
june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06") june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06")
june06 = june06.Add(7777 * time.Second) 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) { c.Run("Norsk nynorsk", func(c *qt.C) {
f := NewTimeFormatter(translators.GetTranslator("nn")) 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.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) { func BenchmarkTimeFormatter(b *testing.B) {