module Date

Modify parsing methods to handle accountant date format correctly.

Constants

ACCOUNTANT_SLASH_DATE_RE

American date format detected by the library.

Public Class Methods

_parse(string, comp=true) click to toggle source

Transform accountant dates into ISO dates before parsing.

# File lib/accountant_date.rb, line 13
def _parse(string, comp=true)
  return string if string.nil? or string == ''
  _parse_without_accountant_date(convert_accountant_to_iso(string), comp)
end
_parse_without_accountant_date(string, comp=true)

Alias for stdlib Date._parse

Alias for: _parse
parse(string, comp=true) click to toggle source

Transform accountant dates into ISO dates before parsing.

# File lib/accountant_date.rb, line 23
def parse(string, comp=true)
  return string if string.nil? or string == ''
  parse_without_accountant_date(convert_accountant_to_iso(string), comp)
end
parse_without_accountant_date(string, comp=true)

Alias for stdlib Date.parse

Alias for: parse

Private Class Methods

convert_accountant_to_iso(string) click to toggle source

Transform accountant date fromat into ISO format.

# File lib/accountant_date.rb, line 32
def convert_accountant_to_iso(string)
  if string.count('/') == 1
    string.sub(ACCOUNTANT_SLASH_DATE_RE){ |m| "#$2-#$1-1" }
  elsif string.length <= 8 and string =~ /\d{#{string.length}}/
    # If the whole string is digits
    convert_date_no_slashes_to_iso(string)
  else
    string
  end
end
convert_date_no_slashes_to_iso(string) click to toggle source

Transform date to have slashes

# File lib/accountant_date.rb, line 44
def convert_date_no_slashes_to_iso(string)
  case string.length
  when 8 # mmddyyyy
    "#{string[4,4]}-#{string[0,2]}-#{string[2,2]}"
  when 7 # mddyyyy
    "#{string[4,4]}-#{string[0]}-#{string[1,2]}"
  when 6 # mmddyy
    "#{string[4,2]}-#{string[0,2]}-#{string[2,2]}"
  when 5 # mddyy
    "#{string[3,2]}-#{string[0]}-#{string[1,2]}"
  when 4 # mmyy
    "#{string[2,2]}-#{string[0,2]}-1"
  when 3 # myy
    "#{string[1,2]}-#{string[0]}-1"
  else
    string
  end
end