Module: ParseDate::IntFromString
- Included in:
- ParseDate
- Defined in:
- lib/parse_date/int_from_string.rb
Overview
Parse (Year) Integers from Date Strings
Class Method Summary collapse
-
.earliest_year(date_str) ⇒ Integer?
earliest year as Integer if we can parse one from date_str e.g.
-
.latest_year(date_str) ⇒ Integer?
latest year as Integer if we can parse one from date_str e.g.
-
.year_int_valid?(year) ⇒ Boolean
true if the year is between -9999 and (current year + 1), inclusive.
Class 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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/parse_date/int_from_string.rb', line 18 def self.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) return ParseDate.send(:earliest_century_bc, date_str) if date_str.match(YY_YY_CENTURY_BC_REGEX) return ParseDate.send(:between_bc_earliest_year, date_str) if date_str.match(BETWEEN_Yn_AND_Yn_BC_REGEX) return ParseDate.send(:year_int_for_bc, date_str) if date_str.match(YEAR_BC_REGEX) result ||= ParseDate.send(:between_earliest_year, date_str) result ||= ParseDate.send(:hyphen_4digit_earliest_year, date_str) result ||= ParseDate.send(:negative_first_four_digits, date_str) result ||= ParseDate.send(:first_four_digits, date_str) result ||= ParseDate.send(:year_from_mm_dd_yy, date_str) result ||= ParseDate.send(:first_year_for_decade, date_str) # 198x or 201x result ||= ParseDate.send(:first_year_for_century, date_str) # includes BC result ||= ParseDate.send(:year_for_early_numeric, date_str) unless result # try removing brackets between digits in case we have 169[5] or [18]91 no_brackets = ParseDate.send(:remove_brackets, date_str) return earliest_year(no_brackets) if no_brackets end result.to_i if result && year_int_valid?(result.to_i) 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/parse_date/int_from_string.rb', line 51 def self.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) return ParseDate.send(:last_year_mult_centuries_bc, date_str) if date_str.match(YY_YY_CENTURY_BC_REGEX) return ParseDate.send(:between_bc_latest_year, date_str) if date_str.match(BETWEEN_Yn_AND_Yn_BC_REGEX) return ParseDate.send(:last_year_for_bc_century, date_str) if date_str.match(BC_CENTURY_REGEX) return ParseDate.send(:year_int_for_bc, date_str) if date_str.match(BC_REGEX) result ||= ParseDate.send(:between_latest_year, date_str) result ||= ParseDate.send(:hyphen_4digit_latest_year, date_str) result ||= ParseDate.send(:hyphen_2digit_latest_year, date_str) result ||= ParseDate.send(:yyuu_after_hyphen, date_str) result ||= ParseDate.send(:year_after_or, date_str) result ||= ParseDate.send(:negative_4digits_after_hyphen, date_str) result ||= ParseDate.send(:negative_first_four_digits, date_str) result ||= ParseDate.send(:last_year_for_0s_decade, date_str) result ||= ParseDate.send(:first_four_digits, date_str) result ||= ParseDate.send(:year_from_mm_dd_yy, date_str) result ||= ParseDate.send(:last_year_for_decade, date_str) # 198x or 201x result ||= ParseDate.send(:last_year_mult_centuries, date_str) # nth-nth century result ||= ParseDate.send(:last_year_for_century, date_str) result ||= ParseDate.send(:last_year_for_early_numeric, date_str) unless result # try removing brackets between digits in case we have 169[5] or [18]91 no_brackets = ParseDate.send(:remove_brackets, date_str) return earliest_year(no_brackets) if no_brackets end result.to_i if result && year_int_valid?(result.to_i) end |
.year_int_valid?(year) ⇒ Boolean
true if the year is between -9999 and (current year + 1), inclusive
85 86 87 88 89 |
# File 'lib/parse_date/int_from_string.rb', line 85 def self.year_int_valid?(year) return false unless year.is_a? Integer (-10000 < year.to_i) && (year < Date.today.year + 2) end |