Module: Emendate::DateUtils
- Extended by:
- DateUtils
- Included in:
- DatePartTagger, DateSegmenter, DateUtils, FormatStandardizer, MonthAlpha, MonthDayAnalyzer, ShortYearHandler, UncertaintyDigits
- Defined in:
- lib/emendate/date_utils.rb
Instance Method Summary collapse
-
#expand_shorter_digits(year, num) ⇒ Integer
The shorter digits, expanded to follow the pattern of the given `year` value.
-
#is_range?(year, num) ⇒ Boolean
True if it's a possible range and it can't be a month/season.
- #max_season ⇒ 24, ...
- #min_season ⇒ NilClass, 21
- #month_abbr_literal(month) ⇒ Integer
- #month_literal(month) ⇒ Integer
-
#possible_range?(year, num) ⇒ Boolean
rubocop:todo Layout/LineLength determines whether the number following a year could be the end of a range beginning with that year rubocop:enable Layout/LineLength 2020-10 – false, the 10 has to be October rubocop:todo Layout/LineLength 2020-21 – true, the 21 could indicate 2021 as end of range, OR this could mean Spring 2020 rubocop:enable Layout/LineLength.
- #valid_date?(yr, mth, day) ⇒ TrueClass, FalseClass
- #valid_day?(int) ⇒ TrueClass, FalseClass
- #valid_month?(int) ⇒ TrueClass, FalseClass
- #valid_month_or_season?(int) ⇒ TrueClass, FalseClass
- #valid_season?(int) ⇒ TrueClass, FalseClass
-
#valid_year?(str) ⇒ Boolean
rubocop:todo Layout/LineLength rubocop:enable Layout/LineLength.
Instance Method Details
#expand_shorter_digits(year, num) ⇒ Integer
Returns the shorter digits, expanded to follow the pattern of the given `year` value. Examples: returns 2010 for 2020-10; returns 1999 for 1998-9.
13 14 15 16 17 18 |
# File 'lib/emendate/date_utils.rb', line 13 def (year, num) year_s = year.literal.to_s digits = num.literal.to_s.rjust(num.digits, "0") diff = year_s.length - digits.length - 1 "#{year_s[0..diff]}#{digits}".to_i end |
#is_range?(year, num) ⇒ Boolean
Returns true if it's a possible range and it can't be a month/season.
24 25 26 27 28 |
# File 'lib/emendate/date_utils.rb', line 24 def is_range?(year, num) return false if valid_month_or_season?(num.literal) possible_range?(year, num) end |
#max_season ⇒ 24, ...
33 34 35 36 37 |
# File 'lib/emendate/date_utils.rb', line 33 def max_season return 24 if Emendate..max_month_number_handling == :edtf_level_1 41 if Emendate..max_month_number_handling == :edtf_level_2 end |
#min_season ⇒ NilClass, 21
41 42 43 |
# File 'lib/emendate/date_utils.rb', line 41 def min_season 21 unless Emendate..max_month_number_handling == :months end |
#month_abbr_literal(month) ⇒ Integer
47 48 49 50 51 52 53 54 |
# File 'lib/emendate/date_utils.rb', line 47 def month_abbr_literal(month) lookup = {} Date::ABBR_MONTHNAMES.compact.map(&:downcase).each_with_index do |str, i| lookup[str] = i + 1 end lookup["sept"] = 9 lookup[month.downcase.strip.delete_suffix(".")] end |
#month_literal(month) ⇒ Integer
58 59 60 |
# File 'lib/emendate/date_utils.rb', line 58 def month_literal(month) Date::MONTHNAMES.map { |mth| mth&.downcase }.index(month.downcase) end |
#possible_range?(year, num) ⇒ Boolean
rubocop:todo Layout/LineLength determines whether the number following a year could be the end of a range beginning with that year rubocop:enable Layout/LineLength 2020-10 – false, the 10 has to be October rubocop:todo Layout/LineLength 2020-21 – true, the 21 could indicate 2021 as end of range, OR this could mean Spring 2020 rubocop:enable Layout/LineLength
71 72 73 74 75 76 |
# File 'lib/emendate/date_utils.rb', line 71 def possible_range?(year, num) = (year, num) return false unless valid_year?() .to_i > year.literal end |
#valid_date?(yr, mth, day) ⇒ TrueClass, FalseClass
83 84 85 86 87 88 89 |
# File 'lib/emendate/date_utils.rb', line 83 def valid_date?(yr, mth, day) Date.new(yr.literal, mth.literal, day.literal) rescue Date::Error valid_english_date?(yr, mth, day) else true end |
#valid_day?(int) ⇒ TrueClass, FalseClass
94 95 96 97 98 99 100 101 |
# File 'lib/emendate/date_utils.rb', line 94 def valid_day?(int) unless int.is_a?(Integer) raise StandardError, "string passed instead of integer" end int >= 1 && int <= 31 end |
#valid_month?(int) ⇒ TrueClass, FalseClass
121 122 123 124 125 126 127 128 |
# File 'lib/emendate/date_utils.rb', line 121 def valid_month?(int) unless int.is_a?(Integer) raise StandardError, "string passed instead of integer" end int > 0 && int < 13 end |
#valid_month_or_season?(int) ⇒ TrueClass, FalseClass
133 134 135 136 137 138 139 140 |
# File 'lib/emendate/date_utils.rb', line 133 def valid_month_or_season?(int) unless int.is_a?(Integer) raise StandardError, "string passed instead of integer" end valid_month?(int) || valid_season?(int) end |
#valid_season?(int) ⇒ TrueClass, FalseClass
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/emendate/date_utils.rb', line 145 def valid_season?(int) unless int.is_a?(Integer) raise StandardError, "string passed instead of integer" end return false if Emendate..max_month_number_handling == :months int >= min_season && int <= max_season end |
#valid_year?(str) ⇒ Boolean
Years shouldn't have to be fewer than 4 digits - check/fix this assumption
rubocop:todo Layout/LineLength rubocop:enable Layout/LineLength
159 160 161 162 |
# File 'lib/emendate/date_utils.rb', line 159 def valid_year?(str) str = str.to_s unless str.is_a?(String) str.length <= 4 end |