Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"September" always incorrectly parsed as October #12

Open
buzzert opened this issue Sep 1, 2018 · 4 comments
Open

"September" always incorrectly parsed as October #12

buzzert opened this issue Sep 1, 2018 · 4 comments

Comments

@buzzert
Copy link

buzzert commented Sep 1, 2018

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
Copy link
Author

buzzert commented Sep 1, 2018

Also:

"9/9/18" => August!

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

@gbaptista
Copy link
Contributor

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

@buzzert
Copy link
Author

buzzert commented Apr 1, 2019

@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

@tadeokondrak
Copy link

tadeokondrak commented Jan 30, 2022

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:

func (c *Context) Time(t time.Time) (time.Time, error) {

Why february 11 parses as March 11:

t is time.Now (Jan 30):

t = time.Now()

then, the month is set:

when/rules/context.go

Lines 31 to 34 in 59bd4ed

if c.Month != nil {
t = time.Date(t.Year(), time.Month(*c.Month), t.Day(),
t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}

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:

when/rules/context.go

Lines 42 to 45 in 59bd4ed

if c.Day != nil {
t = time.Date(t.Year(), t.Month(), *c.Day, t.Hour(),
t.Minute(), t.Second(), t.Nanosecond(), t.Location())
}

This sets March 2 to March 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants