Converting date format (CSV to yaml)

  • Thread starter Kamarulnizam Rahim
  • Start date
K

Kamarulnizam Rahim

Hi,

Anyone know how to convert date format of .csv to . yaml?

csv date format:

12/1/2011

yaml date format:

2011-01-12

Nizam
 
A

Anurag Priyam

Anyone know how to convert date format of .csv to . yaml?
.csv date format:

12/1/2011

.yaml date format:

2011-01-12

cdate = "12/1/2011"
m, d, y = cdate.split('/').map{|i| if i.length == 1 then "0#{i}" else i end}
ydate = "#{y}-#{d}-#{m}"

The map part on the second line takes care of padding a single digit
month, and date with a '0'.
 
B

brabuhr

Anyone know how to convert date format of .csv to . yaml?
.csv date format:

12/1/2011

.yaml date format:

2011-01-12

Another option could be to parse the String into a Date that is then
naturally marshaled into the right format. That could help make the
intent of the code more clear than splitting and re-arranging by hand,
but note that Date.parse changed from Ruby 1.8 to Ruby 1.9.

Ruby 1.8--- 2011-12-01
=> nil

Ruby 1.9--- 2011-01-12
=> nil

In practice, I sometimes just split it by hand, but I usually try to
use strptime:

Ruby 1.8 and 1.9:--- 2011-01-12
=> nil
 
K

Kamarulnizam Rahim

Hi,

I used the following code:

cdate = "2011-01-12"
m, d, y = cdate.split('/').map{|i| if i.length == 1 then "0#{i}" else i
end}
pp ydate = "#{y}-#{d}-#{m}"

Output:
"2011-01-12"

How i change it to:
2011-01-12 <-- without the quotation mark


Nizam
 
J

Jeremy Bopp

Hi,

I used the following code:

cdate = "2011-01-12"
m, d, y = cdate.split('/').map{|i| if i.length == 1 then "0#{i}" else i
end}
pp ydate = "#{y}-#{d}-#{m}"

Output:
"2011-01-12"

How i change it to:
2011-01-12 <-- without the quotation mark

The pp method is adding the quotation marks when printing the string.
The quotes are not really in the string's value. Try using puts instead
of pp to see the difference.

-Jeremy
 
K

Kamarulnizam Rahim

But when i tried transferring the ydate into my yaml structure, it still
print out the quotation marks.

{"type"=>"TargetComponent",
"title"=>"None",
"args"=>
{:start_date=>'2010-08-10',
:end_date=> (ydate),
:state=>"#{status}"}}]}

My yaml output:

- type: TargetComponent
title: None
args:
:start_date: "2010-08-10"
:end_date: "2011-01-12"
:state: Active
 
J

Jeremy Bopp

But when i tried transferring the ydate into my yaml structure, it still
print out the quotation marks.

{"type"=>"TargetComponent",
"title"=>"None",
"args"=>
{:start_date=>'2010-08-10',
:end_date=> (ydate),
:state=>"#{status}"}}]}

My yaml output:

- type: TargetComponent
title: None
args:
:start_date: "2010-08-10"
:end_date: "2011-01-12"
:state: Active

It's because you actually have a String rather than a Date object. Try
this in irb:

ruby-1.9.2-p136 :001 > require 'yaml'
=> true
ruby-1.9.2-p136 :002 > require 'date'
=> false
ruby-1.9.2-p136 :003 > puts Date.today.to_yaml
--- 2011-02-02
=> nil
ruby-1.9.2-p136 :004 > puts "2011-02-02".to_yaml
--- "2011-02-02"
=> nil

The command at :004 is what you're doing, but you want what the command
at :003 is doing. Take the advice provided in the post linked below and
use Date objects to process your dates instead of Strings:

http://www.ruby-forum.com/topic/1005964#979296

-Jeremy
 
K

Kamarulnizam Rahim

I used both method. Both of them return an error


cdate = due_date
m, d, y = cdate.split('/').map{|i| if i.length == 1 then "0#{i}"
else i
end}
puts ydate = "#{y}-#{d}-#{m}"
date = Date.parse(ydate)
#date = Date.strptime(ydate, '%d/%m/%Y')

C:/Ruby192/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags': invalid date
(ArgumentError)
from C:/Ruby192/lib/ruby/1.9.1/date.rb:1066:in `parse'

cdate = due_date
m, d, y = cdate.split('/').map{|i| if i.length == 1 then "0#{i}"
else i
end}
puts ydate = "#{y}-#{d}-#{m}"
#date = Date.parse(ydate)
date = Date.strptime(ydate, '%d/%m/%Y')

C:/Ruby192/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags': invalid date
(ArgumentError)
from C:/Ruby192/lib/ruby/1.9.1/date.rb:1046:in `strptime'

Is it because of my IDE?

Nizam
 
B

botp

.csv date format:

12/1/2011

.yaml date format:

2011-01-12

when csv reads the data, it's still a string. you may want to convert
the string further to Date. in that case, use Date.strptime and pass
the string and it's current format

eg in irb,
Date.strptime "12/1/2011", "%d/%M/%Y"
#=> #<Date: 2011-01-12 (4911147/2,0,2299161)>

where %d stands for day, %M for month, and %Y for year
note, the format must match your string otherwise you'll get errors
like invalid date..

best regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top