This project is archived and is in readonly mode.

#3183 ✓invalid
Evgeniy Pirogov

time_with_datetime_fallback returns DateTime object in fallback instead of Time

Reported by Evgeniy Pirogov | September 10th, 2009 @ 08:16 PM

"2009-09-10 24:00:00".to_time - "2009-09-10 00:00:00".to_time
TypeError Exception: expected numeric or date


Expected
86400.0

Reason
"2009-09-10 24:00:00".to_time.class
DateTime

"2009-09-10 00:00:00".to_time.class
Time


source

# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 44
44:           def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
45:             ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
46:           rescue
47:             offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0
48:             ::DateTime.civil(year, month, day, hour, min, sec, offset)
49:           end



Fix
# File vendor/rails/activesupport/lib/active_support/core_ext/time/calculations.rb, line 44
def time_with_datetime_fallback(utc_or_local, year, month=1, day=1, hour=0, min=0, sec=0, usec=0)
  ::Time.send(utc_or_local, year, month, day, hour, min, sec, usec)
rescue
  offset = utc_or_local.to_sym == :local ? ::DateTime.local_offset : 0
 ::DateTime.civil(year, month, day, hour, min, sec, offset).to_time
end

Comments and changes to this ticket

  • Jeremy Kemper

    Jeremy Kemper September 18th, 2009 @ 03:17 AM

    • State changed from “new” to “invalid”

    The rescue is used to handle Times out of range of the 32-bit epoch, not to fix invalid arguments.

  • Evgeniy Pirogov

    Evgeniy Pirogov September 18th, 2009 @ 02:33 PM

    interesting, so the method return object of different class.
    why the rescue does not distinguish between "time out of range" and "argument out of range"

    ::Time.send(:utc, 2040, 1, 1, 1, 1, 1, 0) ArgumentError: time out of range

    from (irb):1:in `utc'
    from (irb):1:in `send'
    from (irb):1
    

    ::Time.send(:utc, 2010, 1, 1, 24, 0, 0, 0) ArgumentError: argument out of range

    from (irb):2:in `utc'
    from (irb):2:in `send'
    from (irb):2
    

    32bit problem in my view is less evil, than different class.
    and 32bit problem "can't be properly" solved on 32bit platform anyhow
    the rails app will die before reaching this point in the code in year 2038-Jun-19 03:14:07

    just my 2 cents

  • csnk

    csnk May 18th, 2011 @ 08:23 AM

    • Importance changed from “” to “”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages