开发者

How to convert apache log time to activerecord :datetime

I'd like to save ap开发者_JAVA技巧ache 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!


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


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"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜