I'm trying to seed some data into a Rails 3 app that I'm developing, using the db/seed.rb file and the rake db:seed command.
The data I'm seeding involves a database table called Meeting that has three columns: string title, datetime startDate, datetime endDate. The datetimes I'm trying to insert are in "mm/dd/yyyy hh:mm" format -- eg. "01/02/2003 13:23" = January 2, 2003 1:23 PM. However, the DateTime.parse() is consistently erroring out with an "invalid date" error -- as it tries to parse the dates in "dd/mm/yyyy" format.
From my Googling, I've been led to believe that when parsing DateTime objects the compiler looks at the string that's passed in and does some hasty pattern matching and then maps "mm/dd/yyyy" and "dd-mm-yyyy" accordingly per American/British(etc) standards based on whether a slash or a dash was used as a seperator. This doesn't seem to be the case, however, and I'm wondering why. And how to fix it.
This is how I'm seeding the Meetings -- the first one parses properly, the second one fails.
Meeting.create([
{
:title => "This meeting parses fine",
:startDate => DateTime.parse("09/01/2009 17:00"),
:endDate => DateTime.parse("09/01/2009 19:00")
},
{
:title => "This meeting errors out",
:startDate => DateTime.parse("09/14/2009 8:00")
:endDate => DateTime.parse("09/14/2009 9:00")
}])
You can try Date.strptime
:startDate => DateTime.parse("09/14/2009 8:00")
#=>
:startDate => DateTime.strptime("09/14/2009 8:00", "%m/%d/%Y %H:%M")
so
Meeting.create([
{
:title => "This meeting parses fine",
:startDate => DateTime.strptime("09/01/2009 17:00", "%m/%d/%Y %H:%M"),
:endDate => DateTime.strptime("09/01/2009 19:00", "%m/%d/%Y %H:%M")
},
{
:title => "This meeting errors out",
:startDate => DateTime.strptime("09/14/2009 8:00", "%m/%d/%Y %H:%M")
:endDate => DateTime.strptime("09/14/2009 9:00", "%m/%d/%Y %H:%M")
}])