Module: Timetwister

Defined in:
lib/timetwister.rb,
lib/timetwister/version.rb

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.parse(str, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/timetwister.rb', line 7

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) && \
        !Utilities.replace_ordinals(conj).match(/[0-9]{4}\s[a-z]*\.?\s[0-9]{1,2}/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

.rearrange_conjunctions(str) ⇒ Object

sometimes years are considered implicit in complex dates e.g. “1900 January & February” this rearranges them into complete atomic dates



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/timetwister.rb', line 34

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