mardi 13 décembre 2016

Why does Time.parse fail in Rails 4 and 5 but not in Rails 3 for date strings with a space between the date and time?

In investigating a bug I ran into after upgrading an old Rails 3 app to Rails 4, I noticed that Time.parse fails when this date string contains a space, but parses correctly when the space is removed:

In Rails 3.3.33.5:

Time.parse "12/13/2016 3:11 PM"
=> 2016-12-13 15:11:00 -0500

But in Rails 4 and 5:

Time.parse "12/13/2016 3:11 PM"
ArgumentError: argument out of range
from /Users/diego/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/time.rb:302:in `local'

But if I remove the space between the date and time it works:

Time.parse "12/13/20163:11 PM"
=> 2016-12-13 15:11:00 -0500

The previous also works in Rails 3. I'm stumped as to what happened between the two versions of Rails that broke Time parse when there's a space between the date and time. Any ideas?

As a solution, because I need to be backwards compatible, I'm rescuing the ArgumentError and gsubbing the space out of the date string and trying again. Is there a better solution for this?

Both examples were done in Ruby 2.2.5

Aucun commentaire:

Enregistrer un commentaire