Search code examples
rubydatestrptime

Ruby Date.strptime doesn't enforce 4-digit year


I would expect this code to give me an ArgumentError: invalid date error. In Ruby 2.0.0 irb:

irb(main):003:0> Date.strptime('05-10-2014', '%Y-%m-%d')
=> #<Date: 0005-10-20 ((1723177j,0s,0n),+0s,2299161j)>

Am I doing something wrong or will Ruby accept a 2-digit year even when I specify %Y?

I am looking at testing user input. In this case my program is expecting it in the %Y-%m-%d and the input date was "entered" in the wrong format, but strptime says it's ok.


Solution

  • There was a Ruby bug opened for this issue last year, but it was rejected. I guess the Ruby team feels that this is valid behavior.

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