aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/oid/date_time.rb
blob: b7acbf7178ddf8c628fc1bec559cff70c71fca55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module OID # :nodoc:
        class DateTime < Type::DateTime # :nodoc:
          def cast_value(value)
            case value
            when "infinity" then ::Float::INFINITY
            when "-infinity" then -::Float::INFINITY
            when / BC$/
              astronomical_year = format("%04d", -value[/^\d+/].to_i + 1)
              super(value.sub(/ BC$/, "").sub(/^\d+/, astronomical_year))
            else
              super
            end
          end
        end
      end
    end
  end
end