Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 314
J Graham Avatar asked Jul 16 '14 19:07

J Graham


1 Answers

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

like image 127
infused Avatar answered Dec 14 '22 23:12

infused