Project

General

Profile

Actions

Bug #9764

closed

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

Added by StephenOTT (Steve R) almost 10 years ago. Updated almost 10 years ago.

Status:
Rejected
Target version:
-
ruby -v:
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]
[ruby-core:62111]

Description

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

Updated by StephenOTT (Steve R) almost 10 years ago

Same issue with: ruby 2.0.0p451 (2014-02-24 revision 45167) [x86_64-darwin12.5.0]

Updated by tadf (tadayoshi funaba) almost 10 years ago

  • Status changed from Open to Assigned
  • Assignee set to tadf (tadayoshi funaba)
  • Priority changed from Normal to 3

Updated by tadf (tadayoshi funaba) almost 10 years ago

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)>


Updated by tadf (tadayoshi funaba) almost 10 years ago

  • Status changed from Assigned to Rejected

Updated by StephenOTT (Steve R) almost 10 years ago

Okay great. Thank you for the quick and detailed response. This seems to be a documentation confusion as we had about 10 people trying this out over a few days and all were confused based on what was in the documentation and the results. I will submit some updated documentation to clarify.

Updated by StephenOTT (Steve R) almost 10 years ago

@Tadayoshi funaba,

I think i understand part of the issue now related to some of the confusion.

Date.Comercial() allows you to provide the Year and Week number to get a date. It was the expectation that puts Date.strptime('2014 00', '%Y %U' ) would generate the same value as .Comercial but with Sunday as the first day of Week.

Is there a way to support .Commercial input arguments with a Sunday first day or week?

The use case is that many systems will return a %U style 0-53 week time grouping (group results by week number). The system returns a Week Number (0-53) integer and a Year integer. You want to convert this into a date for sorting and other benefits, but you do not know the day number.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0