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

Can't set time_class to Date #411

Open jgomo3 opened 1 year ago

jgomo3 commented 1 year ago

This line [1] suggest that one could use Date as the time_class. But if you do, then this other line fails[2] because the class Date doesn't have the now method.

You would think that the option :now would work, but line[3] fails as it doesn't use this option.

require 'date'
require 'chronic'
Chronic.time_class = Date
Chronic.parse('2000 January 1st', now: Time.now)

Error:

Traceback (most recent call last):
       11: from bin/console:14:in `<main>'
       10: from (irb):5
        9: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic.rb:90:in `parse'
        8: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/parser.rb:60:in `parse'
        7: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/parser.rb:228:in `tokenize'
        6: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/parser.rb:228:in `each'
        5: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/parser.rb:229:in `block in tokenize'
        4: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/ordinal.rb:12:in `scan'
        3: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/ordinal.rb:12:in `each_index'
        2: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/ordinal.rb:19:in `block in scan'
        1: from /home/monkey/.rvm/gems/ruby-2.7.2/gems/chronic-0.10.2/lib/chronic/date.rb:66:in `make_year'
NoMethodError (undefined method `now' for Date:Class)
Did you mean?  new

[1] https://github.com/mojombo/chronic/blob/master/lib/chronic.rb#L145 [2] https://github.com/mojombo/chronic/blob/master/lib/chronic/parser.rb#L60 [3] https://github.com/mojombo/chronic/blob/master/lib/chronic/date.rb#L65

sriram0601 commented 7 months ago

Chronic gem basically parse the given string to suitable Time or DateTime object, when it comes to Date, we cannot handle the time based methods (i.e: we cannot find hour, mins and seconds). I think thats why they don't provide option to handle Date class.