when icon indicating copy to clipboard operation
when copied to clipboard

"September" always incorrectly parsed as October

Open buzzert opened this issue 7 years ago • 5 comments

Not sure why this is happening, but the following strings are yielding results that are a month off:

when.Parse("September 5th", time.Now()) => 2018-10-05 23:41:28.856911 -0700 PDT
when.Parse("Sept. 5th", time.Now()) => 2018-10-05 23:41:52.679274 -0700 PDT
when.Parse("Sep 5", time.Now()) => 2018-10-05 23:41:52.679274 -0700 PDT

Correct:

when.Parse("October 5th", time.Now()) => 2018-10-05 23:42:06.57631 -0700 PDT

buzzert avatar Sep 01 '18 06:09 buzzert

Also:

"9/9/18" => August!

It looks like when doesn't like September at all!

buzzert avatar Sep 01 '18 06:09 buzzert

Hi @buzzert,

I could not reproduce the reported error, it still happens?

I figured it might be something related to the day you were testing or the timezone, but the result came out right anyway:

w := when.New(nil)
w.Add(en.All...)
w.Add(common.All...)

PDTTimeZone, _ := time.LoadLocation("US/Pacific")

reference_date := time.Date(
	2018, time.September, 1,
	23, 41, 30, 0,
	PDTTimeZone)

r, _ := w.Parse("September 5th", reference_date)
fmt.Println(r.Time.String())

r, _ = w.Parse("Sept. 5th", reference_date)
fmt.Println(r.Time.String())

r, _ = w.Parse("Sep 5", reference_date)
fmt.Println(r.Time.String())

r, _ = w.Parse("9/9/18", reference_date)
fmt.Println(r.Time.String())

Output:

2018-09-05 23:41:30 -0700 PDT
2018-09-05 23:41:30 -0700 PDT
2018-09-05 23:41:30 -0700 PDT
2018-09-09 23:41:30 -0700 PDT

gbaptista avatar Mar 09 '19 14:03 gbaptista

@gbaptista hmm it looks like this only reproduces if I use time.Now() as the reference date. Any idea why this is happening?

The last test makes even less sense.

package main 
import (
    "fmt"
    "time"
    "github.com/olebedev/when"
    "github.com/olebedev/when/rules/en"
    "github.com/olebedev/when/rules/common"
)

func main() {
    w := when.New(nil)
    w.Add(en.All...)
    w.Add(common.All...)

    reference_date := time.Now()

    r, _ := w.Parse("September 5th", reference_date)
    fmt.Println(r.Time.String())

    r, _ = w.Parse("Sept. 5th", reference_date)
    fmt.Println(r.Time.String())

    r, _ = w.Parse("Sep 5", reference_date)
    fmt.Println(r.Time.String())

    r, _ = w.Parse("9/9/18", reference_date)
    fmt.Println(r.Time.String())

    // Correct
    r, _ = w.Parse("January 5th", reference_date)
    fmt.Println(r.Time.String())
}

Output:

2019-10-05 20:54:08.475639614 -0700 PDT
2019-10-05 20:54:08.475639614 -0700 PDT
2019-10-05 20:54:08.475639614 -0700 PDT
2019-03-31 20:54:08.475639614 -0700 PDT m=+0.001660614                                             
2019-01-05 20:56:28.701848988 -0800 PST

buzzert avatar Apr 01 '19 03:04 buzzert

I think I'm running into the same bug - February is parsing as March, and it's near the end of January.

The problem is likely here: https://github.com/olebedev/when/blob/59bd4edcf9d671b3c73416c7b0d55356444507d4/rules/context.go#L17

Why february 11 parses as March 11:

t is time.Now (Jan 30): https://github.com/olebedev/when/blob/59bd4edcf9d671b3c73416c7b0d55356444507d4/rules/context.go#L19

then, the month is set: https://github.com/olebedev/when/blob/59bd4edcf9d671b3c73416c7b0d55356444507d4/rules/context.go#L31-L34

This calls time.Date(2022, February, 30). February 30 is not a real day, so it returns March 2 instead.

Then the day is applied: https://github.com/olebedev/when/blob/59bd4edcf9d671b3c73416c7b0d55356444507d4/rules/context.go#L42-L45

This sets March 2 to March 11.

tadeokondrak avatar Jan 30 '22 23:01 tadeokondrak