module CyymmddConverter

Constants

VERSION

Public Instance Methods

convert_from_cyymmdd() click to toggle source
# File lib/cyymmdd_converter.rb, line 5
def convert_from_cyymmdd
  cleared_string = self

  # Pass missing zero\one to the beginning
  unless self['.'].nil?
    temp = self.split('.')
    cleared_string = temp.reverse.join if temp.last == '0' && temp.first.length == 6
  end
  cleared_string = cleared_string.split('.').first

  # Validate input date, return false if didn't pass
  return false unless cleared_string =~ /\A[1|0]{1}(0[1-9]|[10-99]){1,2}(0[1-9]|1[0-2]){1}(0[1-9]|1[0-9]|2[0-9]|3[0-2]){1}\z/

  # Remove first digit (century)
  begin
    converted_date = Date.parse cleared_string.slice(1..-1)
  rescue Exception => e
    return false
  end

  # Subsctract 100 years if we've got something after 2000 year, but century mark was zero
  converted_date = converted_date - 100.years if cleared_string[0] == '0' && converted_date.year >= 2000

  # Return converted date
  converted_date
end
convert_to_cyymmdd() click to toggle source
# File lib/cyymmdd_converter.rb, line 34
def convert_to_cyymmdd
  converted_date = self.strftime('%y%m%d')

  # calculate century
  century = self.year >= 2000 ? 1 : 0

  "#{century.to_s}#{converted_date}"
end