How to: convert string to DateTime object

Hi, can someone please show an example of:

-how to convert a string to a DateTime object using
DateTime.strptime(1.9.3)

-and how to run an each loop to convert multiple strings

thank you!

a newbie rubyist

How is it?

kirti@kirti-Aspire-5733Z:~$ irb
2.0.0p0 :001 > require ‘time’
=> true
2.0.0p0 :002 > Time.local(“12/04/2013”)
=> 0012-01-01 00:00:00 +0553
2.0.0p0 :003 >

This is the kind of thing you could find in numerous tutorials online.
Have you tried looking on Google?

On Tue, Apr 30, 2013 at 12:18 PM, Ana B. [email protected] wrote:

Hi, can someone please show an example of:

-how to convert a string to a DateTime object using
DateTime.strptime(1.9.3)

Do you need more than this:

?

-and how to run an each loop to convert multiple strings

Since I don’t know your true need, I’ll make something up. Suppose you
have a log file, that looks something like this:

DEBUG 2013-04-23T17:27:50-05:00 - " ESC[1mESC[35mSpree::Preference
Load (5.5ms)ESC[0m SELECT spree_preferences.* FROM
spree_preferences WHERE spree_preferences.key IS NOT NULL AND
spree_preferences.value_type IS NOT NULL"
DEBUG 2013-04-23T17:27:50-05:00 - “Cache write:
spree/app_configuration/products_per_page”
DEBUG 2013-04-23T17:27:50-05:00 “Dalli::Server#connect 127.0.0.1:11211”
DEBUG 2013-04-23T17:27:50-05:00 - “Cache write:
spree/app_configuration/track_inventory_levels”
DEBUG 2013-04-23T17:27:50-05:00 - “Cache write:
spree/app_configuration/allow_backorder_shipping”
DEBUG 2013-04-23T17:27:50-05:00 - “Cache write:
spree/app_configuration/checkout_zone”
DEBUG 2013-04-23T17:27:50-05:00 - “Cache write:
spree/app_configuration/allow_ssl_in_production”
DEBUG 2013-04-23T17:27:50-05:00 - "Cache write:
spree/app_configuration/default_development.log Connecting to database
specified by database.yml

And you want to extract the time stamps from that and convert them to
DateTime objects. The following might be a way to do that:

IO.foreach(“./log/development.log”) do |line|
timestamp = DateTime.strptime(line.split[1]),‘%FT%T%z’)
end

Joel P. wrote in post #1107402:

This is the kind of thing you could find in numerous tutorials online.
Have you tried looking on Google?

yes, this is what I find:

DateTime.strptime(“2009/04/16 19:52:30”, “%Y/%m/%d %H:%M:%S”)

it doesn’t show how to invoke a list of strings…

-do you know of a good tutorial?

On Tue, Apr 30, 2013 at 12:54 PM, tamouse mailing lists
[email protected] wrote:

-and how to run an each loop to convert multiple strings
DEBUG 2013-04-23T17:27:50-05:00 “Dalli::Server#connect 127.0.0.1:11211”
specified by database.yml

And you want to extract the time stamps from that and convert them to
DateTime objects. The following might be a way to do that:

IO.foreach(“./log/development.log”) do |line|
timestamp = DateTime.strptime(line.split[1]),‘%FT%T%z’)
end

Opps, assume that log file didn’t wrap and the DEBUG …
are all on one line.

tamouse mailing lists wrote in post #1107404:

On Tue, Apr 30, 2013 at 12:54 PM, tamouse mailing lists
[email protected] wrote:

-and how to run an each loop to convert multiple strings
DEBUG 2013-04-23T17:27:50-05:00 “Dalli::Server#connect 127.0.0.1:11211”
specified by database.yml

And you want to extract the time stamps from that and convert them to
DateTime objects. The following might be a way to do that:

IO.foreach(“./log/development.log”) do |line|
timestamp = DateTime.strptime(line.split[1]),‘%FT%T%z’)
end

Opps, assume that log file didn’t wrap and the DEBUG …
are all on one line.

thank you, this helps, it’s close to what I’m looking for