mojombo / chronic

Chronic is a pure Ruby natural language date parser.
http://injekt.github.com/chronic
MIT License
3.23k stars 452 forks source link

Incorrect date parse #375

Open MeterSoft opened 6 years ago

MeterSoft commented 6 years ago

[10] pry(#)> Chronic.parse("4/30") => Mon, 30 Apr 2018 12:00:00 PDT -07:00 [11] pry(#)> Chronic.parse("4/31") => nil [12] pry(#)> Chronic.parse("4/32") => Fri, 16 Apr 2032 00:00:00 PDT -07:00

In last example should be nil

davispuh commented 6 years ago

It's fixed in my unfinished rewrite branch.

MeterSoft commented 6 years ago

@davispuh yes, this branch works with my example but has many another issues. Example [5] pry(Order)> Chronic.parse("1/1/18") => Mon, 01 Jan 2018 12:00:00 PST -08:00 [6] pry(Order)> Chronic.parse("01/1/18") => Thu, 18 Jan 2001 12:00:00 PST -08:00 [7] pry(Order)> Chronic.parse("1/01/18") => Mon, 01 Jan 2018 12:00:00 PST -08:00 [8] pry(Order)> Chronic.parse("11/01/18") => Tue, 18 Jan 2011 12:00:00 PST -08:00

davispuh commented 6 years ago

That's kinda expected behavior. There are 3 different ways how to interpret it and it just guesses it as best as it's possible.

In lib/chronic/definition.rb you can see there is

[[ScalarYear, SeparatorSlash, ScalarMonth, SeparatorSlash, ScalarDay], :handle_sy_sm_sd],
...
[[ScalarMonth, SeparatorSlash, ScalarDay, SeparatorSlash, ScalarYear, [SeparatorDot, Scalar, :none]], :handle_sm_sd_sy],
...
[[ScalarDay, SeparatorSlash, ScalarMonth, SeparatorSlash, ScalarYear, [SeparatorDot, Scalar, :none]], :handle_sd_sm_sy],
MeterSoft commented 6 years ago

it is possible to set default format %m%d%y ?

davispuh commented 6 years ago

Default I don't think so. But you could remove other formats with monkey patching.

Anyway firstly you need to pass :endian_precedence => [:middle] so it doesn't use dd/mm/yy and then to remove other formats

class Chronic::DateDefinitions
  def definitions
    []
  end
end

class Chronic::ShortDateDefinitions
  def definitions
    []
  end
end

class Chronic::DateTimeDefinitions
  def definitions
    []
  end
end
MeterSoft commented 6 years ago

it is possible to fix reported issue in this master branch?

davispuh commented 6 years ago

It will be fixed when rewrite branch will be finished and merged in master.