diff --git a/tests/test_e2e.py b/tests/test_e2e.py index fe01135..184784e 100644 --- a/tests/test_e2e.py +++ b/tests/test_e2e.py @@ -79,7 +79,7 @@ def test_default(now, test_input, expected): @pytest.mark.parametrize("test_input, expected", [ # time only ('5p', datetime.time(hour=17, minute=0)), - # ("3 o'clock", datetime.time(hour=3, minute=0)), + ("3 o'clock pm", datetime.time(hour=15, minute=0)), # fixes gh#12 # date only ('July 2019', datetime.date(2019, 7, 1)), @@ -112,7 +112,6 @@ def test_default(now, test_input, expected): ('30-40 mins', (datetime.timedelta(minutes=30), datetime.timedelta(minutes=40))), ('1 or 2 days', [datetime.timedelta(days=1), datetime.timedelta(days=2)]), - # TODO: support "3 o'clock" # TODO: support "quarter to 3" # TODO: support "one and a half hours" @@ -131,7 +130,7 @@ def test_no_inference(now, test_input, expected): (tfhConfig(infer_datetimes=True), '5p', datetime.datetime(2018, 8, 4, 17, 0)), (tfhConfig(infer_datetimes=False), '5p', datetime.time(hour=17, minute=0)), - # (tfhConfig(infer_datetimes=True), '1p', datetime.datetime(2018, 8, 5, 13, 0)), # TODO tomorrow, since passed today (gh#12) + (tfhConfig(infer_datetimes=True), '1p', datetime.datetime(2018, 8, 5, 13, 0)), # TODO tomorrow, since passed today (gh#12) # TODO: add tests for 'next/last' ]) diff --git a/timefhuman/grammar.lark b/timefhuman/grammar.lark index 18dac45..f86b579 100644 --- a/timefhuman/grammar.lark +++ b/timefhuman/grammar.lark @@ -77,7 +77,7 @@ date: month "/" day "/" year // However, that means to support single-integer (e.g., hour) times, we need // to manually add them to the `datetime` rule above. time: hour ":" minute meridiem? - | hour meridiem + | hour ("o'clock")? meridiem | timename duration: ("in"|"for")? duration_part (("and"|",")? duration_part)* ("ago")? diff --git a/timefhuman/main.py b/timefhuman/main.py index 02941ae..2caf448 100644 --- a/timefhuman/main.py +++ b/timefhuman/main.py @@ -276,7 +276,12 @@ def to_object(self, config: tfhConfig = tfhConfig()) -> Union[datetime, date, ti return self.date.to_object(config) elif self.time: if config.infer_datetimes: - return datetime.combine(config.now.date(), self.time.to_object(config)) + candidate = datetime.combine(config.now.date(), self.time.to_object(config)) + if candidate < config.now and config.direction == Direction.next: + candidate += timedelta(days=1) + elif candidate > config.now and config.direction == Direction.previous: + candidate -= timedelta(days=1) + return candidate return self.time.to_object(config) raise ValueError("Datetime is missing both date and time")