Python 25: New Year Countdown

gregorschafroth

Gregor Schafroth

Posted on December 31, 2023

Python 25: New Year Countdown

Alright itโ€™s the last day of the year, so I asked ChatGPT to give me a Python exercises related to that and here is what I got.

You can find my solution below ๐Ÿ˜Š

Exercise: New Year Countdown

Write a Python program that calculates and displays a countdown to the end of the year 2023. The program should:

  1. Calculate the number of days, hours, minutes, and seconds remaining until the end of the year 2023 from the current date and time.
  2. Display the countdown in the following format:
    • "Days: X, Hours: Y, Minutes: Z, Seconds: W"

Your program should use Python's datetime module to work with dates and times. You can calculate the time difference between the current date and time and the end of the year 2023, and then format the result to display the countdown.

Below is my solution. I just spruced it up a bit by displaying the result as a pyfiglet and wishing the user a happy new year if itโ€™s already 2024. As always I used my beloved logging module, even tho it does not show anything in this code when the level is set to ERROR. Change this to DEBUG if you want to see the messages ๐Ÿ™‚

import datetime
import logging
import pyfiglet

logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s: %(message)s')

def time_until_2024():
    """
    Calculates the number of days, hours, minutes, and seconds remaining until the end of the year 2023 from the current date and time.
    """
    logging.debug('time_until_2024()')

    # Get the current date and time
    current_datetime = datetime.datetime.now()

    # Define the end of the year 2023
    end_of_year_2023 = datetime.datetime(2023, 12, 31, 23, 59, 59)

    # Calculate the time difference
    time_difference = end_of_year_2023 - current_datetime

    # Return the time difference
    logging.debug(f'time_until_2024() return: {time_difference}')
    return time_difference

def print_time(time_remaining):
    """
    Display the countdown in the following format: "Days: X, Hours: Y, Minutes: Z, Seconds: W"
    """
    logging.debug(f'print_time({time_remaining})')

    # Create Happy New Year text if it's already 2024
    current_datetime = datetime.datetime.now()
    if current_datetime.year >= 2024:
        print_message = pyfiglet.figlet_format("It's 2024, Happy New Year!")

    else:
        # Extract days, seconds, and microseconds from the time difference
        remaining_days = time_remaining.days
        remaining_seconds = time_remaining.seconds
        remaining_microseconds = time_remaining.microseconds

        # Calculate hours, minutes, and remaining seconds
        remaining_hours, remaining_seconds = divmod(remaining_seconds, 3600)
        remaining_minutes, remaining_seconds = divmod(remaining_seconds, 60)

        # Create stylized text art for the countdown result
        print_message = pyfiglet.figlet_format(f'Days: {remaining_days},\n Hours: {remaining_hours},\n Minutes: {remaining_minutes},\n Seconds: {remaining_seconds}')

    # Display the message
    print(print_message)

def main():
    time_remaining = time_until_2024()
    print_time(time_remaining)
    logging.debug('main() end')

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode
๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
gregorschafroth
Gregor Schafroth

Posted on December 31, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

ยฉ TheLazy.dev

About