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: 2017-11-03 ((2458061j,0s,0n),+0s,2299161j)>

Date.strptime('4 3', '%w %d')
=> #<Date: 2017-11-03 ((2458061j,0s,0n),+0s,2299161j)>
~~~

You can even pass multiple of the same specifiers, in which case last one wins:

~~~ruby
Date.strptime('3 4', '%d %d')
=> #<Date: 2017-11-04 ((2458062j,0s,0n),+0s,2299161j)>

Date.strptime('3 4', '%m %m')
=> #<Date: 2017-04-01 ((2457845j,0s,0n),+0s,2299161j)>
~~~

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: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>