Skip to content

Commit 39de703

Browse files
authored
Merge pull request #26 from Orinks/fix/natural-time-ampm-bug
fix: correct AM/PM in natural time format at noon/midnight boundary
2 parents 3b7c65d + e00d4d7 commit 39de703

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/accessiclock/audio/tts_engine.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ def _format_natural(self, hour: int, minute: int, am_pm: str) -> str:
180180
next_hour = hour % 12 + 1
181181
if next_hour == 0:
182182
next_hour = 12
183-
return f"quarter to {next_hour} {am_pm}"
183+
# Flip AM/PM when crossing noon (12 PM) or midnight (12 AM)
184+
next_am_pm = ("PM" if am_pm == "AM" else "AM") if hour == 11 else am_pm
185+
return f"quarter to {next_hour} {next_am_pm}"
184186
else:
185187
return f"{hour}:{minute:02d} {am_pm}"
186188

tests/test_tts_engine.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,36 @@ def test_format_time_natural_quarter_to(self):
186186
result = engine.format_time(time(14, 45), style="natural")
187187
assert "quarter to" in result.lower()
188188

189+
def test_format_time_natural_quarter_to_noon_boundary(self):
190+
"""At 11:45 AM, 'quarter to' should say 'quarter to 12 PM' not 'AM'."""
191+
from accessiclock.audio.tts_engine import TTSEngine
192+
193+
engine = TTSEngine(force_dummy=True)
194+
result = engine.format_time(time(11, 45), style="natural")
195+
assert "quarter to" in result.lower()
196+
assert "12" in result
197+
assert "PM" in result
198+
199+
def test_format_time_natural_quarter_to_midnight_boundary(self):
200+
"""At 11:45 PM, 'quarter to' should say 'quarter to 12 AM' not 'PM'."""
201+
from accessiclock.audio.tts_engine import TTSEngine
202+
203+
engine = TTSEngine(force_dummy=True)
204+
result = engine.format_time(time(23, 45), style="natural")
205+
assert "quarter to" in result.lower()
206+
assert "12" in result
207+
assert "AM" in result
208+
209+
def test_format_time_natural_quarter_to_no_boundary(self):
210+
"""At 2:45 PM, 'quarter to' should keep PM."""
211+
from accessiclock.audio.tts_engine import TTSEngine
212+
213+
engine = TTSEngine(force_dummy=True)
214+
result = engine.format_time(time(14, 45), style="natural")
215+
assert "quarter to" in result.lower()
216+
assert "3" in result
217+
assert "PM" in result
218+
189219
def test_format_time_natural_irregular_minute(self):
190220
"""Natural style with irregular minutes should show time normally."""
191221
from accessiclock.audio.tts_engine import TTSEngine

0 commit comments

Comments
 (0)