From 2018282331e1cdd9460ed71afe969ff5c3f876c1 Mon Sep 17 00:00:00 2001 From: scott lee davis Date: Fri, 9 Aug 2019 17:20:44 -0700 Subject: [PATCH] handle ISO format dates. Issue #149 (#151) --- server/occurrence.go | 9 +++++++++ server/occurrence_test.go | 6 ++++++ server/parse.go | 25 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/server/occurrence.go b/server/occurrence.go index 7c75e08..b7c5780 100755 --- a/server/occurrence.go +++ b/server/occurrence.go @@ -796,6 +796,15 @@ func (p *Plugin) onEN(when string, user *model.User) (times []time.Time, err err chronoTime := "9:00AM" if len(dateTimeSplit) > 1 { chronoTime = dateTimeSplit[1] + } else { + timeTest := strings.Split(chronoDate, " ") + if len(timeTest) > 1 { + check := timeTest[len(timeTest)-1] + if strings.Contains(check, ":") { + chronoDate = strings.Join(timeTest[:len(timeTest)-1], " ") + chronoTime = check + } + } } dateUnit, ndErr := p.normalizeDate(chronoDate, user) diff --git a/server/occurrence_test.go b/server/occurrence_test.go index fc0d2ee..38cde22 100755 --- a/server/occurrence_test.go +++ b/server/occurrence_test.go @@ -496,6 +496,12 @@ func TestOn(t *testing.T) { assert.True(t, times[0].In(location).Month() == 1 && times[0].In(location).Day() == 1 && times[0].In(location).Hour() == 0) + times, err = p.onEN("on 2020-08-10 13:55", user) + assert.Nil(t, err) + assert.True(t, times[0].In(location).Year() == 2020 && + times[0].In(location).Month() == 8 && times[0].In(location).Day() == 10 && + times[0].In(location).Hour() == 13 && times[0].In(location).Minute() == 55) + }) } diff --git a/server/parse.go b/server/parse.go index 82d65fd..f5a3410 100755 --- a/server/parse.go +++ b/server/parse.go @@ -677,6 +677,31 @@ func (p *Plugin) normalizeDate(text string, user *model.User) (string, error) { } return parts[2] + "-" + parts[0] + "-" + parts[1] + "T00:00:00Z", nil + } else if match, _ := regexp.MatchString("^([0-9]{4}-[0-9]{2}-[0-9]{2})", date); match { + + date := p.regSplit(date, "-") + + switch len(date) { + case 3: + year, yErr := strconv.Atoi(date[0]) + if yErr != nil { + return "", yErr + } + month, mErr := strconv.Atoi(date[1]) + if mErr != nil { + return "", mErr + } + day, dErr := strconv.Atoi(date[2]) + if dErr != nil { + return "", dErr + } + + return time.Date(year, time.Month(month), day, 0, 0, 0, 0, location).Format(time.RFC3339), nil + + default: + return "", errors.New("unrecognized date") + } + } else if match, _ := regexp.MatchString("^(([0-9]{2}|[0-9]{1})(-|/)([0-9]{2}|[0-9]{1})((-|/)([0-9]{4}|[0-9]{2}))?)", date); match { date := p.regSplit(date, "-|/")