class Dates::DateFormatter
Extracts a date in seconds past UNIX epoc from a string date. The result can be used for other date operations. Converts from dd-mmm-yy and similar formats as commonly found in csv files
Constants
- MONTHS
A hash of text months and their corresponding month number
Public Instance Methods
format_date(date)
click to toggle source
# File lib/docfolio/paragraph_modules/dates.rb, line 8 def format_date(date) day, month, year = components(date) begin Time.new(year, month, day).to_i rescue ArgumentError => e print_argument_error_msg(e) return nil rescue => e raise e end end
Private Instance Methods
components(date)
click to toggle source
splits date into is component day month and time
# File lib/docfolio/paragraph_modules/dates.rb, line 32 def components(date) date = date.split('-') day = date[0].to_i month = convert_month_to_number(date[1]) year = date[2].to_i if year < 100 # no century year > Time.now.year % 1000 ? century = 1900 : century = 2000 year += century end [day, month, year] end
convert_month_to_number(month)
click to toggle source
Takes a text month to its corresponding number, case insensitive @param [String] month Month of year in text @return [Integer] Number of month in calander e.g. Feb is 2
# File lib/docfolio/paragraph_modules/dates.rb, line 75 def convert_month_to_number(month) return month.to_i if month.to_i > 0 # already a number month = month.downcase MONTHS[month] end
print_argument_error_msg(e)
click to toggle source
# File lib/docfolio/paragraph_modules/dates.rb, line 22 def print_argument_error_msg(e) puts "\n#{e.to_s.upcase}" puts "date : #{date.inspect}" puts "day : #{day}" puts "month : #{month}" puts "year : #{year}" puts e.backtrace end