Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveSupport::TimeZone.strptime has an empty? for nil exception #25701

Closed
nbibler opened this issue Jul 5, 2016 · 5 comments
Closed

ActiveSupport::TimeZone.strptime has an empty? for nil exception #25701

nbibler opened this issue Jul 5, 2016 · 5 comments

Comments

@nbibler
Copy link
Contributor

nbibler commented Jul 5, 2016

Steps to reproduce

Parse an ActiveSupport::TimeZone using strptime with a malformed string.

require "active_support/all"

tz = ActiveSupport::TimeZone.new("Eastern Time (US & Canada)")

puts %|Good: tz.strptime("2014-04-13", "%Y-%m-%d")|
p tz.strptime("2014-04-13", "%Y-%m-%d")

puts %|Good: tz.strptime("2014-04-13 21:20:50", "%Y-%m-%d")|
p tz.strptime("2014-04-13 21:20:50", "%Y-%m-%d")

# slashes and wrong part order.
puts %|Bad: tz.strptime("2014-04-13 21:20:50", "%m/%d/%Y")|
p tz.strptime("2014-04-13 21:20:50", "%m/%d/%Y")

Yields:

Good: tz.strptime("2014-04-13", "%Y-%m-%d")
Sun, 13 Apr 2014 00:00:00 EDT -04:00
Good: tz.strptime("2014-04-13 21:20:50", "%Y-%m-%d")
Sun, 13 Apr 2014 00:00:00 EDT -04:00
Bad: tz.strptime("2014-04-13 21:20:50", "%m/%d/%Y")
/ruby/2.3.1/gems/activesupport-5.0.0/lib/active_support/values/time_zone.rb:450:in `parts_to_time': undefined method `empty?' for nil:NilClass (NoMethodError)
        from /ruby/2.3.1/gems/activesupport-5.0.0/lib/active_support/values/time_zone.rb:383:in `strptime'
        from test.rb:12:in `<main>'

Expected behavior

It should either raise a reasonable exception or return nil.

Actual behavior

It raises a NoMethodError: undefined method empty? for nil:NilClass.

System configuration

Rails version: 4.2.6, 5.0.0

Ruby version: 2.1.8, 2.3.1

@prakashmurthy
Copy link
Contributor

Standard ruby version of strptime returns a ArgumentError: invalid date error message in such cases.

> Date.strptime("2016-09-15", "%Y-%d-%m")
ArgumentError: invalid date
> DateTime.strptime("2014-04-13 21:20:50", "%m/%d/%Y")
ArgumentError: invalid date

@nbibler
Copy link
Contributor Author

nbibler commented Jul 5, 2016

ArgumentError would be fine. The current error is due to a coding problem / bug (parts.empty?) in ActiveSupport:

module ActiveSupport
  class TimeZone
    private
      def parts_to_time(parts, now)
        return if parts.empty?
        # …
      end
  end
end

The exception coming out of ActiveSupport does not give an application developer any indication of the actual problem and loses the "malformed string / unmatching format" indication due to the nil handling issue.

@jmondo
Copy link
Contributor

jmondo commented Jul 11, 2016

@nbibler are you working on this? if not, I'd be happy to submit a PR. I've been working my way through ActiveSupport as part of a learning exercise and fixing this bug would help my learning! LMK

@nbibler
Copy link
Contributor Author

nbibler commented Jul 11, 2016

You're welcome to go for it. I imagine it'll be straightforward. Worst case, it's just a return if parts.nil? || parts.empty?

@jmondo
Copy link
Contributor

jmondo commented Jul 11, 2016

Sweet. Yep I think so too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants