Skip to content

Commit

Permalink
Added support for dotted meridians.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmecham committed May 29, 2014
1 parent 9f8af90 commit 848deb4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
14 changes: 9 additions & 5 deletions parsedatetime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def __init__(self, constants=None):
self.weekdyFlag = False # monday/tuesday/...
self.dateStdFlag = False # 07/21/06
self.dateStrFlag = False # July 21st, 2006
self.timeStdFlag = False # 5:50
self.timeStdFlag = False # 5:50
self.meridianFlag = False # am/pm
self.dayStrFlag = False # tomorrow/yesterday/today/..
self.timeStrFlag = False # lunch/noon/breakfast/...
Expand Down Expand Up @@ -1648,7 +1648,7 @@ def inc(self, source, month=None, year=None):
m = m % 12 # get remaining months

if mi < 0:
y *= -1 # otherwise negative mi will give future dates
y *= -1 # otherwise negative mi will give future dates
mth = mth - m # sub months from start month
if mth < 1: # cross start-of-year?
y -= 1 # yes - decrement year
Expand Down Expand Up @@ -1931,7 +1931,7 @@ def _initSymbols(ptc):
Initialize symbols and single character constants.
"""
# build am and pm lists to contain
# original case, lowercase and first-char
# original case, lowercase, first-char and dotted
# versions of the meridian text

if len(ptc.locale.meridian) > 0:
Expand All @@ -1940,9 +1940,11 @@ def _initSymbols(ptc):

if len(am) > 0:
ptc.am.append(am[0])
ptc.am.append('{}.{}.'.format(am[0], am[1]))
am = am.lower()
ptc.am.append(am)
ptc.am.append(am[0])
ptc.am.append('{}.{}.'.format(am[0], am[1]))
else:
am = ''
ptc.am = [ '', '' ]
Expand All @@ -1953,9 +1955,11 @@ def _initSymbols(ptc):

if len(pm) > 0:
ptc.pm.append(pm[0])
ptc.pm.append('{}.{}.'.format(pm[0], pm[1]))
pm = pm.lower()
ptc.pm.append(pm)
ptc.pm.append(pm[0])
ptc.pm.append('{}.{}.'.format(pm[0], pm[1]))
else:
pm = ''
ptc.pm = [ '', '' ]
Expand Down Expand Up @@ -2066,7 +2070,7 @@ def __init__(self, localeID=None, usePyICU=True, fallbackLocales=['en_US']):
# week +1 F

self.CurrentDOWParseStyle = False

if self.usePyICU:
self.locale = pdtLocales['icu'](self.localeID)

Expand Down Expand Up @@ -2428,4 +2432,4 @@ def buildSources(self, sourceTime=None):
sources[item] = ( values['yr'], values['mth'], values['dy'],
values['hr'], values['mn'], values['sec'], wd, yd, isdst )

return sources
return sources
74 changes: 48 additions & 26 deletions parsedatetime/tests/TestSimpleDateTimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,20 @@ def testTimes(self):
start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
target = datetime.datetime(self.yr, self.mth, self.dy, 23, 0, 0).timetuple()

self.assertTrue(_compareResults(self.cal.parse('11:00:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('2300', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('23:00', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11p', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11pm', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11:00:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('2300', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('23:00', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11p', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11pm', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11:00:00 P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11:00 P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11 P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11p.m.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11 p.m.', start), (target, 2)))

target = datetime.datetime(self.yr, self.mth, self.dy, 11, 0, 0).timetuple()

Expand All @@ -77,6 +83,12 @@ def testTimes(self):
self.assertTrue(_compareResults(self.cal.parse('11:00', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11a', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11am', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11:00:00 A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11:00 A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11 A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11a.m.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('11 a.m.', start), (target, 2)))

target = datetime.datetime(self.yr, self.mth, self.dy, 7, 30, 0).timetuple()

Expand Down Expand Up @@ -221,29 +233,39 @@ def testMidnight(self):
start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
target = datetime.datetime(self.yr, self.mth, self.dy, 0, 0, 0).timetuple()

self.assertTrue(_compareResults(self.cal.parse('midnight', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00:00 AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00 AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12 AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12am', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12a', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('0000', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('00:00', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('midnight', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00:00 AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00 AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12 AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12AM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12am', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12a', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('0000', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('00:00', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00:00 A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00 A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12 A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12A.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12a.m.', start), (target, 2)))

def testNoon(self):
start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
target = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0).timetuple()

self.assertTrue(_compareResults(self.cal.parse('noon', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12pm', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12p', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('1200', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('noon', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12 PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12PM', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12pm', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12p', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('1200', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00:00 P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12:00 P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12 P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12P.M.', start), (target, 2)))
self.assertTrue(_compareResults(self.cal.parse('12p.m.', start), (target, 2)))

# def testMonths(self):
#
Expand Down

0 comments on commit 848deb4

Please sign in to comment.