1
我有我的,看起來像下面的日誌文件中解析出的字符串:如何使用Ruby將日期類型中的字符串存儲在MongoDB中?
「[22 /月/ 2011:23:02:21 +0000]」
什麼是最好的方式(Ruby中的示例將會非常感謝,因爲我使用的是Mongo Ruby驅動程序)將它作爲本機Date類型存入MongoDB中?
我有我的,看起來像下面的日誌文件中解析出的字符串:如何使用Ruby將日期類型中的字符串存儲在MongoDB中?
「[22 /月/ 2011:23:02:21 +0000]」
什麼是最好的方式(Ruby中的示例將會非常感謝,因爲我使用的是Mongo Ruby驅動程序)將它作爲本機Date類型存入MongoDB中?
require 'date' # this is just to get the ABBR_MONTHNAMES list
input = "[22/May/2011:23:02:21 +0000]"
# this regex captures the numbers and month name
pattern = %r{^\[(\d{2})/(\w+)/(\d{4}):(\d{2}):(\d{2}):(\d{2}) ([+-]\d{4})\]$}
match = input.match(pattern)
# MatchData can be splatted, which is very convenient
_, date, month_name, year, hour, minute, second, tz_offset = *match
# ABBR_MONTHNAMES contains "Jan", "Feb", etc.
month = Date::ABBR_MONTHNAMES.index(month_name)
# we need to insert a colon in the tz offset, because Time.new expects it
tz = tz_offset[0,3] + ':' + tz_offset[3,5]
# this is your time object, put it into Mongo and it will be saved as a Date
Time.new(year.to_i, month, date.to_i, hour.to_i, minute.to_i, second.to_i, tz)
有幾件事情需要注意:
ABBR_MONTHNAMES
列表,否則,只是讓自己的列表。Date.parse
解析日期,這是非常慢的,同樣的DateTime.parse
,Time.parse
,它們使用相同的實現。String#index
,#[]
和#split
來提取你需要的部分。如果你想盡可能快地做到這一點,像下面可能是比較合適的。它不使用正則表達式(這是有用的,但不是快):
date = input[1, 2].to_i
month_name = input[4, 3]
month = Date::ABBR_MONTHNAMES.index(month_name)
year = input[8, 4].to_i
hour = input[13, 2].to_i
minute = input[16, 2].to_i
second = input[19, 2].to_i
tz_offset = input[22, 3].to_i * 60 * 60 + input[25, 2].to_i * 60
Time.new(year, month, date, hour, minute, second, tz_offset)
它需要的事實,所有領域都有固定寬度的優勢(至少我認爲他們這樣做)。所以你需要做的就是提取子字符串。它還將時區偏移量計算爲數字而不是字符串。