Sha256: edd6d0a140f4d1d2220db8d9e810d1cb55001499ceb201670386a3a2cfcd1585

Contents?: true

Size: 1.19 KB

Versions: 8

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module ActiveModel
  module Type
    class Time < Value # :nodoc:
      include Helpers::TimeValue
      include Helpers::AcceptsMultiparameterTime.new(
        defaults: { 1 => 1970, 2 => 1, 3 => 1, 4 => 0, 5 => 0 }
      )

      def type
        :time
      end

      def user_input_in_time_zone(value)
        return unless value.present?

        case value
        when ::String
          value = "2000-01-01 #{value}"
          time_hash = ::Date._parse(value)
          return if time_hash[:hour].nil?
        when ::Time
          value = value.change(year: 2000, day: 1, month: 1)
        end

        super(value)
      end

      private

        def cast_value(value)
          return apply_seconds_precision(value) unless value.is_a?(::String)
          return if value.empty?

          dummy_time_value = value.sub(/\A(\d\d\d\d-\d\d-\d\d |)/, "2000-01-01 ")

          fast_string_to_time(dummy_time_value) || begin
            time_hash = ::Date._parse(dummy_time_value)
            return if time_hash[:hour].nil?
            new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset))
          end
        end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
activemodel-5.2.2.1 lib/active_model/type/time.rb
activemodel-6.0.0.beta1 lib/active_model/type/time.rb
nullifyable-0.1.0 vendor/bundle/gems/activemodel-5.2.2/lib/active_model/type/time.rb
activemodel-5.2.2 lib/active_model/type/time.rb
activemodel-5.2.2.rc1 lib/active_model/type/time.rb
activemodel-5.2.1.1 lib/active_model/type/time.rb
activemodel-5.2.1 lib/active_model/type/time.rb
activemodel-5.2.1.rc1 lib/active_model/type/time.rb