chronic
chronic copied to clipboard
Mother's day parsing (calling 'next' may when current month is May)
So I have a method that I was using to calculate Mother's day. And it was working fine until we ticked over to the month of May.
Calling this May works fine now:
2.1.2 :023 > Time.now
=> 2015-05-06 11:08:30 +1000
2.1.2 :024 > Chronic.parse("2nd Sunday this May")
=> 2015-05-10 12:00:00 +1000
But something strange happens when I ask for the 2nd Sunday of next May
2.1.2 :025 > Chronic.parse("2nd Sunday of next May")
RuntimeError: Current month should be set by now
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/repeaters/repeater_month_name.rb:48:in `next'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/handlers.rb:529:in `get_anchor'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/handlers.rb:489:in `handle_o_r_g_r'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/handler.rb:76:in `invoke'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/parser.rb:260:in `block in tokens_to_span'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/parser.rb:258:in `each'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/parser.rb:258:in `tokens_to_span'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic/parser.rb:61:in `parse'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/chronic-0.10.2/lib/chronic.rb:90:in `parse'
from (irb):25
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/skylight-0.3.20/lib/skylight/probes.rb:81:in `require'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/skylight-0.3.20/lib/skylight/probes.rb:81:in `require'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `block in require'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:247:in `require'
from /Users/Fabs/Desktop/Work/HappyWifeBackend/bin/rails:8:in `<top (required)>'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/Fabs/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.5/lib/active_support/dependencies.rb:241:in `load'
from /Users/Fabs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/Fabs/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I could understand if it returned May 2016, but it's not setting the month at all.
Had a look through the source and I am not sure how this is happening… looks a little out of my league though, so not sure if I could contribute a bugfix.
Don't worry about fixing, there's ongoing rewrite of large part of Chronic and I know how big mess current code is. Also actually this is fixed in my rewrite branch
Chronic.parse("2nd Sunday of next May", :guess => false)
=> 2016-05-08 00:00:00 +0300...2016-05-09 00:00:00 +0300
But that rewrite isn't finished and I'll have time to continue work on it only in autumn. So for now you'll have to somehow workaround it yourself.