From ab3e19dabc8a041786798da3deccde188dda1ba2 Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 27 Mar 2018 15:11:05 +0430 Subject: [PATCH] fix(day-postfix): day postfix can be st, nd, rd, th --- humandate/index.py | 8 ++++---- humandate/tests.py | 3 +++ setup.py | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/humandate/index.py b/humandate/index.py index 186c4ef..3343a39 100644 --- a/humandate/index.py +++ b/humandate/index.py @@ -20,7 +20,7 @@ postfixes = { time_prefix = 'at' time_postfix = ['am', 'pm'] -day_postfix = 'th' +day_postfix = ['th', 'st', 'nd', 'rd'] multipliers = { '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: filtered.append(int(w[0:-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(day_postfix) + filtered.append(w[-2:]) elif ':' in w: 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: key, v = multipliers[tokens[i + 1]] 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 elif isinstance(t, int) and has_next and tokens[i + 1] in time_postfix: if tokens[i + 1] == 'am': diff --git a/humandate/tests.py b/humandate/tests.py index a6f5c25..0af44c0 100644 --- a/humandate/tests.py +++ b/humandate/tests.py @@ -57,6 +57,9 @@ class HumanDateTests(unittest.TestCase): 'february 28': (28, 2), 'december 1th': (1, 12), 'Feb. 28': (28, 2), + 'March 31st': (31, 3), + 'March 23rd': (23, 3), + 'March 22nd': (22, 3), } for (k, (day, m)) in tests.items(): diff --git a/setup.py b/setup.py index 99eb00f..a62dea9 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from distutils.core import setup setup( name = 'humandate', packages = ['humandate'], - version = '0.7', + version = '0.8', description = 'Parse human-readable dates', author = 'Mahdi Dibaiee', author_email = 'mdibaiee@aol.com',