ruby / date

A subclass of Object includes Comparable module for handling dates.
Other
70 stars 35 forks source link

Date.commercial method does not account for leap year #30

Closed amboxer21 closed 3 years ago

amboxer21 commented 3 years ago

The Date.commercial method does NOT account for a leap year.

irb(main):099:0> d = Date.parse('2021-01-03')
=> #<Date: 2021-01-03 ((2459218j,0s,0n),+0s,2299161j)>
irb(main):100:0> 
irb(main):101:0> d.year
=> 2021
irb(main):102:0> d.cweek
=> 53
irb(main):103:0> 
irb(main):104:0> 
irb(main):105:0> 
irb(main):106:0> Date.commercial(d.year,d.cweek)
Traceback (most recent call last):
        3: from /usr/bin/irb:11:in `<main>'
        2: from (irb):106
        1: from (irb):106:in `commercial'
ArgumentError (invalid date)
irb(main):107:0>
amboxer21 commented 3 years ago

When this year that we are working with is a leap year, cweek returns 53 when ran on the Date.parse method. The Date.commercial method does not know how to handle week 53.

jeremyevans commented 3 years ago

You are misusing the API. You should be using d.cwyear instead of d.year when calling Date.commercial.