Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/tardyclock/cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import typer
from .core import random_time

DEFAULT_TIME_FORMAT = "%A %B %m, %Y, %H:%M:%S"
DEFAULT_TIME_FORMAT_12_HOUR = "%A %B %m, %Y, %I:%M:%S"

tardy = typer.Typer()


@tardy.command()
def tell(hr24: bool = True, showtimezone: bool = False):
print(random_time(hr24=hr24, showtimezone=showtimezone))
def tell(
hr24: bool = True,
showtimezone: bool = False,
time_format: str = DEFAULT_TIME_FORMAT,
):
if not hr24 and time_format == DEFAULT_TIME_FORMAT:
time_format = DEFAULT_TIME_FORMAT_12_HOUR

print(
random_time(
hr24=hr24,
showtimezone=showtimezone,
time_format=time_format,
)
)


if __name__ == "__main__":
Expand Down
17 changes: 14 additions & 3 deletions src/tardyclock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@
import pytz


def random_time(hr24: bool = True, showtimezone: bool = False):
def random_time(
hr24: bool = True,
showtimezone: bool = False,
time_format: str | None = None,
):
"""Tells you the current time in a timezone of its choosing"""
tz = pytz.timezone(choice(pytz.all_timezones))
now = datetime.now(tz)

time_format = f"%A %B %m, %Y, {'%H' if hr24 else '%I'}:%M:%S"
if showtimezone:
if time_format is None:
time_format = f"%A %B %m, %Y, {'%H' if hr24 else '%I'}:%M:%S"
else:
if hr24 and "%I" in time_format and "%H" not in time_format:
time_format = time_format.replace("%I", "%H")
elif not hr24 and "%H" in time_format and "%I" not in time_format:
time_format = time_format.replace("%H", "%I")

if showtimezone and "%Z" not in time_format:
time_format += " %Z"

return now.strftime(time_format)