Skip to content

Commit

Permalink
Merge pull request #8 from shade33/master
Browse files Browse the repository at this point in the history
Month names and short codes putted into one place
  • Loading branch information
robintw committed Aug 15, 2015
2 parents 914747e + cf6bfb1 commit 6861cc5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
63 changes: 32 additions & 31 deletions daterangeparser/parse_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@
import datetime
import calendar

MONTHS = {
'jan': 1,
'january': 1,
'feb': 2,
'february': 2,
'mar': 3,
'march': 3,
'apr': 4,
'april': 4,
'may': 5,
'jun': 6,
'june': 6,
'jul': 7,
'july': 7,
'aug': 8,
'august': 8,
'sep': 9,
'sept': 9,
'september': 9,
'oct': 10,
'october': 10,
'nov': 11,
'november': 11,
'dec': 12,
'december': 12
}


def check_day(tokens=None):
"""
Expand All @@ -33,39 +60,13 @@ def check_day(tokens=None):
raise ParseException("Couldn't parse resulting datetime")

def month_to_number(tokens):
"""Converts a given month in string format to the equivalent month number.
"""
Converts a given month in string format to the equivalent month number.
Works with strings in any case, both abbreviated (Jan) and full (January).
"""

conversions = { 'jan' : 1,
'january' : 1,
'feb' : 2,
'february' : 2,
'mar' : 3,
'march' : 3,
'apr' : 4,
'april' : 4,
'may' : 5,
'jun' : 6,
'june': 6,
'jul' : 7,
'july' : 7,
'aug' : 8,
'august' : 8,
'sep' : 9,
'sept' : 9,
'september' : 9,
'oct' : 10,
'october' : 10,
'nov' : 11,
'november' : 11,
'dec' : 12,
'december' : 12}

month_name = tokens[0].lower()
return conversions[month_name]
return MONTHS[month_name]

def post_process(res):
"""Perform post-processing on the results of the date range parsing.
Expand Down Expand Up @@ -161,7 +162,7 @@ def create_parser():
full_day_string.setParseAction(check_day)

# Month names, with abbreviations, with action to convert to equivalent month number
month = oneOf("Jan January Feb February Mar March Apr April May Jun June Jul July Aug August Sep September Oct October Nov November Dec December", caseless=True)
month = oneOf(MONTHS.keys(), caseless=True)
month.setParseAction(month_to_number)

# Year
Expand All @@ -185,7 +186,7 @@ def create_parser():
separator = oneOf(u"- -- to until \u2013 \u2014 ->", caseless=True)

# Strings to completely ignore (whitespace ignored by default)
ignoreable_chars = oneOf(", from", caseless=True)
ignoreable_chars = oneOf(", from starting beginning", caseless=True)

# Final putting together of everything
daterange = Optional(first_date("start") + Optional(time).suppress() + separator.suppress()) + last_date("end") + Optional(time).suppress() + stringEnd
Expand Down
1 change: 1 addition & 0 deletions daterangeparser/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class WorkingParsingTest(unittest.TestCase):
# Ignorable characters
("14, July", "14/7/XXXX", None),
("From 1st to 9th Jan 2008", "1/1/2008", "9/1/2008"),
("beGinning 1st to 9th Sept 2008", "1/9/2008", "9/9/2008"),
("17th, -, 19th June 1987", "17/6/1987", "19/6/1987"),

# Ignoring of times
Expand Down

0 comments on commit 6861cc5

Please sign in to comment.