Issue #9764 has been updated by tadayoshi funaba.


~~~
first of all, you have to know there are three systems of week.

the following thee are proper/typical combinatins.

%G %V %u # (I)
%Y %W %u # (E)
%Y %U %w # (R)

strptime try to find other ways, but basically supoorts the above three only.

    January 2014  (I)
 M Tu  W Th  F  S  S
30 31  1  2  3  4  5  # 1st
 6  7  8  9 10 11 12  # 2nd
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31      
                    
    January 2014  (E)
 M Tu  W Th  F  S  S
       1  2  3  4  5  # 0th
 6  7  8  9 10 11 12  # 1st
13 14 15 16 17 18 19  # 2nd
20 21 22 23 24 25 26
27 28 29 30 31      

    January 2014  (R)
 S  M Tu  W Th  F  S
          1  2  3  4  # 0th
 5  6  7  8  9 10 11  # 1st
12 13 14 15 16 17 18  # 2nd
19 20 21 22 23 24 25
26 27 28 29 30 31   

Date.new(2013,12,30).strftime('%G %V %u | %Y %W %u | %Y %U %w') #=> "2014 01 1 | 2013 52 1 | 2013 52 1"
Date.new(2014,1,1).  strftime('%G %V %u | %Y %W %u | %Y %U %w') #=> "2014 01 3 | 2014 00 3 | 2014 00 3"
Date.new(2014,1,5).  strftime('%G %V %u | %Y %W %u | %Y %U %w') #=> "2014 01 7 | 2014 00 7 | 2014 01 0"
Date.new(2014,1,6).  strftime('%G %V %u | %Y %W %u | %Y %U %w') #=> "2014 02 1 | 2014 01 1 | 2014 01 1"

they are different systems.


answer of 1.

it is meaningless.


answer of 2.

%U (and %W) can't align with %G.
so, strptime just ignores %U and completes first day of first week.

Date.strptime("2014","%G") == Date.strptime("2014 01 1","%G %V %u") #=> true


answer of 3.

Date.strptime('00 2014', '%W %Y') # ArgumentError: invalid date

but,

Date.strptime('01 2014', '%W %Y') #=> #<Date: 2014-01-06 ((2456664j,0s,0n),+0s,2299161j)>
Date.strptime('3 00 2014', '%u %W %Y') #=> #<Date: 2014-01-01 ((2456659j,0s,0n),+0s,2299161j)>

why?

since, the 0th monday (= the first day of week) is not exists, not valid.

Date.new(2013,12,30).step(Date.new(2014,1,11)){|d| p d.strftime('%Y %W %u')}
"2013 52 1"
"2013 52 2"
"2014 00 3"
"2014 00 4"
"2014 00 5"
"2014 00 6"
"2014 00 7"
"2014 01 1"
"2014 01 2"
"2014 01 3"
"2014 01 4"
"2014 01 5"
"2014 01 6"
#=> #<Date: 2013-12-30 ((2456657j,0s,0n),+0s,2299161j)>


~~~

----------------------------------------
Bug #9764: Date and DateTime strptime and strftime not supporting proper Week Numbering for Monday vs Sunday as start day and %G causes ignore of all other format arguments
https://bugs.ruby-lang.org/issues/9764#change-46275

* Author: Steve R
* Status: Assigned
* Priority: Low
* Assignee: tadayoshi funaba
* Category: 
* Target version: 
* ruby -v: ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
Date and DateTime strftime and strptime are not supporting %U (0-53 Week Numbers as defined in strptime): 
http://www.ruby-doc.org/stdlib-2.1.1/libdoc/date/rdoc/Date.html#method-i-strftime

Some examples that are not producing expected results/output:

1. require 'date'; puts Date.new(2013,12,30).strftime("%G %U")  # 2014 52  -- How is this Week 52 of 2013??

2. require 'date'; puts Date.strptime("2014 01","%G %U")  # 2013-12-30 -- This should really be 2013-12-29 as the 29th is the sunday and the 30th is the Monday, and %U should be using Sunday as the first day of week.

3. The following three examples all produce the same result but really should not.  Even if you change the week number to any number it still stays at the same output/result.  It seems like there is a bug related to %G that ignores all other commands:

puts Date.strptime('00 2014', '%U %G')  # 2013-12-30
puts Date.strptime('00 2014', '%W %G')  # 2013-12-30
puts Date.strptime('2014W011', '%GW%V%u')  # 2013-12-30


Additionally one would expect the following two lines to produce the same results:
puts Date.strptime('00 2014', '%W %G')  # 2013-12-30
puts Date.strptime('00 2014', '%W %Y')  # Invalid Date Argument





-- 
https://bugs.ruby-lang.org/