Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
handle ISO format dates. Issue #149
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleedavis committed Aug 10, 2019
1 parent 1244cef commit 432ab3c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/occurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions server/occurrence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})
}

Expand Down
25 changes: 25 additions & 0 deletions server/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "-|/")
Expand Down

0 comments on commit 432ab3c

Please sign in to comment.