Skip to content

Commit

Permalink
Next sensible time, supporting "o'clock" (#38)
Browse files Browse the repository at this point in the history
* support o'clock - fixes #12 (second half)

* move to next day if time already passed and date not specified - fixes #12 (first half)
  • Loading branch information
alvinwan authored Jan 17, 2025
1 parent 04b9e7b commit 0a74384
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 2 additions & 3 deletions tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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"
Expand All @@ -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'
])
Expand Down
2 changes: 1 addition & 1 deletion timefhuman/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -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")?
Expand Down
7 changes: 6 additions & 1 deletion timefhuman/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 0a74384

Please sign in to comment.