diff --git a/src/tardyclock/cli.py b/src/tardyclock/cli.py index 0da1fa6..d90dff0 100644 --- a/src/tardyclock/cli.py +++ b/src/tardyclock/cli.py @@ -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__": diff --git a/src/tardyclock/core.py b/src/tardyclock/core.py index 024a88e..a4b666b 100644 --- a/src/tardyclock/core.py +++ b/src/tardyclock/core.py @@ -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)