From 5771bab4b7937fbff947b320cc373c76829127dd Mon Sep 17 00:00:00 2001 From: Edmo Vamerlatti Costa <11836452+edmocosta@users.noreply.github.com> Date: Fri, 26 Jul 2024 11:45:02 +0200 Subject: [PATCH] Change periods of the day replacements to be case-sensitive (#7) Changed to replace the layout day periods elements respecting the define layout letter case --- lunes.go | 21 +++++++++++++++++--- lunes_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++-- tables_test.go | 2 +- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/lunes.go b/lunes.go index 1270fa1..8074b2a 100644 --- a/lunes.go +++ b/lunes.go @@ -75,11 +75,16 @@ var longMonthNamesStd = []string{ "December", } -var dayPeriodsStd = []string{ +var dayPeriodsStdUpper = []string{ "AM", "PM", } +var dayPeriodsStdLower = []string{ + "am", + "pm", +} + // Parse parses a formatted string in foreign language and returns the [time.Time] value // it represents. See the documentation for the constant called [time.Layout] to see how to // represent the format. @@ -239,13 +244,23 @@ func TranslateWithLocale(layout string, value string, locale Locale) (string, er } case 'P', 'p': // PM, pm if len(layout) >= layoutOffset+2 && unicode.ToUpper(rune(layout[layoutOffset+1])) == 'M' { + var layoutElem string + // day-periods case matters for the time package parsing functions + if c == 'p' { + layoutElem = "pm" + stdTab = dayPeriodsStdLower + } else { + layoutElem = "PM" + stdTab = dayPeriodsStdUpper + } + lookupTab = locale.DayPeriods() if len(lookupTab) == 0 { - return "", newUnsupportedLayoutElemError("PM", locale) + return "", newUnsupportedLayoutElemError(layoutElem, locale) } layoutOffset += 2 - valueOffset, err = writeLayoutValue("PM", lookupTab, dayPeriodsStd, valueOffset, value, &sb) + valueOffset, err = writeLayoutValue(layoutElem, lookupTab, stdTab, valueOffset, value, &sb) if err != nil { return "", err } diff --git a/lunes_test.go b/lunes_test.go index 9c1cce9..b073894 100644 --- a/lunes_test.go +++ b/lunes_test.go @@ -818,6 +818,56 @@ func TestParsingWithUnsupportedLocale(t *testing.T) { }) } +func TestDayPeriodsLayoutCase(t *testing.T) { + tests := []struct { + name string + format string + value string + lang string + }{ + { + name: "AllLowerPm", + format: "Monday January 03:04:05pm", + value: "lunes enero 03:04:05a.m.", + lang: LocaleEs, + }, + { + name: "AllUpperPm", + format: "Monday January 03:04:05PM", + value: "Monday January 03:04:05AM", + lang: LocaleEnUS, + }, + { + name: "UpperPmLowerValue", + format: "Monday January 03:04:05PM", + value: "Monday January 03:04:05am", + lang: LocaleEnUS, + }, + { + name: "LowerPmUpperValue", + format: "Monday January 03:04:05pm", + value: "Monday January 03:04:05AM", + lang: LocaleEnUS, + }, + } + + for _, test := range tests { + t.Run(fmt.Sprintf("ParseWith%s", test.name), func(t *testing.T) { + _, err := Parse(test.format, test.value, test.lang) + if err != nil { + t.Errorf("no error expected, got: '%v'", err) + } + }) + + t.Run(fmt.Sprintf("ParseInLocationWith%s", test.name), func(t *testing.T) { + _, err := ParseInLocation(test.format, test.value, test.lang, defaultLocation) + if err != nil { + t.Errorf("no error expected, got: '%v'", err) + } + }) + } +} + func TestAllLocalesReplacements(t *testing.T) { var shortLayoutTests []ParseTest var longLayoutTests []ParseTest @@ -826,7 +876,7 @@ func TestAllLocalesReplacements(t *testing.T) { day := month % 7 period := month % 2 - shortStdValue := fmt.Sprintf("%s %s 03:04:05%s", shortDayNamesStd[day], shortMonthNamesStd[month], dayPeriodsStd[period]) + shortStdValue := fmt.Sprintf("%s %s 03:04:05%s", shortDayNamesStd[day], shortMonthNamesStd[month], dayPeriodsStdUpper[period]) shortLayoutTests = append(shortLayoutTests, ParseTest{ name: shortStdValue, format: "Mon Jan 03:04:05PM", @@ -836,7 +886,7 @@ func TestAllLocalesReplacements(t *testing.T) { locales: allLocalesTests("%s %s 03:04:05%s", []replacement{{shortDayNamesField, day}, {shortMonthNamesField, month}, {dayPeriodsField, period}}), }) - longStdValue := fmt.Sprintf("%s %s 03:04:05%s", longDayNamesStd[day], longMonthNamesStd[month], dayPeriodsStd[period]) + longStdValue := fmt.Sprintf("%s %s 03:04:05%s", longDayNamesStd[day], longMonthNamesStd[month], dayPeriodsStdUpper[period]) longLayoutTests = append(longLayoutTests, ParseTest{ format: "Monday January 03:04:05PM", stdValue: longStdValue, diff --git a/tables_test.go b/tables_test.go index 01ec712..a71ec8f 100644 --- a/tables_test.go +++ b/tables_test.go @@ -32,7 +32,7 @@ func TestLocaleTableEn(t *testing.T) { longDayNamesStd, shortMonthNamesStd, longMonthNamesStd, - dayPeriodsStd, + dayPeriodsStdUpper, } lang := "en"