From: merch-redmine@... Date: 2017-11-13T01:42:08+00:00 Subject: [ruby-core:83747] [Ruby trunk Bug#14102] Date.strptime ignores constraints provided by day name Issue #14102 has been updated by jeremyevans0 (Jeremy Evans). cyclotron3k (A Samuel) wrote: > Date.strptime('Potato, November 13, 2017', '%A, %B %d, %Y').strftime('%A, %B %d, %Y') > => ArgumentError: invalid date Fails because Potato is not a valid day name. > > Date.strptime('Friday, November 31, 2017', '%A, %B %d, %Y').strftime('%A, %B %d, %Y') > => ArgumentError: invalid date Fails because November only has 30 days. > Date.strptime('Tuesday, November 13, 2017', '%A, %B %d, %Y').strftime('%A, %B %d, %Y') > => "Monday, November 13, 2017" Doesn't fail because `%d` takes precedence over `%A`. `Date.strptime` does not check that the all format specifier values are internally consistent. This is true not just for day names but in general: ~~~ruby Date.strptime('4 3', '%W %d') => # Date.strptime('4 3', '%w %d') => # ~~~ You can even pass multiple of the same specifiers, in which case last one wins: ~~~ruby Date.strptime('3 4', '%d %d') => # Date.strptime('3 4', '%m %m') => # ~~~ I get the feeling that asking `Date.strptime` to check that all format specifier values are internally consistent is asking too much. It may be possible to correctly handle all cases, but it would be very complex. There are also a lot of situations where the combination of format specifiers used still results in ambiguity. Consider `Date.strptime('4 3', '%w %d')`, where week day is 3 and month day is 4. Should it try to find the closest Wednesday that is the 4th of the month? ---------------------------------------- Bug #14102: Date.strptime ignores constraints provided by day name https://bugs.ruby-lang.org/issues/14102#change-67785 * Author: cyclotron3k (A Samuel) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: 2.4.2 * Backport: 2.3: UNKNOWN, 2.4: UNKNOWN ---------------------------------------- ~~~ ruby RUBY_VERSION => "2.4.2" # also tested in 2.5.0 require 'date' => true Date.strptime('Potato, November 13, 2017', '%A, %B %d, %Y').strftime('%A, %B %d, %Y') => ArgumentError: invalid date Date.strptime('Friday, November 31, 2017', '%A, %B %d, %Y').strftime('%A, %B %d, %Y') => ArgumentError: invalid date # November 13, 2017 is a Monday Date.strptime('Tuesday, November 13, 2017', '%A, %B %d, %Y').strftime('%A, %B %d, %Y') => "Monday, November 13, 2017" ~~~ None of the dates above are valid, only one gets coerced. -- https://bugs.ruby-lang.org/ Unsubscribe: