4

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-31to 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.

Milan
  • 1,780
  • 1
  • 16
  • 29
  • 1
    See: https://en.wikipedia.org/wiki/ISO_week_date#Last_week and https://en.wikipedia.org/wiki/ISO_8601 – Tom Lord Jan 24 '19 at 14:55
  • _"I've found out that Thursday is the breaking point"_ – that's expected. According to the [docs](http://ruby-doc.org/stdlib-2.6/libdoc/date/rdoc/Date.html#class-Date-label-Week+Date): _"the first calendar week of the year is the one that includes the first Thursday of that year"_ – Stefan Jan 24 '19 at 15:27
  • What's your definition of week numbering? How do you determine the year's first / last week? Does your week start on Saturday, Sunday, or Monday? Maybe give some examples. – Stefan Jan 24 '19 at 15:36

0 Answers0