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

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.

Parameters:

  • year (String)

    the known full year

  • digits (String)

    the following shorter digit string to be expanded

Returns:

  • (Integer)

    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 expand_shorter_digits(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.

Parameters:

  • year (Segment)

    representing known year

  • num (Segment)

    representing ambiguous number

Returns:

  • (Boolean)

    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_season24, ...

Returns:

  • (24)

    when max_month_number_handling == :edtf_level_1

  • (41)

    when max_month_number_handling == :edtf_level_2

  • (NilClass)

    when max_month_number_handling == :months



33
34
35
36
37
# File 'lib/emendate/date_utils.rb', line 33

def max_season
  return 24 if Emendate.options.max_month_number_handling == :edtf_level_1

  41 if Emendate.options.max_month_number_handling == :edtf_level_2
end

#min_seasonNilClass, 21

Returns:

  • (NilClass)

    when max_month_number_handling == :months

  • (21)

    otherwise



41
42
43
# File 'lib/emendate/date_utils.rb', line 41

def min_season
  21 unless Emendate.options.max_month_number_handling == :months
end

#month_abbr_literal(month) ⇒ Integer

Parameters:

  • month (String)

    month abbreviation

Returns:

  • (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

Parameters:

  • month (String)

    full month name

Returns:

  • (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

Parameters:

  • year (Segment)

    representing known year

  • num (Segment)

    representing ambiguous number

Returns:

  • (Boolean)


71
72
73
74
75
76
# File 'lib/emendate/date_utils.rb', line 71

def possible_range?(year, num)
  expanded = expand_shorter_digits(year, num)
  return false unless valid_year?(expanded)

  expanded.to_i > year.literal
end

#valid_date?(yr, mth, day) ⇒ TrueClass, FalseClass

Parameters:

  • yr (Segment)
  • mth (Segment)
  • day (Segment)

Returns:

  • (TrueClass)

    if segment literals can be converted to a valid date

  • (FalseClass)

    otherwise



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

Parameters:

  • int (Integer)

Returns:

  • (TrueClass)

    if 1 to 31

  • (FalseClass)

    otherwise



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

Parameters:

  • int (Integer)

Returns:

  • (TrueClass)

    if 1 to 12

  • (FalseClass)

    otherwise



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

Parameters:

  • int (Integer)

Returns:



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

Parameters:

  • int (Integer)

Returns:

  • (TrueClass)

    if is a valid EDTF season/quarter/semester indicator

  • (FalseClass)

    otherwise



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.options.max_month_number_handling == :months

  int >= min_season && int <= max_season
end

#valid_year?(str) ⇒ Boolean

TODO:

Years shouldn't have to be fewer than 4 digits - check/fix this assumption

rubocop:todo Layout/LineLength rubocop:enable Layout/LineLength

Returns:

  • (Boolean)


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