Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5956627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:17:03+00:00 2026-05-22T18:17:03+00:00

I have a string that I’m parsing out from log files that looks like

  • 0

I have a string that I’m parsing out from log files that looks like the following:

“[22/May/2011:23:02:21 +0000]”

What’s the best way (examples in Ruby would be most appreciated, as I’m using the Mongo Ruby driver) to get that stashed into MongoDB as a native Date type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:17:04+00:00Added an answer on May 22, 2026 at 6:17 pm
    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)
    

    A few things to note:

    • I assumed that the month names are the same as in the ABBR_MONTHNAMES list, otherwise, just make your own list.
    • Never ever use Date.parse to parse dates it is incredibly slow, the same goes for DateTime.parse, Time.parse, which use the same implementation.
    • If you parse a lot of different date formats check out the home_run gem.
    • If you do a lot of these (like you often do when parsing log files), consider not using a regex. Use String#index, #[] and #split to extract the parts you need.

    If you want to do this as fast as possible, something like the following is probably more appropriate. It doesn’t use regexes (which are useful, but not fast):

    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)
    

    It takes advantage of the fact that all fields have fixed width (at least I assume they do). So all you need to do is extract the substrings. It also calculates the timezone offset as a number instead of a string.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that contains the representation of a date. It looks like:
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a String[] that looks like this where the first String is aways
I have a string that looks like: EXP001,1,#,*\r\n\r\nEXP001,2,#,*\r\n\r\n How do I separate the two
I have a string that looks something like this: foobar\n I read this string
I have a string that looks like this: <td width=308>Services Needed: <a href=javascript:void(null); title=weekly
I have a string that looks like this: 'p10005c4' I need to extract the
I have a string that is coming from DB. I would like to read
I have a String that looks like: Hello my is Joeseph. It is very
I have a string that looks like: '- 10 TEST (FOO 3 TEST.BAR 213

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.