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

Example returns unexpected value

matkoniecz opened this issue Β· comments

Holidays.any_holidays_during_work_week?(Date.civil(2016, 1, 1))

returns False.

Note that readme has

To check if there are any holidays taking place during a specified work week:

Holidays.any_holidays_during_work_week?(Date.civil(2016, 1, 1))
=> true

I am unsure is it a regression or a documentation bug.

Hm, I can't get any_holidays_during_work_week to return true.

irb(main):028:0> Holidays.any_holidays_during_work_week?(Date.civil(2018, 1, 1), :pl)
=> false
irb(main):029:0> Holidays.any_holidays_during_work_week?(Date.civil(2018, 1, 2), :pl)
=> false
irb(main):030:0> Holidays.any_holidays_during_work_week?(Date.civil(2018, 5, 3), :pl)
=> false
irb(main):031:0> Holidays.any_holidays_during_work_week?(Date.civil(2018, 5, 2), :pl)

All of that should return true, holidays are listed in Holidays.between(Date.today, Date.today + 365*2, :pl, :observed).

@matkoniecz Thank you for the issue report! I'll try to take a look at this over the weekend to see what is going on.

@matkoniecz Thank you for the issue report.:)
And I'm sorry for the delay in my response.
I'll try to take a look at this too. πŸ˜„

@matkoniecz Small update: I've been digging into #251 and I'm wondering if the knock-on effects of the bug there are contributing to weird behavior in other places. I am close to having a PR to address that issue, after that is done I'm going to re-evaluate the behavior in bugs like this one to see if they are related.

I was just about to report this bug, but see it was already reported a year ago. Has there been any progress in solving this? Or should this method just not be used anymore?

@amy-mac @matkoniecz I'm very sorry for the silence. I have no excuse, just lost track. πŸ€•

I am looking at this tonight. I will add my findings and the produce either a PR with a fix or provide triage info tonight so we know next steps.


For starters, I am able to repro using the same examples from @matkoniecz for :pl. This surprised me because I know we have tests explicitly for this functionality here. You'll notice that all of my tests are focused on the :us region, though. 😭

I will continue digging and provide an update shortly.

EDIT: I removed the other stuff I had in here because I almost immediately remembered the fact that we do switch to a special :any key that we set if no region is submitted! I completely forgot! That's embarrassing! 🀑

Show's how long I have been neglecting. If anyone should remember that it should be me. πŸ’€

Sorry for the wall of text. I wrote most of it while investigating to help organize my thoughts.

tl;dr

When I renamed this method in 2016 I did so based on the stated behavior in the rdoc for the method. It turns out the rdoc was 100% wrong, describing the exact opposite behavior from how the code actually works. Because of this the method returns false when you would expect it to return true based on the name.

Shame on me for trusting comments without reading the code! πŸ’€

What I am going to do to resolve this

  • I will leave the name as any_holidays_during_work_week?
  • I will write new tests to cover the updated behavior
  • I will add more examples to the README
  • I will update the method behavior to match the behavior described by the method name (see below)

The updated method will have the following behavior:

  • It will return true if at least one holiday occurs between Monday and Friday (inclusive) of the week containing the specified day
  • It will return false if no holidays occur between Monday and Friday (inclusive) of the week containing the specified day
  • It will not set any default options (such as :informal or :observed). You can pass in options to modify behavior the same as you can other methods
  • It will calculate the specified week (Monday through Friday, inclusive) from the input date with the assumption that each week starts on Sunday and ends on Saturday. So an input date of Sunday will pick the following Monday through Friday and an input date of Saturday will take the preceding Monday through Friday

Details if you are interested

Originally this function was named full_week? and would return true if there were no observed holidays during the specified week. To put it another way it tried to answer the following question: "do I have to work all 5 days during the specified week?"

When I refactored this function in 2016 I renamed it to the current any_holidays_during_work_week?. It is admittedly a clumsy name but I felt that the method behavior itself was clumsy, too, and decided to be as precise as possible.

Unfortunately I renamed it based on the rdoc for the method and not on the actual behavior of the code. Turns out the behavior stated in the rdoc was exactly opposite from how it actually behaved and from what was stated in the original PR.

From the PR description:

This returns true if it's a normal work week, or false if any of the days is a holiday. 

To me the statement above describes the following behavior:

  • Return true if no holidays took place between Monday through Friday of the specified week since it was a 'normal work week' with no holidays
  • Return false if at least one holiday took place between Monday through Friday of the specified week since it was not a 'normal work week' and had at least one holiday

Now from the rdoc:

Returns true if any holidays fall on Monday - Friday of the given week.

To me the statement above describes the following behavior:

  • Return false if no holidays took place between Monday through Friday of the specified week since it was a 'normal work week' with no holidays
  • Return true if at least one holiday took place between Monday through Friday of the specified week since it was not a 'normal work week' and had at least one holiday

The behavior between the two statements is completely opposite. Unfortunately the rdoc is the one that is incorrect and the first statement is actually what the code does. πŸ€·β€β™‚οΈ

I removed all of the rdoc on July 10, 2015. I am not a believer in heavy commenting to document behavior and this is why: I stupidly relied on the comments without reading the source and it bit me and wasted everyone's time.

Final update for tonight: I'm working on filling out test cases and modifying the behavior. I'm not sure when but I'll aim to get a PR out by this upcoming weekend.

I have opened a PR to address this: #327

After @ttwo32 takes a look I'll merge and gets a release out.

I have merged the PR and released v6.6.1. As far as I can tell this resolves the issue! Please feel free to reopen if I missed something or it is still not behaving as you expect. Thank you again for the report and for your patience!