Skip to content

Commit 5771bab

Browse files
authored
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
1 parent 03846fc commit 5771bab

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

lunes.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,16 @@ var longMonthNamesStd = []string{
7575
"December",
7676
}
7777

78-
var dayPeriodsStd = []string{
78+
var dayPeriodsStdUpper = []string{
7979
"AM",
8080
"PM",
8181
}
8282

83+
var dayPeriodsStdLower = []string{
84+
"am",
85+
"pm",
86+
}
87+
8388
// Parse parses a formatted string in foreign language and returns the [time.Time] value
8489
// it represents. See the documentation for the constant called [time.Layout] to see how to
8590
// represent the format.
@@ -239,13 +244,23 @@ func TranslateWithLocale(layout string, value string, locale Locale) (string, er
239244
}
240245
case 'P', 'p': // PM, pm
241246
if len(layout) >= layoutOffset+2 && unicode.ToUpper(rune(layout[layoutOffset+1])) == 'M' {
247+
var layoutElem string
248+
// day-periods case matters for the time package parsing functions
249+
if c == 'p' {
250+
layoutElem = "pm"
251+
stdTab = dayPeriodsStdLower
252+
} else {
253+
layoutElem = "PM"
254+
stdTab = dayPeriodsStdUpper
255+
}
256+
242257
lookupTab = locale.DayPeriods()
243258
if len(lookupTab) == 0 {
244-
return "", newUnsupportedLayoutElemError("PM", locale)
259+
return "", newUnsupportedLayoutElemError(layoutElem, locale)
245260
}
246261

247262
layoutOffset += 2
248-
valueOffset, err = writeLayoutValue("PM", lookupTab, dayPeriodsStd, valueOffset, value, &sb)
263+
valueOffset, err = writeLayoutValue(layoutElem, lookupTab, stdTab, valueOffset, value, &sb)
249264
if err != nil {
250265
return "", err
251266
}

lunes_test.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,56 @@ func TestParsingWithUnsupportedLocale(t *testing.T) {
818818
})
819819
}
820820

821+
func TestDayPeriodsLayoutCase(t *testing.T) {
822+
tests := []struct {
823+
name string
824+
format string
825+
value string
826+
lang string
827+
}{
828+
{
829+
name: "AllLowerPm",
830+
format: "Monday January 03:04:05pm",
831+
value: "lunes enero 03:04:05a.m.",
832+
lang: LocaleEs,
833+
},
834+
{
835+
name: "AllUpperPm",
836+
format: "Monday January 03:04:05PM",
837+
value: "Monday January 03:04:05AM",
838+
lang: LocaleEnUS,
839+
},
840+
{
841+
name: "UpperPmLowerValue",
842+
format: "Monday January 03:04:05PM",
843+
value: "Monday January 03:04:05am",
844+
lang: LocaleEnUS,
845+
},
846+
{
847+
name: "LowerPmUpperValue",
848+
format: "Monday January 03:04:05pm",
849+
value: "Monday January 03:04:05AM",
850+
lang: LocaleEnUS,
851+
},
852+
}
853+
854+
for _, test := range tests {
855+
t.Run(fmt.Sprintf("ParseWith%s", test.name), func(t *testing.T) {
856+
_, err := Parse(test.format, test.value, test.lang)
857+
if err != nil {
858+
t.Errorf("no error expected, got: '%v'", err)
859+
}
860+
})
861+
862+
t.Run(fmt.Sprintf("ParseInLocationWith%s", test.name), func(t *testing.T) {
863+
_, err := ParseInLocation(test.format, test.value, test.lang, defaultLocation)
864+
if err != nil {
865+
t.Errorf("no error expected, got: '%v'", err)
866+
}
867+
})
868+
}
869+
}
870+
821871
func TestAllLocalesReplacements(t *testing.T) {
822872
var shortLayoutTests []ParseTest
823873
var longLayoutTests []ParseTest
@@ -826,7 +876,7 @@ func TestAllLocalesReplacements(t *testing.T) {
826876
day := month % 7
827877
period := month % 2
828878

829-
shortStdValue := fmt.Sprintf("%s %s 03:04:05%s", shortDayNamesStd[day], shortMonthNamesStd[month], dayPeriodsStd[period])
879+
shortStdValue := fmt.Sprintf("%s %s 03:04:05%s", shortDayNamesStd[day], shortMonthNamesStd[month], dayPeriodsStdUpper[period])
830880
shortLayoutTests = append(shortLayoutTests, ParseTest{
831881
name: shortStdValue,
832882
format: "Mon Jan 03:04:05PM",
@@ -836,7 +886,7 @@ func TestAllLocalesReplacements(t *testing.T) {
836886
locales: allLocalesTests("%s %s 03:04:05%s", []replacement{{shortDayNamesField, day}, {shortMonthNamesField, month}, {dayPeriodsField, period}}),
837887
})
838888

839-
longStdValue := fmt.Sprintf("%s %s 03:04:05%s", longDayNamesStd[day], longMonthNamesStd[month], dayPeriodsStd[period])
889+
longStdValue := fmt.Sprintf("%s %s 03:04:05%s", longDayNamesStd[day], longMonthNamesStd[month], dayPeriodsStdUpper[period])
840890
longLayoutTests = append(longLayoutTests, ParseTest{
841891
format: "Monday January 03:04:05PM",
842892
stdValue: longStdValue,

tables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestLocaleTableEn(t *testing.T) {
3232
longDayNamesStd,
3333
shortMonthNamesStd,
3434
longMonthNamesStd,
35-
dayPeriodsStd,
35+
dayPeriodsStdUpper,
3636
}
3737

3838
lang := "en"

0 commit comments

Comments
 (0)