Thariq Shihipar
Thariq Shihipar

Reputation: 1082

Rails/Active Record "ArgumentError: argument out of range" - for "time" fields greater than 24 hours

I'm working with a legacy database which doesn't use great conventions, but I'm unable to change any of the schema.

One problem that has arisen is that for some time fields, the values are greater than 24 hours like: 33:06:46. This gives me a: ArgumentError: argument out of range

This question has been asked before but it didn't have a satisfactory answer. The asker eventually said he used DataMapper which is something I want to avoid.

Does anyone have any ideas for catching this? Can I make Rails treat it as a Date-Time field instead of a Time field without changing the actual schema?

Thanks!

Thariq

Upvotes: 3

Views: 4621

Answers (1)

icanhazbroccoli
icanhazbroccoli

Reputation: 1035

Not sure it's the best way, however:

ActiveRecord uses ActiveSupport::TimeZone.parse method to parse such values ( gems/activesupport-?.?.?/lib/active_support/values/time_zone.rb )

It's quite simple:

def parse(str, now=now)
  date_parts = Date._parse(str)
  return if date_parts.blank?
  time = Time.parse(str, now) rescue DateTime.parse(str)
  if date_parts[:offset].nil?
    ActiveSupport::TimeWithZone.new(nil, self, time)
  else
    time.in_time_zone(self)
  end
end

So you could patch this method for your time format

Upvotes: 1

Related Questions