Bug #4460
closedDate.commercial date calculations don't match strftime output
Description
=begin
When retrieving the date for a week number using Date.commercial, the output sometimes differs from what you expect when looking at strftime. In my opinion, the following should always be true:
(({Date.commercial(year, week, day).strftime('%W') => week (String) }))
Which is in fact true today:
(({Date.today.strftime('%W') => "09"
Date.commercial(2011,9,1) => Mon, 28 Feb 2011
Date.today.at_beginning_of_week => Mon, 28 Feb 2011 }))
But not two years from now:
(({(Date.today+2.years).strftime("%W") => "08"
Date.commercial(2013,8,1) => Mon, 18 Feb 2013
(Date.today+2.years).at_beginning_of_week => Mon, 25 Feb 2013 }))
so:
(({Date.commercial(2013,8,1).strftime('%W') => "7"}))
I'm not quite sure why this is, but I think it's a bug and unfortunately it messes up some of my date calculations.
=end
Updated by jhaagmans (Jaap Haagmans) about 14 years ago
=begin
Hmm the pieces of code got printed on one line, I'll try again.
- The today example:
Date.today.strftime('%W') => "09"
Date.commercial(2011,9,1) => Mon, 28 Feb 2011
Date.today.at_beginning_of_week => Mon, 28 Feb 2011
*The 2 years from now example:
(Date.today+2.years).strftime("%W") => "08"
Date.commercial(2013,8,1) => Mon, 18 Feb 2013
(Date.today+2.years).at_beginning_of_week => Mon, 25 Feb 2013
=end
Updated by tadf (tadayoshi funaba) almost 14 years ago
- Assignee set to tadf (tadayoshi funaba)
Updated by tadf (tadayoshi funaba) almost 14 years ago
- Status changed from Open to Rejected
not a bug.
'%U', '%V' and '%W' are available for week number.
you should use %V in this case.
you can also use it with Date.strptime.
Date.new(2011,2,28).strftime('%G-%V-%u')
#=> "2011-09-1"
Date.new(2011,2,28).next_year(2).strftime('%G-%V-%u')
#=> "2013-09-4"
Date.commercial(2011,9,1)
#=> #<Date: 2011-02-28 ((2455621j,0s,0n),+0s,2299161j)>
Date.commercial(2013,9,4)
#=> #<Date: 2013-02-28 ((2456352j,0s,0n),+0s,2299161j)>
Date.strptime('2011-09-1', '%G-%V-%u')
#=> #<Date: 2011-02-28 ((2455621j,0s,0n),+0s,2299161j)>
Date.strptime('2013-09-4', '%G-%V-%u')
#=> #<Date: 2013-02-28 ((2456352j,0s,0n),+0s,2299161j)>
Updated by tadf (tadayoshi funaba) almost 14 years ago
repost code
=begin
Date.new(2011,2,28).strftime('%G-%V-%u')
#=> "2011-09-1"
Date.new(2011,2,28).next_year(2).strftime('%G-%V-%u')
#=> "2013-09-4"
Date.commercial(2011,9,1)
#=> #<Date: 2011-02-28 ((2455621j,0s,0n),+0s,2299161j)>
Date.commercial(2013,9,4)
#=> #<Date: 2013-02-28 ((2456352j,0s,0n),+0s,2299161j)>
Date.strptime('2011-09-1', '%G-%V-%u')
#=> #<Date: 2011-02-28 ((2455621j,0s,0n),+0s,2299161j)>
Date.strptime('2013-09-4', '%G-%V-%u')
#=> #<Date: 2013-02-28 ((2456352j,0s,0n),+0s,2299161j)>
=end