views:

25

answers:

2

I'd like to save apache logs to MySQL via ActiveRecord. In the apache log, the default time string is like:

[13/Aug/2008:00:50:49 -0700]

How can I convert it to a ActiveRecord :datetime type? Thanks!

A: 

I was able to get DateTime to parse your string after removing the colon between the year and hour:

# given apache_time = "[13/Aug/2008:00:50:49 -0700]"
stamp = apache_time.sub %r[(/\d{4}):(\d{2})], '\1 \2'
datetime = DateTime.parse stamp   # returns DateTime representing "2008-08-13T00:50:49-07:00"
cam
+2  A: 
apache_time = "[13/Aug/2008:00:50:49 -0700]"
d = DateTime.strptime( apache_time, "[%d/%b/%Y:%H:%M:%S %Z]")

The format string is documented here: http://ruby-doc.org/core/classes/Time.src/M000298.html

steenslag