fix(day-postfix): day postfix can be st, nd, rd, th

This commit is contained in:
Mahdi Dibaiee 2018-03-27 15:11:05 +04:30
parent 18d01dc186
commit ab3e19dabc
3 changed files with 8 additions and 5 deletions

View File

@ -20,7 +20,7 @@ postfixes = {
time_prefix = 'at' time_prefix = 'at'
time_postfix = ['am', 'pm'] time_postfix = ['am', 'pm']
day_postfix = 'th' day_postfix = ['th', 'st', 'nd', 'rd']
multipliers = { multipliers = {
'second': ('seconds', 1), 'second': ('seconds', 1),
@ -85,9 +85,9 @@ def tokenize(string):
elif w[0:-2].isdigit() and w[-2:] in time_postfix and int(w[0:-2]) <= 12: elif w[0:-2].isdigit() and w[-2:] in time_postfix and int(w[0:-2]) <= 12:
filtered.append(int(w[0:-2])) filtered.append(int(w[0:-2]))
filtered.append(w[-2:]) filtered.append(w[-2:])
elif w[0:-2].isdigit() and w[-2:] == day_postfix: elif w[0:-2].isdigit() and w[-2:] in day_postfix:
filtered.append(int(w[0:-2])) filtered.append(int(w[0:-2]))
filtered.append(day_postfix) filtered.append(w[-2:])
elif ':' in w: elif ':' in w:
f, s = w[0:w.index(':')], w[w.index(':') + 1:] f, s = w[0:w.index(':')], w[w.index(':') + 1:]
@ -115,7 +115,7 @@ def compute(tokens):
elif isinstance(t, int) and has_next and tokens[i + 1] in multipliers: elif isinstance(t, int) and has_next and tokens[i + 1] in multipliers:
key, v = multipliers[tokens[i + 1]] key, v = multipliers[tokens[i + 1]]
value += timedelta(**{ key: t * v }) value += timedelta(**{ key: t * v })
elif isinstance(t, int) and has_next and (tokens[i + 1] in months or tokens[i + 1] == day_postfix): elif isinstance(t, int) and has_next and (tokens[i + 1] in months or tokens[i + 1] in day_postfix):
day = t day = t
elif isinstance(t, int) and has_next and tokens[i + 1] in time_postfix: elif isinstance(t, int) and has_next and tokens[i + 1] in time_postfix:
if tokens[i + 1] == 'am': if tokens[i + 1] == 'am':

View File

@ -57,6 +57,9 @@ class HumanDateTests(unittest.TestCase):
'february 28': (28, 2), 'february 28': (28, 2),
'december 1th': (1, 12), 'december 1th': (1, 12),
'Feb. 28': (28, 2), 'Feb. 28': (28, 2),
'March 31st': (31, 3),
'March 23rd': (23, 3),
'March 22nd': (22, 3),
} }
for (k, (day, m)) in tests.items(): for (k, (day, m)) in tests.items():

View File

@ -2,7 +2,7 @@ from distutils.core import setup
setup( setup(
name = 'humandate', name = 'humandate',
packages = ['humandate'], packages = ['humandate'],
version = '0.7', version = '0.8',
description = 'Parse human-readable dates', description = 'Parse human-readable dates',
author = 'Mahdi Dibaiee', author = 'Mahdi Dibaiee',
author_email = 'mdibaiee@aol.com', author_email = 'mdibaiee@aol.com',