Skip to content

Commit

Permalink
Merge pull request #207 from jakopako:jakopako/issue206
Browse files Browse the repository at this point in the history
Add dutch to date auto format extraction + other fix
  • Loading branch information
jakopako authored Mar 23, 2023
2 parents cb9803d + 9cf57a0 commit 03beec3
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 15 deletions.
53 changes: 38 additions & 15 deletions date/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func GetDateFormat(date string, parts CoveredDateParts) (string, string) {
sepTokens = append(sepTokens, "")
}

language := ""
potLangs := [][]string{}
formatTokens := []string{}
for i, token := range tokens {
if token == "" {
Expand All @@ -109,19 +109,15 @@ func GetDateFormat(date string, parts CoveredDateParts) (string, string) {
if parts.Month {
if m, l, err := getFormatAndLangMonthLetters(token); err == nil {
formatTokens = append(formatTokens, m)
if language == "" {
language = l
}
potLangs = append(potLangs, l)
parts.Month = false // so that we know that we had month already
continue
}
}
if parts.Day {
if d, l, err := getFormatAndLangDayLetters(token); err == nil {
formatTokens = append(formatTokens, d)
if language == "" {
language = l
}
potLangs = append(potLangs, l)
// in contrast to month we don't do this with day because it happens
// that day occurs as number _and_ as word in a single date
// parts.Day = false
Expand Down Expand Up @@ -167,45 +163,72 @@ func GetDateFormat(date string, parts CoveredDateParts) (string, string) {
finalFormat += sepTokens[i]
}

// finding the correct language
language := ""
if len(potLangs) > 1 {
intersection := potLangs[0]
for i := 1; i < len(potLangs) && len(intersection) > 0; i++ {
intersection = utils.IntersectionSlices(intersection, potLangs[i])
}
if len(intersection) > 0 {
language = intersection[0]
}
} else if len(potLangs) > 0 {
language = potLangs[0][0]
}
return finalFormat, language
}

func getFormatAndLangMonthLetters(month string) (string, string, error) {
func getFormatAndLangMonthLetters(month string) (string, []string, error) {
potLangs := []string{}
monthTmp := strings.ToLower(month)
for _, m := range longMonthNames {
for n := range m.namesMap {
if monthTmp == strings.ToLower(n) {
return "January", m.lang, nil
potLangs = append(potLangs, m.lang)
}
}
}
if len(potLangs) > 0 {
return "January", potLangs, nil
}
for _, m := range shortMonthNames {
for n := range m.namesMap {
if monthTmp == strings.ToLower(n) {
return "Jan", m.lang, nil
potLangs = append(potLangs, m.lang)
}
}
}
return "", "", fmt.Errorf("%s is not a month", month)
if len(potLangs) > 0 {
return "Jan", potLangs, nil
}
return "", potLangs, fmt.Errorf("%s is not a month", month)
}

func getFormatAndLangDayLetters(day string) (string, string, error) {
func getFormatAndLangDayLetters(day string) (string, []string, error) {
potLangs := []string{} // there might be multiple matches for a certain day string
dayTmp := strings.ToLower(day)
for _, m := range longDayNames {
for n := range m.namesMap {
if dayTmp == strings.ToLower(n) {
return "Monday", m.lang, nil
potLangs = append(potLangs, m.lang)
}
}
}
if len(potLangs) > 0 {
return "Monday", potLangs, nil
}
for _, m := range shortDayNames {
for n := range m.namesMap {
if dayTmp == strings.ToLower(n) {
return "Mon", m.lang, nil
potLangs = append(potLangs, m.lang)
}
}
}
return "", "", fmt.Errorf("%s is not a day", day)
if len(potLangs) > 0 {
return "Mon", potLangs, nil
}
return "", potLangs, fmt.Errorf("%s is not a day", day)
}

func isDayNumber(number string) bool {
Expand Down
32 changes: 32 additions & 0 deletions date/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,35 @@ func TestGetDateFormat12(t *testing.T) {
}
}
}

func TestGetDateFormat13(t *testing.T) {
dateFormats := []formatTestStruct{
{
input: "do 23 maart 2023",
coveredParts: CoveredDateParts{Day: true, Month: true, Year: true},
formatString: "Mon 2 January 2006",
language: "nl_BE",
},
{
input: "wo 5 april 2023",
coveredParts: CoveredDateParts{Day: true, Month: true, Year: true},
formatString: "Mon 2 January 2006",
language: "nl_BE",
},
{
input: "za 22 april 2023",
coveredParts: CoveredDateParts{Day: true, Month: true, Year: true},
formatString: "Mon 2 January 2006",
language: "nl_BE",
},
}
for _, df := range dateFormats {
f, l := GetDateFormat(df.input, df.coveredParts)
if f != df.formatString {
log.Fatalf("expected %s but got %s", df.formatString, f)
}
if l != df.language {
log.Fatalf("expected date language %s but got %s", df.language, l)
}
}
}
28 changes: 28 additions & 0 deletions date/day_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var longDayNames = []langMap{
lang: "fr_FR",
namesMap: longDayNamesFrFR,
},
{
lang: "nl_BE",
namesMap: longDayNamesNlBE,
},
}

var shortDayNames = []langMap{
Expand All @@ -28,6 +32,10 @@ var shortDayNames = []langMap{
lang: "fr_FR",
namesMap: shortDayNamesFrFR,
},
{
lang: "nl_BE",
namesMap: shortDayNamesNlBE,
},
}

var shortDayNamesEnUS = map[string]bool{
Expand Down Expand Up @@ -89,3 +97,23 @@ var longDayNamesFrFR = map[string]bool{
"vendredi": true,
"samedi": true,
}

var shortDayNamesNlBE = map[string]bool{
"zo": true,
"ma": true,
"di": true,
"wo": true,
"do": true,
"vr": true,
"za": true,
}

var longDayNamesNlBE = map[string]bool{
"zondag": true,
"maandag": true,
"dinsdag": true,
"woensdag": true,
"donderdag": true,
"vrijdag": true,
"zaterdag": true,
}
38 changes: 38 additions & 0 deletions date/month_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var longMonthNames = []langMap{
lang: "fr_FR",
namesMap: longMonthNamesFrFR,
},
{
lang: "nl_BE",
namesMap: longMonthNamesNlBE,
},
}

var shortMonthNames = []langMap{
Expand All @@ -33,6 +37,10 @@ var shortMonthNames = []langMap{
lang: "fr_FR",
namesMap: shortMonthNamesFrFR,
},
{
lang: "nl_BE",
namesMap: shortMonthNamesNlBE,
},
}

var shortMonthNamesEnUS = map[string]bool{
Expand Down Expand Up @@ -124,3 +132,33 @@ var longMonthNamesFrFR = map[string]bool{
"novembre": true,
"décembre": true,
}

var shortMonthNamesNlBE = map[string]bool{
"jan": true,
"feb": true,
"mrt": true,
"apr": true,
"mei": true,
"jun": true,
"jul": true,
"aug": true,
"sep": true,
"okt": true,
"nov": true,
"dec": true,
}

var longMonthNamesNlBE = map[string]bool{
"januari": true,
"februari": true,
"maart": true,
"april": true,
"mei": true,
"juni": true,
"juli": true,
"augustus": true,
"september": true,
"oktober": true,
"november": true,
"december": true,
}

0 comments on commit 03beec3

Please sign in to comment.