Converting date format (CSV to yaml)

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

···

--
Posted via http://www.ruby-forum.com/.

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'.

···

--
Anurag Priyam
http://about.me/yeban/

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
    >> require 'date'
    => true
    >> require 'yaml'
    => true
    >> d = Date.parse('12/1/2011')
    => #<Date: 4911793/2,0,2299161>
    >> puts d.to_yaml
    --- 2011-12-01
    => nil

Ruby 1.9
    >> require 'date'
    => true
    >> require 'yaml'
    => true
    >> d = Date.parse('12/1/2011')
    => #<Date: 2011-01-12 (4911147/2,0,2299161)>
    >> puts d.to_yaml
    --- 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:
    >> d = Date.strptime('12/1/2011', '%d/%m/%Y')
    => #<Date: 2011-01-12 (4911147/2,0,2299161)>
    >> puts d.to_yaml
    --- 2011-01-12
    => nil

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

.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

···

On Thu, Feb 3, 2011 at 6:14 AM, Kamarulnizam Rahim <niezam54@hotmail.com> wrote:

problem solved. thanks guys!!!

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 02/02/2011 10:36 PM, Kamarulnizam Rahim wrote:

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

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:

-Jeremy

···

On 02/02/2011 11:02 PM, Kamarulnizam Rahim wrote:

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