From 2ca387850f340381cb20b3b2cc3dea48e387c2c5 Mon Sep 17 00:00:00 2001 From: Valentyn Date: Tue, 5 May 2020 23:28:16 +0300 Subject: [PATCH] fixed error of array length in 3 plces and fixed i18n test (#184) nice work @Chubik . cleaner to boot. Thank you. --- server/i18n_test.go | 4 +- server/occurrence.go | 6 +++ server/parse.go | 99 +++++++++++++++++++++++--------------------- 3 files changed, 60 insertions(+), 49 deletions(-) diff --git a/server/i18n_test.go b/server/i18n_test.go index 2c40eda..d7e8c69 100644 --- a/server/i18n_test.go +++ b/server/i18n_test.go @@ -50,7 +50,9 @@ func TestTranslationsPreInit(t *testing.T) { p.API = api err = p.TranslationsPreInit() require.True(t, err.Error() == "unable to read i18n directory: readdirent: invalid argument" || - err.Error() == "unable to read i18n directory: readdirent: not a directory") + err.Error() == "unable to read i18n directory: readdirent: not a directory" || + err.Error() == "unable to read i18n directory: fdopendir: not a directory") + }) t.Run("no i18n files", func(t *testing.T) { diff --git a/server/occurrence.go b/server/occurrence.go index adb27f6..bdb3636 100755 --- a/server/occurrence.go +++ b/server/occurrence.go @@ -886,6 +886,9 @@ func (p *Plugin) onEN(when string, user *model.User) (times []time.Time, err err dateSplit := p.regSplit(dateUnit, "T|Z") dateSplit = p.regSplit(dateSplit[0], "-") timeSplit := p.regSplit(timeUnit, ":") + if len(dateSplit) < 3 || len(timeSplit) < 3 { + return []time.Time{}, errors.New("error date/time format") + } year, _ := strconv.Atoi(dateSplit[0]) month, _ := strconv.Atoi(dateSplit[1]) day, _ := strconv.Atoi(dateSplit[2]) @@ -1059,6 +1062,9 @@ func (p *Plugin) everyEN(when string, user *model.User) (times []time.Time, err dateSplit := p.regSplit(dateUnit, "T|Z") dateSplit = p.regSplit(dateSplit[0], "-") timeSplit := p.regSplit(timeUnit, ":") + if len(dateSplit) < 3 || len(timeSplit) < 3 { + return []time.Time{}, errors.New("error date/time format") + } year, _ := strconv.Atoi(dateSplit[0]) month, _ := strconv.Atoi(dateSplit[1]) day, _ := strconv.Atoi(dateSplit[2]) diff --git a/server/parse.go b/server/parse.go index ba55d13..5f54249 100755 --- a/server/parse.go +++ b/server/parse.go @@ -603,54 +603,14 @@ func (p *Plugin) normalizeDate(text string, user *model.User) (string, error) { return "", errors.New("unrecognized date format") } - switch parts[0] { - case T("jan"), - T("january"): - parts[0] = "01" - break - case T("feb"), - T("february"): - parts[0] = "02" - break - case T("mar"), - T("march"): - parts[0] = "03" - break - case T("apr"), - T("april"): - parts[0] = "04" - break - case T("may"): - parts[0] = "05" - break - case T("june"): - parts[0] = "06" - break - case T("july"): - parts[0] = "07" - break - case T("aug"), - T("august"): - parts[0] = "08" - break - case T("sept"), - T("september"): - parts[0] = "09" - break - case T("oct"), - T("october"): - parts[0] = "10" - break - case T("nov"), - T("november"): - parts[0] = "11" - break - case T("dec"), - T("december"): - parts[0] = "12" - break - default: - return "", errors.New("month not found") + var err error + parts[0], err = p.monthNumber(parts[0], user) + if err != nil { + return "", err + } + + if len(parts) < 3 { + return "", errors.New("unrecognized date format") } mon, mErr := strconv.Atoi(parts[0]) @@ -903,6 +863,49 @@ func (p *Plugin) daySuffix(user *model.User, day string) string { return day } +func (p *Plugin) monthNumber(month string, user *model.User) (string, error) { + + T, _ := p.translation(user) + + switch month { + case T("jan"), + T("january"): + return "01", nil + case T("feb"), + T("february"): + return "02", nil + case T("mar"), + T("march"): + return "03", nil + case T("apr"), + T("april"): + return "04", nil + case T("may"): + return "05", nil + case T("june"): + return "06", nil + case T("july"): + return "07", nil + case T("aug"), + T("august"): + return "08", nil + case T("sept"), + T("september"): + return "09", nil + case T("oct"), + T("october"): + return "10", nil + case T("nov"), + T("november"): + return "11", nil + case T("dec"), + T("december"): + return "12", nil + default: + return "", errors.New("month not found") + } +} + func (p *Plugin) weekDayNumber(day string, user *model.User) int { T, _ := p.translation(user)