Following code works unexpectedly from my point of view:
date = Date.parse('2012-12-31')
# => Mon, 31 Dec 2012
date.cweek
# => 1
Testing it some more, I've found out that Thursday is the breaking point, meaning if most days of a week are in current year, than week is calculated towards that year, otherwise it is the next.
date = Date.parse('2014-12-31')
# => Wed, 31 Dec 2014
date.cweek
# => 1
date = Date.parse('2015-12-31')
# => Thu, 31 Dec 2015
date.cweek
# => 53
Since it's a last day of the year shouldn't it return 53 instead? Does the fact that 2012 is leap year make any difference?
The problem I'm facing is that I'm trying to generate week number for data analysts and they consider for example 2012-12-31
to be the last week (53) of 2012.
Any idea whether this Ruby logic is some kind of standard, or just an arithmetic middle logic?
I've tried looking for some official docs illustrating this behavior, but haven't found any.