2019 incorrect week calculation by the standard date format mask

1

Normally I calculate the week of the year by the standard date format mask %Y%W. Till 2019 it worked quite good but last days I've noticed that the first week of the year 2019 (2018/12/31-2019/01/06) is shown like the last week number 53 from 2018. And only the week 2019/01/07-2019/01/13 is the first week of 2019. I've checked if it's correct.

Everywhere you can see that the week 2018/12/31-2019/01/06 is currently the first week of 2019. And even Oracle knows it to_char(..., 'yyyyiw'), but R doesn't. At least it should have the same opinion.

It does matter for me and I think it looks like a bug. I'm using R 3.5.0.

Wladislaw Poniatowski

Posted 2019-01-09T10:32:06.200

Reputation: 11

Answers

1

See ?strftime:

 ‘%U’ Week of the year as decimal number (00-53) using Sunday as
      the first day 1 of the week (and typically with the first
      Sunday of the year as day 1 of week 1).  The US convention.

 ‘%V’ Week of the year as decimal number (01-53) as defined in ISO
      8601.  If the week (starting on Monday) containing 1 January
      has four or more days in the new year, then it is considered
      week 1.  Otherwise, it is the last week of the previous year,
      and the next week is week 1.  (Accepted but ignored on
      input.)

 ‘%W’ Week of the year as decimal number (00-53) using Monday as
      the first day of week (and typically with the first Monday of
      the year as day 1 of week 1).  The UK convention.

The format mask IW in Oracle returns the ISO week number of the year, i.e. %V in R:

R> x <- as.Date("2018-12-28") + 0:10
R> data.frame(date=x, weeknum=format(x, "%V"))
         date weeknum
1  2018-12-28      52
2  2018-12-29      52
3  2018-12-30      52
4  2018-12-31      01
5  2019-01-01      01
6  2019-01-02      01
7  2019-01-03      01
8  2019-01-04      01
9  2019-01-05      01
10 2019-01-06      01
11 2019-01-07      02

rcs

Posted 2019-01-09T10:32:06.200

Reputation: 170

Thanks a lot! I didn't know about the mask with %V. It's not always present in the date format descriptions, you can find on the internet. I didn't know there's something else. It was also confusing that the %W mask has worked very well in 2018. Anyway thanks! – Wladislaw Poniatowski – 2019-01-09T13:39:46.423