Skip to content

Instantly share code, notes, and snippets.

@glyph

glyph/ampm.py Secret

Created May 13, 2024 21:30
Show Gist options
  • Save glyph/ad25a9e8d93bb9f7ac15a947cbf53a5d to your computer and use it in GitHub Desktop.
Save glyph/ad25a9e8d93bb9f7ac15a947cbf53a5d to your computer and use it in GitHub Desktop.
from typing import Literal
AMPM = Literal["AM", "PM"]
def ampmify(hour: int, ampm: AMPM) -> int:
if hour == 12:
if ampm == "AM":
return 0
else:
return 12
elif ampm == "PM":
return hour + 12
else:
return hour
def addampm(hour: int) -> tuple[int, AMPM]:
if hour == 0:
return (12, "AM")
elif hour == 12:
return (12, "PM")
elif hour > 12:
return (hour - 12, "PM")
else:
return (hour, "AM")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment