Commit 67a7c282a2abfb48f2e0458109056301db24f8ed

Authored by Antonio Terceiro
1 parent fbc702c7

ActionItem916: coping with other date formats

Showing 1 changed file with 17 additions and 2 deletions   Show diff stats
db/migrate/059_add_birth_date_to_person.rb
... ... @@ -18,8 +18,14 @@ class AddBirthDateToPerson < ActiveRecord::Migration
18 18 date_string = date_string[0..-3] + (year > (Date.today.year - 2000) ? year + 1900 : year + 2000).to_s
19 19 end
20 20  
21   - date_string =~ (/(\d+)[^\d]+(\d+)[^\d]+(\d+)/)
22   - Date.new($3.to_i, $2.to_i, $1.to_i)
  21 + if ! ((date_string =~ /(\d+)[^\d]+(\d+)[^\d]+(\d+)/) || (date_string =~ /^(\d\d)(\d\d)(\d\d\d\d)$/))
  22 + return nil
  23 + end
  24 + begin
  25 + Date.new($3.to_i, $2.to_i, $1.to_i)
  26 + rescue Exception => e
  27 + nil
  28 + end
23 29 end
24 30 end
25 31  
... ... @@ -106,6 +112,15 @@ if $PROGRAM_NAME == __FILE__
106 112 assert_nil date
107 113 end
108 114  
  115 + should 'convert date without separators' do
  116 + date = AddBirthDateToPerson::ConvertDates.convert('27071977')
  117 + assert_equal [ 1977, 07, 27] , [date.year, date.month, date.day]
  118 + end
  119 +
  120 + should 'not try to create invalid date' do
  121 + assert_nil AddBirthDateToPerson::ConvertDates.convert('70/05/1987')
  122 + end
  123 +
109 124 end
110 125  
111 126 end
... ...