Skip to content

Commit

Permalink
Change periods of the day replacements to be case-sensitive (#7)
Browse files Browse the repository at this point in the history
Changed to replace the layout day periods elements respecting the define layout letter case
  • Loading branch information
edmocosta authored Jul 26, 2024
1 parent 03846fc commit 5771bab
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
21 changes: 18 additions & 3 deletions lunes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
54 changes: 52 additions & 2 deletions lunes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestLocaleTableEn(t *testing.T) {
longDayNamesStd,
shortMonthNamesStd,
longMonthNamesStd,
dayPeriodsStd,
dayPeriodsStdUpper,
}

lang := "en"
Expand Down

0 comments on commit 5771bab

Please sign in to comment.