Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

time_with_datetime_fallback returns DateTime object in fallback instead of Time

#3183
"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

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • 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.

    September 18th, 2009 @ 03:17 AM

  2. Evgeniy Pirogov
    Evgeniy Pirogov

    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

    September 18th, 2009 @ 02:33 PM