aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/time_value.rb
blob: 8d9ac25643c26c14189800df5e43ce3f46371faf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module ActiveRecord
  module Type
    module TimeValue # :nodoc:
      def klass
        ::Time
      end

      def type_cast_for_schema(value)
        "'#{value.to_s(:db)}'"
      end

      def user_input_in_time_zone(value)
        value.in_time_zone
      end

      private

      def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
        # Treat 0000-00-00 00:00:00 as nil.
        return if year.nil? || (year == 0 && mon == 0 && mday == 0)

        if offset
          time = ::Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
          return unless time

          time -= offset
          Base.default_timezone == :utc ? time : time.getlocal
        else
          ::Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
        end
      end

      # Doesn't handle time zones.
      def fast_string_to_time(string)
        if string =~ ConnectionAdapters::Column::Format::ISO_DATETIME
          microsec = ($7.to_r * 1_000_000).to_i
          new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
        end
      end
    end
  end
end