Off by 1 year error in GNU date

coreutilsdate

Right now, 2014-01-06, GNU date 8.4 on CentOS release 6.4, does this:

$ date --date "last week" +"%Y-%V"
2013-01

On the other hand,

$ date --date "last week" +"%Y-%U"
2013-52

which is just marginally better.

Is this really the correct behavior?!

Best Answer

I didn't want to step on @Jordanm's toes by editing up his answer too much but these additional sentences from the ISO week date he cited were also helpful in understanding the issue the OP has highlighted.

excerpt #1 - regarding ISO week dates

The system uses the same cycle of 7 weekdays as the Gregorian calendar. Weeks start with Monday. ISO week-numbering years have a year numbering which is approximately the same as the Gregorian years, but not exactly since an ISO week-numbering year (also called ISO year or week year informally) has 52 or 53 full weeks. That is 364 or 371 days instead of the usual 365 or 366 days. The extra week is referred to here as a leap week, although ISO 8601 does not use this term.

excerpt #2 - I also found this bit helpful in understanding the issue

The first week of a year is the week that contains the first Thursday of the year. It is also (equivalently) the week containing the 4th day of January.

Revisiting the date command

So if you take a look at the dateman page you'll notice this option in the formatting section.

   %G     year of ISO week number (see %V); normally useful only with %V

So when you want to print the year along with the ISO week (%V) you should really be using the %G for the year and not the typical %Y.

Example

So when my system's date is reportedly this:

$ date
Mon Jan  6 15:13:32 EST 2014

This command will report the ISO week date correctly, as you're looking for:

$ date --date "last week" +"%G-%V"
2014-01
Related Question