Skip to content

Commit

Permalink
move to next day if time already passed and date not specified - fixes
Browse files Browse the repository at this point in the history
…#12 (first half)
  • Loading branch information
alvinwan committed Jan 17, 2025
1 parent 3ee0833 commit 68265f4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,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
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 68265f4

Please sign in to comment.