Module: ParseDate::IntFromString

Included in:
ParseDate
Defined in:
lib/parse_date/int_from_string.rb

Overview

Parse (Year) Integers from Date Strings

Instance Method Summary collapse

Instance Method Details

#earliest_year(date_str) ⇒ Integer?

earliest year as Integer if we can parse one from date_str

e.g. if 17uu, result is 1700

NOTE: if we have a x/x/yy or x-x-yy pattern (the only 2 digit year patterns

found in our actual date strings in stanford-mods records), then
we use 20 as century digits unless it is greater than current year:
1/1/17  ->  2017
1/1/27  ->  1927

Returns:

  • (Integer, nil)

    Integer year if we could parse one, nil otherwise



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/parse_date/int_from_string.rb', line 14

def earliest_year(date_str)
  return unless date_str && !date_str.empty?
  return if date_str == '0000-00-00' # shpc collection has these useless dates

  # B.C. first (match longest string first)
  bc_result = earliest_year_bc_parsing(date_str)
  return bc_result if bc_result

  result = earliest_year_parsing(date_str)
  return result if result

  # try removing brackets between digits in case we have 169[5] or [18]91
  no_brackets = remove_brackets(date_str)
  earliest_year(no_brackets) if no_brackets
end

#latest_year(date_str) ⇒ Integer?

latest year as Integer if we can parse one from date_str

e.g. if 17uu, result is 1799

NOTE: if we have a x/x/yy or x-x-yy pattern (the only 2 digit year patterns

found in our actual date strings in stanford-mods records), then
we use 20 as century digits unless it is greater than current year:
1/1/17  ->  2017
1/1/27  ->  1927

Returns:

  • (Integer, nil)

    Integer year if we could parse one, nil otherwise



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/parse_date/int_from_string.rb', line 38

def latest_year(date_str)
  return unless date_str && !date_str.empty?
  return if date_str == '0000-00-00' # shpc collection has these useless dates

  # B.C. first (match longest string first)
  bc_result = latest_year_bc_parsing(date_str)
  return bc_result if bc_result

  result = latest_year_parsing(date_str)
  return result if result

  # try removing brackets between digits in case we have 169[5] or [18]91
  no_brackets = remove_brackets(date_str)
  latest_year(no_brackets) if no_brackets
end

#year_int_valid?(year) ⇒ Boolean

true if the year is between -9999 and (current year + 1), inclusive

Returns:

  • (Boolean)

    true if the year is between -999 and (current year + 1); false otherwise



56
57
58
59
60
# File 'lib/parse_date/int_from_string.rb', line 56

def year_int_valid?(year)
  return false unless year.is_a? Integer

  (-10000 < year.to_i) && (year < Date.today.year + 2)
end