Sha256: b978ae0af423312a1720ef1115193485dc9255e0deb128cc2747a00ee0a026ad

Contents?: true

Size: 1.49 KB

Versions: 31

Compression:

Stored size: 1.49 KB

Contents

module Krikri::Util
  module ExtendedDateParser
    module_function

    def parse(date_str)
      date = Date.edtf(date_str)
      date ||= partial_edtf(date_str)
      date ||= decade_hyphen(date_str)
      date ||= month_year(date_str)
      date ||= decade_s(date_str)
      date ||= hyphenated_partial_range(date_str)
      date ||= begin
                 Date.parse(date_str)
               rescue ArgumentError
                 nil
               end
    end

    def range_match(str)
      regexp = %r{
        ([\d\-\.xu\?\~]*\d{4}[\d\-\.xu\?\~]*)
        \s*[-\.]+\s*
        ([\d\-\.xu\?\~]*\d{4}[\d\.\-xu\?\~]*)
      }x
      regexp.match(str) do |m|
        [m[1], m[2]]
      end
    end

    ##
    # e.g. 01-2045
    def month_year(str)
      /(\d{2})-(\d{4})/.match(str) do |m|
        Date.edtf("#{m[2]}-#{m[1]}")
      end
    end

    ##
    # e.g. 1990-92
    def hyphenated_partial_range(str)
      /^(\d{2})(\d{2})-(\d{2})$/.match(str) do |m|
        Date.edtf("#{m[1]}#{m[2]}/#{m[1]}#{m[3]}")
      end
    end

    ##
    # e.g. 1970-08-01/02 or 1970-12/10
    def partial_edtf(str)
      /^(\d{4}(-\d{2})*)-(\d{2})\/(\d{2})$/.match(str) do |m|
        Date.edtf("#{m[1]}-#{m[3]}/#{m[1]}-#{m[4]}")
      end
    end

    ##
    # e.g. 1990s
    def decade_s(str)
      /^(\d{3})0s$/.match(str) do |m|
        Date.edtf("#{m[1]}x")
      end
    end

    ##
    # e.g. 199-
    def decade_hyphen(str)
      /^(\d{3})-$/.match(str) do |m|
        Date.edtf("#{m[1]}x")
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
krikri-0.5.7 lib/krikri/util/extended_date_parser.rb~
krikri-0.5.6 lib/krikri/util/extended_date_parser.rb~
krikri-0.5.5 lib/krikri/util/extended_date_parser.rb~
krikri-0.4.0 lib/krikri/util/extended_date_parser.rb~
krikri-0.3.3 lib/krikri/util/extended_date_parser.rb~
krikri-0.3.2 lib/krikri/util/extended_date_parser.rb~
krikri-0.3.1 lib/krikri/util/extended_date_parser.rb~
krikri-0.1.3 lib/krikri/util/extended_date_parser.rb~
krikri-0.1.2 lib/krikri/util/extended_date_parser.rb~
krikri-0.1.1 lib/krikri/util/extended_date_parser.rb~
krikri-0.1.0 lib/krikri/util/extended_date_parser.rb~