Module: Timeliness::Helpers

Included in:
Format
Defined in:
lib/timeliness/helpers.rb

Instance Method Summary collapse

Instance Method Details

#abbr_month_namesObject



40
41
42
# File 'lib/timeliness/helpers.rb', line 40

def abbr_month_names
  i18n_loaded? ? I18n.t('date.abbr_month_names') : Date::ABBR_MONTHNAMES
end

#full_hour(hour, meridian) ⇒ Object

Helper methods used in format component processing. See Definitions.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/timeliness/helpers.rb', line 7

def full_hour(hour, meridian)
  hour = hour.to_i
  return hour if meridian.nil?

  meridian.delete!('.')
  meridian.downcase!

  if meridian == 'am'
    raise(ArgumentError) if hour == 0 || hour > 12
    hour == 12 ? 0 : hour
  else
    hour == 12 ? hour : hour + 12
  end
end

#i18n_loaded?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/timeliness/helpers.rb', line 53

def i18n_loaded?
  defined?(I18n)
end

#microseconds(usec) ⇒ Object



44
45
46
# File 'lib/timeliness/helpers.rb', line 44

def microseconds(usec)
  (".#{usec}".to_f * 1_000_000).to_i
end

#month_index(month) ⇒ Object



31
32
33
34
# File 'lib/timeliness/helpers.rb', line 31

def month_index(month)
  return month.to_i if month.match?(/\d/)
  (month.length > 3 ? month_names : abbr_month_names).index { |str| month.casecmp?(str) }
end

#month_namesObject



36
37
38
# File 'lib/timeliness/helpers.rb', line 36

def month_names
  i18n_loaded? ? I18n.t('date.month_names') : Date::MONTHNAMES
end

#offset_in_seconds(offset) ⇒ Object



48
49
50
51
# File 'lib/timeliness/helpers.rb', line 48

def offset_in_seconds(offset)
  offset =~ /^([-+])?(\d{2}):?(\d{2})/
  ($1 == '-' ? -1 : 1) * ($2.to_f * 3600 + $3.to_f * 60)
end

#unambiguous_year(year) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/timeliness/helpers.rb', line 22

def unambiguous_year(year)
  if year.length <= 2
    century = Time.now.year.to_s[0..1].to_i
    century -= 1 if year.to_i >= Timeliness.configuration.ambiguous_year_threshold
    year = "#{century}#{year.rjust(2,'0')}"
  end
  year.to_i
end