From 67a7c282a2abfb48f2e0458109056301db24f8ed Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Wed, 28 Jan 2009 12:19:00 -0300 Subject: [PATCH] ActionItem916: coping with other date formats --- db/migrate/059_add_birth_date_to_person.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/db/migrate/059_add_birth_date_to_person.rb b/db/migrate/059_add_birth_date_to_person.rb index 6f3bed9..e552362 100644 --- a/db/migrate/059_add_birth_date_to_person.rb +++ b/db/migrate/059_add_birth_date_to_person.rb @@ -18,8 +18,14 @@ class AddBirthDateToPerson < ActiveRecord::Migration date_string = date_string[0..-3] + (year > (Date.today.year - 2000) ? year + 1900 : year + 2000).to_s end - date_string =~ (/(\d+)[^\d]+(\d+)[^\d]+(\d+)/) - Date.new($3.to_i, $2.to_i, $1.to_i) + if ! ((date_string =~ /(\d+)[^\d]+(\d+)[^\d]+(\d+)/) || (date_string =~ /^(\d\d)(\d\d)(\d\d\d\d)$/)) + return nil + end + begin + Date.new($3.to_i, $2.to_i, $1.to_i) + rescue Exception => e + nil + end end end @@ -106,6 +112,15 @@ if $PROGRAM_NAME == __FILE__ assert_nil date end + should 'convert date without separators' do + date = AddBirthDateToPerson::ConvertDates.convert('27071977') + assert_equal [ 1977, 07, 27] , [date.year, date.month, date.day] + end + + should 'not try to create invalid date' do + assert_nil AddBirthDateToPerson::ConvertDates.convert('70/05/1987') + end + end end -- libgit2 0.21.2