Sha256: d15fe7a885ceafd4d68bbc8cbba4e180b26b4fe8b548e5cd269d9e8467d52d32

Contents?: true

Size: 1.31 KB

Versions: 9

Compression:

Stored size: 1.31 KB

Contents

module ValidatesTimeliness
  module Parser

    class << self

      def parse(raw_value, type, options={})
        return nil if raw_value.blank?
        return raw_value if raw_value.acts_like?(:time) || raw_value.is_a?(Date)
        
        time_array = ValidatesTimeliness::Formats.parse(raw_value, type, options.reverse_merge(:strict => true))
        return nil if time_array.nil?
        
        if type == :date
          Date.new(*time_array[0..2]) rescue nil
        else
          make_time(time_array[0..7])
        end
      end

      def make_time(time_array)
        # Enforce date part validity which Time class does not
        return nil unless Date.valid_civil?(*time_array[0..2])

        if Time.respond_to?(:zone) && ValidatesTimeliness.use_time_zones
          Time.zone.local(*time_array)
        else
          # Older AR way of handling times with datetime fallback
          begin
            time_zone = ValidatesTimeliness.default_timezone
            Time.send(time_zone, *time_array)
          rescue ArgumentError, TypeError
            zone_offset = time_zone == :local ? DateTime.local_offset : 0
            time_array.pop # remove microseconds
            DateTime.civil(*(time_array << zone_offset))
          end
        end
      rescue ArgumentError, TypeError
        nil
      end

    end

  end
end

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
adzap-validates_timeliness-2.2.0 lib/validates_timeliness/parser.rb
adzap-validates_timeliness-2.2.1 lib/validates_timeliness/parser.rb
adzap-validates_timeliness-2.2.2 lib/validates_timeliness/parser.rb
szilm-validates_timeliness-2.3.1 lib/validates_timeliness/parser.rb
validates_timeliness-2.3.1 lib/validates_timeliness/parser.rb
validates_timeliness-2.3.0 lib/validates_timeliness/parser.rb
validates_timeliness-2.2.2 lib/validates_timeliness/parser.rb
validates_timeliness-2.2.1 lib/validates_timeliness/parser.rb
validates_timeliness-2.2.0 lib/validates_timeliness/parser.rb