Suppose you have the following file:
textfield,datetimefield,numfield
foo,2008-07-01 17:50:55.004688,1
bar,2008-07-02 17:50:55.004688,2
The Rub
You are asking how to use converters.
My example defines the usage of a new converter mytime
.
The converter my_time tries to build a time object if possible.
#Define test data
src = < true, :converters => [:mytime])
csv.each do |row|
print "#{row}"
the_date = row['datetimefield'].to_date
end
Using this technique, you may also define a converter to create a date from time-like strings. This solution also keeps all other converters.
#define test data
src = < true, :converters => CSV::Converters.keys + [:time2date])
csv.each do |row|
print "#{row}"
p row['datetimefield'] #Date-field
end