Sha256: 238820b9481d4bdd4578f050940cc1ff1a8b9afd0d34db756c908ed92a279a7c

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 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)
        
        options.reverse_merge!(:strict => true)

        time_array = ValidatesTimeliness::Formats.parse(raw_value, type, options)
        raise if time_array.nil?
        
        # Rails dummy time date part is defined as 2000-01-01
        time_array[0..2] = 2000, 1, 1 if type == :time
  
        # Date.new enforces days per month, unlike Time
        date = Date.new(*time_array[0..2]) unless type == :time
        
        return date if type == :date
        
        make_time(time_array[0..7])
      rescue
        nil
      end

      def make_time(time_array)
        if Time.respond_to?(:zone) && ValidatesTimeliness.use_time_zones
          Time.zone.local(*time_array)
        else
          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
      end

    end

  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
adzap-validates_timeliness-2.0.0 lib/validates_timeliness/parser.rb
adzap-validates_timeliness-2.1.0 lib/validates_timeliness/parser.rb
validates_timeliness-2.1.0 lib/validates_timeliness/parser.rb
validates_timeliness-2.0.0 lib/validates_timeliness/parser.rb