Module: Utilities::Dates

Defined in:
lib/utilities/dates.rb

Constant Summary collapse

ROMAN_MONTHS =
i ii iii iv v vi vii viii ix x xi xii
SHORT_MONTH_FILTER =

TODO: Write unit tests Concept from from www.rdoc.info/github/inukshuk/bibtex-ruby/master/BibTeX/Entry Converts integers, month names, or roman numerals, regardless of class to three letter symbols.

SHORT_MONTH_FILTER[1]         # => :jan
SHORT_MONTH_FILTER['JANUARY'] # => :jan
SHORT_MONTH_FILTER['i']       # => :jan
SHORT_MONTH_FILTER['I']       # => :jan
SHORT_MONTH_FILTER['foo']     # => 'foo':
Hash.new do |h,k|
  v = k.to_s.strip
  if v =~ /^(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i
    h[k] = v[0,3].downcase.to_sym
  else
    i = nil
    if v =~ /^\d\d?$/
      i = v.to_i
    elsif ROMAN_MONTHS.include?(v.downcase.to_sym)
      i = ROMAN_MONTHS.index(v.downcase.to_sym) + 1
    end 
    if !i.nil? && i > 0 && i < 13
      h[k] = DateTime.new(1, i, 1).strftime("%b").downcase.to_sym
    else # return the value passed if it doesn't match
      k
    end
  end
end

Class Method Summary collapse

Class Method Details

.nomenclature_date(day = nil, month = nil, year = nil) ⇒ Object

@return a UTC time (Uses Time instead of Date so that it can be saved as a UTC object -

See http://www.ruby-doc.org/core-2.0.0/Time.html)
Returns nomenclature_date based on computation of the values of :year, :month, :day.
 if :year is empty, return nil
 if :month is empty, returns 12/31/:year
 if :day is empty, returns the last day of the month


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/utilities/dates.rb', line 40

def self.nomenclature_date(day = nil, month = nil, year = nil)
  if year.nil?
    nil
  elsif month.nil?
    Time.utc(year, 12, 31)
  elsif day.nil?
    tmp = Time.utc(year, month)
    if tmp.month == 12 # want the last day of december
      Time.utc(year, 12, 31)
    else # time + 1 month - 1 day (60 sec * 60 min *24 hours)
      Time.utc(year, tmp.month + 1) - 86400
    end
  else
    Time.utc(year, month, day)
  end
end