Sha256: 00b8993a759f73b593d7b56d18b7bdd7434d5b8954523859b1e291efb3ff5efb

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require "timetwister/version"
require "timetwister/parser"
require "timetwister/utilities"

module Timetwister

  def self.parse(str, options={})

    out = []
    str = rearrange_conjunctions(str)

    str.split(';').each do |semi|
      semi.split(/\sand\s|\s\&\s/i).each do |conj|

        # check for dates of form "Month Day(-Day), Year" before splitting on commas
        # (removes certainty markers as to not jam the regex)
        if Utilities.replace_ordinals(conj).gsub(/[\?\[\]]/, '').match(/[a-z]*\.?\s[0-9]{1,2}(\s?-[0-9]{1,2})?\,\s[0-9]{4}/i)
          out << Parser.string_to_dates(conj, options)
        else
          conj.split(',').each do |comma|
            out << Parser.string_to_dates(comma, options)
          end
        end
      end
    end

    return out
  end

  # sometimes years are considered implicit in complex dates
  # e.g. "1900 January & February"
  # this rearranges them into complete atomic dates
  def self.rearrange_conjunctions(str)

    # if we don't have a complete year and month, call it quits
    if !str.match(/[0-9]{4}/) || !str.match(/[a-z]+/i)
      return str
    end

    year = str.match(/[0-9]{4}/)[0]
    month = str.match(/[a-z]+/i)[0]
    return_str = ''

    if month.eql?('and')
      return str
    end

    str.split(/\sand\s|\s\&\s/).each do |conj|

      if !conj.match(/[0-9]{4}/)
        conj << ' ' + year
      end

      if !conj.match(/[a-z]+/i)
        conj = month + ' ' + conj
      end

      return_str << ' and ' + conj
    end

    return return_str.sub(/\sand\s/, '')

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
timetwister-0.2.5 lib/timetwister.rb