holidays / holidays

A collection of Ruby methods to deal with statutory and other holidays. You deserve a holiday!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling Holidays.on(Date.yesterday) gives empty array until given a flag

itmart opened this issue · comments

Yesterday, Feb 18, 2019, was Presidents' Day in the US. But when we try to do Holidays.on(Date.yesterday), it gave an empty array. Then we tried Holidays.on(Date.yesterday, :us) and Presidents' Day showed up. Holidays.on(Date.yesterday) started giving all the holidays on that day on subsequent calls. I couldn't figure out why this is happening and just really wanted an explanation if I missed something.

2.5.1 :001 > Holidays.on(Date.yesterday)
 => [] 
2.5.1 :002 > Holidays.on(Date.yesterday)
 => [] 
2.5.1 :003 > Holidays.on(Date.yesterday)
 => [] 
2.5.1 :004 > Holidays.on(Date.yesterday)
 => [] 
2.5.1 :005 > Holidays.on(Date.yesterday, :us)
 => [{:date=>Mon, 18 Feb 2019, :name=>"Presidents' Day", :regions=>[:us]}] 
2.5.1 :006 > Holidays.on(Date.yesterday)
 => [{:date=>Mon, 18 Feb 2019, :name=>"Family Day", :regions=>[:ca_ab, :ca_on, :ca_sk]}, {:date=>Mon, 18 Feb 2019, :name=>"Louis Riel Day", :regions=>[:ca_mb]}, {:date=>Mon, 18 Feb 2019, :name=>"Nova Scotia Heritage Day", :regions=>[:ca_ns]}, {:date=>Mon, 18 Feb 2019, :name=>"Islander Day", :regions=>[:ca_pe]}, {:date=>Mon, 18 Feb 2019, :name=>"Presidents' Day", :regions=>[:us]}] 

Hi @itmart! Happy to take a look. It looks like what you are providing should work but the last two command results make me nervous.

To explain the result changes between calls: we load definitions on the fly for performance reasons and we also do some special logic surrounding holidays that are 'shared' between North American countries. In theory you should never receive different results depending on the order in which you are calling different commands but I'm wondering if there's a bug in how that definition loading is working.

First, can you tell me which version of the gem you are running?

Second, can you tell me what Date.yesterday actually resolves to? If I remember correctly, Date.yesterday is an ActiveSupport method, right? I'll try testing that out specifically in the meantime.

Can I also ask what you get when you run this:

irb(main):004:0> d = Date.civil(2019, 2, 18)
=> #<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>
irb(main):005:0> Holidays.on(d)
=> [{:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Washington's Birthday", :regions=>[:federalreserve, :federalreservebanks]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Family Day", :regions=>[:ca_sk]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Family Day", :regions=>[:ca_on]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Family Day", :regions=>[:ca_bc]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Family Day", :regions=>[:ca_nb]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Louis Riel Day", :regions=>[:ca_mb]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Nova Scotia Heritage Day", :regions=>[:ca_ns]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Islander Day", :regions=>[:ca_pe]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Presidents' Day", :regions=>[:us, :nyse]}, {:date=>#<Date: 2019-02-18 ((2458533j,0s,0n),+0s,2299161j)>, :name=>"Family Day", :regions=>[:ca_ab]}]

Now that you mentioned it, I realized we are still on 4.7.0. I tried updating to 7.0.0 and it seem to fix the problem! I can close this issue now but I'm not sure if you'd want to look into this any further, so it's your call!

But to answer your other questions, here's what I'm getting when I run them through my console. You are also correct that Date.yesterday is an ActiveSupport method, and it should be returning a Date object too:

2.5.1 :001 > Date.yesterday
 => Mon, 18 Feb 2019 
2.5.1 :002 > Date.civil(2019, 2, 18)
 => Mon, 18 Feb 2019 
2.5.1 :003 > d = Date.civil(2019, 2, 18)
 => Mon, 18 Feb 2019 
2.5.1 :004 > Holidays.on(d)
 => [] 

Okay, great! I have fixed a few issues like this over the years so I'm glad that we're doing what you expected. Please don't hesitate to open new issues if you spot weird behavior. ❤️

Going to close this out, feel free to open a new issue if you spot behavior you don't expect.