Reverting date from Time.to_s

I receive the dates from Twitter in the following format “Mon Mar 22
04:42:17 +0000 2010”

Now how can I create a time object from this string… ie Obj.year=2010,
Obj.month=3… etc etc

Regards

Utsav Gupta

On Mar 22, 2010, at 1:06 PM, Utsav Gupta wrote:

I receive the dates from Twitter in the following format “Mon Mar 22
04:42:17 +0000 2010”

Now how can I create a time object from this string… ie Obj.year=2010,
Obj.month=3… etc etc

Quite simple.

cremes$ irb
ruby-1.9.1-p378 > require ‘time’
=> true
ruby-1.9.1-p378 > s = “Mon Mar 22 04:42:17 +0000 2010”
=> “Mon Mar 22 04:42:17 +0000 2010”
ruby-1.9.1-p378 > t = Time.parse s
=> 2010-03-21 23:42:17 -0500
ruby-1.9.1-p378 > t.month
=> 3
ruby-1.9.1-p378 > t.year
=> 2010

Note that Time.parse creates your time object with the local timezone.
You can still access zulu time by using the #utc method on that object.

cr

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs