diff --git a/.gitignore b/.gitignore index a5b9cff..f8b98e2 100644 --- a/.gitignore +++ b/.gitignore @@ -371,3 +371,4 @@ Cargo.lock # End of https://www.toptal.com/developers/gitignore/api/node,go,java,intellij,visualstudiocode,haskell,erlang,elixir,rust /.vs/ProjectSettings.json /.vs/slnx.sqlite +.DS_Store diff --git a/projects/final-project/solution/danieleFiocca/simple_time_display/__init__.py b/projects/final-project/solution/danieleFiocca/simple_time_display/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/projects/final-project/solution/danieleFiocca/simple_time_display/countdown_display.py b/projects/final-project/solution/danieleFiocca/simple_time_display/countdown_display.py new file mode 100644 index 0000000..a721b70 --- /dev/null +++ b/projects/final-project/solution/danieleFiocca/simple_time_display/countdown_display.py @@ -0,0 +1,130 @@ +import threading +import time + + +def set_timer(): + """Setting the timer in seconds""" + while (timer := int(input("Enter time in seconds: "))) <= 0: + print("Invalid time. Time must be greater than 0.") + return timer + + +def get_format_input(): + """Setting the format for display countdown""" + print("\nChoose time format:") + print("1. Complete (DD:HH:MM:SS)") + print("2. Hour:Minute:Seconds") + print("3. Minute:Seconds") + print("4. Seconds") + + while True: + format_select = int(input("\nEnter a time format: ")) + if format_select in [1, 2, 3, 4]: + return format_select + else: + print("Selection must be 1 to 4.") + + +def format_display_timer(seconds, format_selected): + """Formatting the display countdown""" + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) + days, hours = divmod(hours, 24) + if format_selected == 1: + return "{:02d}:{:02d}:{:02d}:{:02d}".format(days, hours, minutes, seconds) + if format_selected == 2: + total_hours = days * 24 + hours + return "{:02d}:{:02d}:{:02d}".format(total_hours, minutes, seconds) + if format_selected == 3: + total_minutes = (days * 24 * 60) + (hours * 60) + minutes + return "{:02d}:{:02d}".format(total_minutes, seconds) + if format_selected == 4: + return "{:04d}".format(seconds) + + +def countdown_logic( + initial_seconds, format_selected, pause_event, stop_event, reset_event +): + """Countdown logic""" + seconds = initial_seconds + while seconds >= 0: + if stop_event.is_set(): + + return + + if reset_event.is_set(): + seconds = initial_seconds + reset_event.clear() + print(f"\nReset to: {format_display_timer(seconds, format_selected)}") + + if not pause_event.is_set(): + print( + f"\r{format_display_timer(seconds, format_selected)} ", + end="", + flush=True, + ) + time.sleep(1) + seconds -= 1 + else: + time.sleep(0.1) # While paused + if not stop_event.is_set(): + print("\nTimer finished.") + + +def handle_commands(pause, stop, reset): + """Logic for threading commands""" + print("Command available: start, pause, stop, reset") + + while not stop.is_set(): + command = input("\n>>>").strip().lower() + match command: + case "start": + pause.clear() + print("time started") + case "pause": + pause.set() + print("time paused") + case "reset": + reset.set() + print("time reset") + case "stop": + stop.set() + print("time stopped") + + case _: + print("Invalid command") + """if command == "start": + pause.clear() + print("Timer started") + elif command == "pause": + pause.set() + print("Timer paused") + elif command == "reset": + reset.set() + print("Timer reset") + elif command == "stop": + stop.set() + print("Timer stopped") + break + else: + print("Command not available")""" + + +def start_timer(): + """Function to start the timer""" + seconds = set_timer() + format_selected = get_format_input() + + pause = threading.Event() + stop = threading.Event() + reset = threading.Event() + + timer_thread = threading.Thread( + target=countdown_logic, args=(seconds, format_selected, pause, stop, reset) + ) + timer_thread.start() + + handle_commands(pause, stop, reset) + + timer_thread.join() + print("\nTimer finished.") diff --git a/projects/final-project/solution/danieleFiocca/simple_time_display/current_time.py b/projects/final-project/solution/danieleFiocca/simple_time_display/current_time.py new file mode 100644 index 0000000..3484da2 --- /dev/null +++ b/projects/final-project/solution/danieleFiocca/simple_time_display/current_time.py @@ -0,0 +1,25 @@ +from datetime import datetime + + +def current_date_time(): + return datetime.now().strftime("%d/%m/%Y %H:%M:%S") + + +def day_of_the_week(): + return datetime.now().strftime("%A") + + +def day_of_the_year(): + return datetime.now().strftime("%j") + + +def week_number(): + return datetime.now().strftime("%W") + + +def show_date_time(): + print("--- DATA AND TIME INFORMATION ---") + print(f"\nDate and time: {current_date_time()}") + print(f"Day of the week: {day_of_the_week()}") + print(f"Day of the year: {day_of_the_year()}") + print(f"Week number: {week_number()}\n") diff --git a/projects/final-project/solution/danieleFiocca/simple_time_display/index.py b/projects/final-project/solution/danieleFiocca/simple_time_display/index.py new file mode 100644 index 0000000..8729318 --- /dev/null +++ b/projects/final-project/solution/danieleFiocca/simple_time_display/index.py @@ -0,0 +1,48 @@ +def show_menu(): + """Function to display menu""" + print("--- Simple Time Display ---") + print("1. Display time with additional date information") + print("2. Time zone conversion") + print("3. Set a countdown") + print("4. World clock collection") + print("5. Exit") + + +def choose_menu(): + """function to select in menu""" + choice = input("Select an option: ").strip() + return choice + + +def elaborate_choice(choice): + """function to elaborate choice""" + while choice in ["1", "2", "3", "4", "5"]: + if choice == "1": + from current_time import show_date_time + + show_date_time() + elif choice == "2": + from timezone_conversion import show_timezone_converted + + show_timezone_converted() + elif choice == "3": + from countdown_display import start_timer + + start_timer() + elif choice == "4": + from world_clock import world_clock + + world_clock() + elif choice == "5": + print("Thank you for using this program!") + exit() + else: + print("Invalid selection.") + + +def main(): + """Main function""" + while True: + show_menu() + choice = choose_menu() + elaborate_choice(choice) diff --git a/projects/final-project/solution/danieleFiocca/simple_time_display/main.py b/projects/final-project/solution/danieleFiocca/simple_time_display/main.py new file mode 100644 index 0000000..a072ece --- /dev/null +++ b/projects/final-project/solution/danieleFiocca/simple_time_display/main.py @@ -0,0 +1,4 @@ +from index import main + +if __name__ == "__main__": + main() diff --git a/projects/final-project/solution/danieleFiocca/simple_time_display/timezone_conversion.py b/projects/final-project/solution/danieleFiocca/simple_time_display/timezone_conversion.py new file mode 100644 index 0000000..e689cfd --- /dev/null +++ b/projects/final-project/solution/danieleFiocca/simple_time_display/timezone_conversion.py @@ -0,0 +1,49 @@ +from datetime import datetime +from zoneinfo import ZoneInfo + + +def get_timezones_available(): + """Function for retrieving available timezones.""" + return { + "1": ("Europe/Rome", "Rome, Italy"), + "2": ("Europe/London", "London, United Kingdom"), + "3": ("America/New_York", "New York, USA"), + "4": ("America/Los_Angeles", "Los Angeles, USA"), + "5": ("Asia/Tokyo", "Tokyo, Japan"), + "6": ("Asia/Shanghai", "Shanghai, China"), + "7": ("Australia/Sydney", "Sydney, Australia"), + "8": ("Europe/Paris", "Paris, France"), + "9": ("Asia/Dubai", "Dubai, UAE"), + "10": ("America/Sao_Paulo", "São Paulo, Brazil"), + } + + +def show_timezones(): + """function to display available timezones.""" + time_zones = get_timezones_available() + for timezone, (tz, names) in time_zones.items(): + print(f"{timezone:2s}: {names}") + + +def get_timezone_selection(): + """Function for to select available timezones.""" + timezones = get_timezones_available() + while True: + selection = input( + f"\nChoose a timezone to convert (1:{len(timezones)}): " + ).strip() + if selection in timezones: + return timezones[selection] + else: + print("Invalid selection") + + +def show_timezone_converted(): + """Function that displays the timezone selected by user""" + print("\n--- Time Zone Conversion ---") + print("\nSelect a time zone to see the current time in that part of the world") + print("\nAvailable timezones:") + show_timezones() + time_zone = get_timezone_selection() + display_time = datetime.now(ZoneInfo(time_zone[0])) + print(f"\nCurrent time in {time_zone[1]}: {display_time.strftime('%H:%M:%S')}") diff --git a/projects/final-project/solution/danieleFiocca/simple_time_display/world_clock.py b/projects/final-project/solution/danieleFiocca/simple_time_display/world_clock.py new file mode 100644 index 0000000..836e0ef --- /dev/null +++ b/projects/final-project/solution/danieleFiocca/simple_time_display/world_clock.py @@ -0,0 +1,116 @@ +from datetime import datetime +from zoneinfo import ZoneInfo + + +def timezone_list(): + """Function that returns a list of all timezone names.""" + return { + "1": ("Europe/Rome", "Rome, Italy"), + "2": ("Europe/London", "London, United Kingdom"), + "3": ("America/New_York", "New York, USA"), + "4": ("America/Los_Angeles", "Los Angeles, USA"), + "5": ("Asia/Tokyo", "Tokyo, Japan"), + "6": ("Asia/Shanghai", "Shanghai, Cina"), + "7": ("Australia/Sydney", "Sydney, Australia"), + "8": ("Europe/Paris", "Parigi, France"), + "9": ("Asia/Dubai", "Dubai, UAE"), + "10": ("America/Sao_Paulo", "San Paolo, Brazil"), + } + + +def display_available_timezones(): + print("Available timezones:") + for key, (tz, label) in timezone_list().items(): + print(f"{key}: {label}") + + +def select_timezone(): + timezones = timezone_list() + selection = input( + f"Which timezone would you like to select (1: {len(timezones)})? " + ).strip() + if selection in timezones: + return timezones[selection] + + else: + print("That's not a valid timezone!") + return None + + +def add_clock(clocks): + """Function that adds a clock to the clocks list.""" + timezone = select_timezone() + if timezone and timezone not in clocks: + clocks.append(timezone) + print(f"Added {timezone[1]}") + elif timezone: + print(f"Timezone {timezone[1]} already exists") + return clocks + + +def get_clock_to_remove(clocks): + """Function that returns the timezone name to remove.""" + while True: + try: + index = int( + input(f"Which timezone would you like to remove (1: {len(clocks)})? ") + ) + if 0 <= index < len(clocks): + return index + else: + print("That's not a valid timezone!") + except ValueError: + print("That's not a valid timezone!") + + +def remove_clock(clocks): + """Function that removes a clock from the clocks list.""" + if not clocks: + print("No clocks to remove") + return + + print("--- REMOVE CLOCK ---") + display_world_clocks(clocks) + + index = get_clock_to_remove(clocks) + removed = clocks.pop(index - 1) + print(f"Removed {removed[1]}") + + +def display_world_clocks(clocks): + """Function that displays the clocks list.""" + print("\n--- WORLD CLOCK ---") + for tz_str, label in clocks: + now = datetime.now(ZoneInfo(tz_str)) + print(f'{label:25s}: {now.strftime("%Y-%m-%d %H:%M:%S")}') + print("============") + + +def world_clock(): + """Function that displays the world clock.""" + clocks = [] + while True: + print("\nMenu:") + print("1. Show timezone clocks") + print("2. Add timezone clocks") + print("3. Remove timezone clocks") + print("4. Exit") + choice = input("Select an option: ").strip() + if choice == "1": + if clocks: + display_world_clocks(clocks) + else: + print("No clocks added yet") + + elif choice == "2": + display_available_timezones() + add_clock(clocks) + + elif choice == "3": + remove_clock(clocks) + + elif choice == "4": + print("Quitting") + break + else: + print("That's not a valid option!")