new-year-timer / app.py
Ivan000's picture
Update app.py
ba5ec10 verified
# app.py
# =============
# This is a complete app.py file for a countdown timer to New Year 2025 in multiple time zones.
import gradio as gr
from datetime import datetime, timedelta
import pytz
# Define a function to calculate the time remaining until New Year 2025 in the specified time zone
def countdown_to_new_year(timezone: str):
try:
# Get the current time in the specified time zone
tz = pytz.timezone(timezone)
now = datetime.now(tz)
# Define the target date and time for New Year 2025
new_year_2025 = tz.localize(datetime(2025, 1, 1, 0, 0, 0))
# Check if New Year has already passed
if now >= new_year_2025:
return f"Happy New Year 2025!\nCurrent time in {timezone}: {now.strftime('%Y-%m-%d %H:%M:%S')}"
# Calculate the time remaining
time_remaining = new_year_2025 - now
# Extract days, hours, minutes, and seconds
days = time_remaining.days
seconds = time_remaining.seconds
hours = seconds // 3600
minutes = (seconds % 3600) // 60
seconds = seconds % 60
return (
f"{days} days, {hours} hours, {minutes} minutes, {seconds} seconds remaining\n"
f"Current time in {timezone}: {now.strftime('%Y-%m-%d %H:%M:%S')}"
)
except Exception as e:
return f"Error: {str(e)}"
# Create a list of common time zones for the user to choose from
TIME_ZONES = pytz.all_timezones
# Define the Gradio interface
def build_interface():
with gr.Blocks() as app:
gr.Markdown("""
# Countdown to New Year 2025
Select a time zone to see how much time remains until New Year 2025!
""")
timezone_selector = gr.Dropdown(
label="Select Time Zone",
choices=TIME_ZONES,
value="UTC"
)
countdown_display = gr.Textbox(label="Countdown and Current Time", interactive=False)
calculate_button = gr.Button("Calculate Countdown")
calculate_button.click(
fn=countdown_to_new_year,
inputs=[timezone_selector],
outputs=[countdown_display]
)
return app
# Build and launch the app
if __name__ == "__main__":
app = build_interface()
app.launch()
# Dependencies
# =============
# The following dependencies are required to run this app:
# - gradio
# - pytz
#
# You can install these dependencies using pip:
# pip install gradio pytz