On Tue, Apr 30, 2013 at 12:54 PM, tamouse mailing lists
<tamouse.lists / gmail.com> wrote:
> On Tue, Apr 30, 2013 at 12:18 PM, Ana Bia <lists / ruby-forum.com> 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:
> http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html#method-c-strptime
> ?
>
>> -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

Opps, assume that log file didn't wrap and the DEBUG <timestamp> ...
are all on one line.