Module: Krikri::Util::ExtendedDateParser

Defined in:
lib/krikri/util/extended_date_parser.rb

Class Method Summary collapse

Class Method Details

.decade_hyphen(str) ⇒ Object

e.g. 199-


141
142
143
144
145
# File 'lib/krikri/util/extended_date_parser.rb', line 141

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

.decade_s(str) ⇒ Object

e.g. 1990s


133
134
135
136
137
# File 'lib/krikri/util/extended_date_parser.rb', line 133

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

.hyphenated_partial_range(str) ⇒ Object

e.g. 1990-92


117
118
119
120
121
# File 'lib/krikri/util/extended_date_parser.rb', line 117

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

.month_year(str) ⇒ Object

e.g. 01-2045


109
110
111
112
113
# File 'lib/krikri/util/extended_date_parser.rb', line 109

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

.parse(date_str, allow_interval = false) ⇒ Date, ...

Attempts to parse a string into a valid EDTF or `Date` format.

- Attempts to split `#providedLabel` on '-', '/', '..', 'to', 'until', and
  looks for EDTF and `Date.parse` patterns on either side, setting them to
  `#begin` and `#end`. Both split and unsplit dates are parsed as follows:
- Attempts to parse `#providedLabel` as an EDTF interval and populates
  begin and end with their respective values.
- Attempts to match to a number of regular expressions which specify
  ranges informally.
- Attempts to parse `#providedLabel` as a single date value with
  `Date.parse` and enters that value to both `#begin` and `#end`.

Parameters:

  • date_str (String)

    a string which may contain a date range

  • allow_interval (Boolean) (defaults to: false)

    a flag specifing whethe to use #range_match to look for range values.

Returns:

  • (Date, EDTF::Epoch, EDTF::Interval, nil)

    the date parsed or nil


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/krikri/util/extended_date_parser.rb', line 23

def parse(date_str, allow_interval = false)
  date_str.strip!
  date_str.gsub!(/\s+/, ' ')
  date = parse_interval(date_str) if allow_interval
  date ||= parse_m_d_y(date_str)
  date ||= Date.edtf(date_str.gsub('.', '-'))
  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 ||= parse_date(date_str)
  date || nil
end

.parse_date(*args) ⇒ Date?

Runs `Date#parse`; if arguments are invalid (as with an invalid date string) returns `nil`.

Returns:

  • (Date, nil)

    the parsed date or nil

See Also:

  • Date#parse

84
85
86
87
88
89
90
# File 'lib/krikri/util/extended_date_parser.rb', line 84

def parse_date(*args)
  begin
    Date.parse(*args)
  rescue ArgumentError
    nil
  end
end

.parse_interval(str) ⇒ ETDF::Interval?

Creates an EDTF::Interval from a string

Parameters:

  • str (String)

    a string which may contain a date range

Returns:

  • (ETDF::Interval, nil)

    an EDTF object representing a date range or nil if none can be found

See Also:

  • #range_match

66
67
68
69
70
71
72
73
74
75
76
# File 'lib/krikri/util/extended_date_parser.rb', line 66

def parse_interval(str)
  match = range_match(str)
  return nil if match.nil?

  begin_date, end_date = match.map { |date| parse(date) || :unknown }

  begin_date = begin_date.first if begin_date.respond_to? :first
  end_date = end_date.last if end_date.respond_to? :last

  EDTF::Interval.new(begin_date, end_date)
end

.parse_m_d_y(value) ⇒ Date?

Runs `Date#strptime` with '%m-%d-%Y'; if arguments are invalid (as with an invalid date string) returns `nil`.

Parameters:

  • value (String)

    the string to parse

Returns:

  • (Date, nil)

    the parsed date or nil

See Also:

  • Date#strptime

99
100
101
102
103
104
105
# File 'lib/krikri/util/extended_date_parser.rb', line 99

def parse_m_d_y(value)
  begin
    Date.strptime(value.gsub(/[^0-9]/, '-'), '%m-%d-%Y')
  rescue ArgumentError
    nil
  end
end

.partial_edtf(str) ⇒ Object

e.g. 1970-08-01/02 or 1970-12/10


125
126
127
128
129
# File 'lib/krikri/util/extended_date_parser.rb', line 125

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

.range_match(str) ⇒ Array(String)

Matches a wide variety of date ranges separated by '..' or '-'

Parameters:

  • str (String)

    a string which may contain a date range

Returns:

  • (Array(String))

    the begining and ending dates of an identified range


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/krikri/util/extended_date_parser.rb', line 44

def range_match(str)
  str = str.gsub('to', '-').gsub('until', '-')
  regexp = %r{
    ([a-zA-Z]{0,3}\s?[\d\-\/\.xu\?\~a-zA-Z]*,?\s?
    \d{3}[\d\-xs][s\d\-\.xu\?\~]*)
    \s*[-\.]+\s*
    ([a-zA-Z]{0,3}\s?[\d\-\/\.xu\?\~a-zA-Z]*,?\s?
    \d{3}[\d\-xs][s\d\-\.xu\?\~]*)
  }x
  regexp.match(str) do |m|
    [m[1], m[2]]
  end
end