I''m inmporting a csv file and I have strings like this: "15-OTT-98", 5-GEN-96". They are dates: OTT stands for Oct in italian and GEN stands for Jan. How can I render those strings in valid date format? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 May 2011, at 10:24, Mauro wrote:> I''m inmporting a csv file and I have strings like this: "15-OTT-98", > 5-GEN-96". > They are dates: OTT stands for Oct in italian and GEN stands for Jan. > How can I render those strings in valid date format?# Note: my Italian is very rusty to non-existent, so change below where needed CUSTOM_MONTHS = [nil, "GEN", "FEB", "MAR", "APR", "MAG", "GIU", "LUG", "AGO", "SET", "OTT", "NOV", "DIC"] def parsedatestring(datestring) day, monthname, year = datestring.split("-") year = (year.to_i > Date.today.year - 2000 ? "19#{year}" : "20#{year}") Date.new(year.to_i, CUSTOM_MONTHS.index(monthname), day.to_i) end You might need to tweak the year conversion in case you have short dates that are in the future. Right now I''m assuming all dates are in the past. Best regards Peter De Berdt -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 May 2011 09:04, Peter De Berdt <peter.de.berdt-LPO8gxj9N8aZIoH1IeqzKA@public.gmane.org> wrote:> > On 19 May 2011, at 10:24, Mauro wrote: > > I''m inmporting a csv file and I have strings like this: "15-OTT-98", > 5-GEN-96". > They are dates: OTT stands for Oct in italian and GEN stands for Jan. > How can I render those strings in valid date format? > > # Note: my Italian is very rusty to non-existent, so change below where > needed > CUSTOM_MONTHS = [nil, "GEN", "FEB", "MAR", "APR", "MAG", "GIU", "LUG", > "AGO", "SET", "OTT", "NOV", "DIC"] > def parsedatestring(datestring) > day, monthname, year = datestring.split("-") > year = (year.to_i > Date.today.year - 2000 ? "19#{year}" : "20#{year}") > Date.new(year.to_i, CUSTOM_MONTHS.index(monthname), day.to_i) > end > You might need to tweak the year conversion in case you have short dates > that are in the future. Right now I''m assuming all dates are in the past.Ok. Can I create past that method inside a custom rake task and use it inside the namespace? Ex: /lib/tasks/import.rake def parsedatestring(datestring) day, monthname, year = datestring.split("-") year = (year.to_i > Date.today.year - 2000 ? "19#{year}" : "20#{year}") Date.new(year.to_i, CUSTOM_MONTHS.index(monthname), day.to_i) end namespace :import do desc ''imports'' task:raw do parsedatestring(.....) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 May 2011, at 11:17, Mauro wrote:> Ok. > Can I create past that method inside a custom rake task and use it > inside the namespace? > Ex: > > /lib/tasks/import.rake > > def parsedatestring(datestring) > day, monthname, year = datestring.split("-") > year = (year.to_i > Date.today.year - 2000 ? "19#{year}" : > "20#{year}") > Date.new(year.to_i, CUSTOM_MONTHS.index(monthname), day.to_i) > end > > namespace :import do > desc ''imports'' > task:raw do > parsedatestring(.....)Sure, you can define methods inside rake tasks and use them there afaik (not 100% sure, but 99%). Try for yourself and see. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.