Subject: Re: bug#10699: bug of `date`: why 1/1/2012 is the second week of 2012?

From: Eric Blake <[email-masked]>
Date: Thu, 02 Feb 2012 11:32:10 -0700

tag 10699 notabug
thanks

On 02/02/2012 01:25 AM, Wang, YuanTao wrote:
> $ date -d "2012-01-01 00:00:00" +%U
> 01
> 
> but in "man date"
> %U     week number of year, with Sunday as first day of week (00..53)
> 
> Is it a bug?

No, it is behavior required by POSIX,
http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html.

The coreutils FAQ at
https://www.gnu.org/software/coreutils/faq/#The-date-command-is-not-working-right_002e
has more information, including a link to
http://en.wikipedia.org/wiki/ISO_8601, which describes where ISO 8601
week 1 starts:

> There are mutually equivalent descriptions of week 01:
> 
>     the week with the year's first Thursday in it (the formal ISO definition),
>     the week with 4 January in it,
>     the first week with the majority (four or more) of its days in the starting year, and
>     the week starting with the Monday in the period 29 December – 4 January.
> 
> If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year (there is no week 00). 28 December is always in the last week of its year.

That means in ISO 8601 week numbering (%G%V), there is no week 0, days
falling before week 1 are assigned to the last week of the previous year
(likewise, days at the end of the calendar year can be assigned into
week 1 of the next year).  But since week-based years can be confusing
if you aren't familiar with ISO, POSIX provides not one, but two
alternatives.  If you like ISO 8601 weeks starting on Monday, then the
POSIX counterpart (%Y%W) is guaranteed to match the ISO week numbering
for all days except those at the beginning or end of the calendar that
ISO would assign into a different year, but where POSIX assigns them to
week 0 or week 52/53 instead.  And if you like weeks starting on Sunday
instead of Monday, then the POSIX counterpart (%Y%U) shifts things to
start week 1 on Sunday instead of Monday, again with week 0 and week 53
rather than rolling things into different years.

-- 
Eric Blake   [email-masked]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org