| import streamlit as st |
| import time |
| import pytz |
| from datetime import datetime, timedelta |
|
|
| |
| def get_timezones(): |
| return ['UTC', 'US/Eastern', 'Europe/London', 'Asia/Kolkata', 'Asia/Tokyo', 'Australia/Sydney'] |
|
|
| def format_time(seconds): |
| mins, secs = divmod(seconds, 60) |
| hours, mins = divmod(mins, 60) |
| return f"{int(hours):02d}:{int(mins):02d}:{int(secs):02d}" |
|
|
| |
| st.set_page_config(page_title="Clock App", page_icon="🕰️", layout="centered") |
| st.title("🕰️ Clock Application") |
|
|
| menu = st.sidebar.radio("Select Function", ("World Clock", "Alarm Clock", "Stopwatch", "Timer")) |
|
|
| |
| if menu == "World Clock": |
| st.header("🌎 World Clock") |
| selected_timezones = st.multiselect("Choose timezones", get_timezones(), default=['UTC']) |
| |
| placeholder = st.empty() |
| while True: |
| with placeholder.container(): |
| for tz in selected_timezones: |
| timezone = pytz.timezone(tz) |
| time_now = datetime.now(timezone).strftime("%Y-%m-%d %H:%M:%S") |
| st.write(f"**{tz}**: {time_now}") |
| time.sleep(1) |
| placeholder.empty() |
|
|
| |
| elif menu == "Alarm Clock": |
| st.header("⏰ Alarm Clock") |
| alarm_time = st.time_input("Set Alarm Time") |
| alarm_set = st.button("Set Alarm") |
| |
| if alarm_set: |
| st.success(f"Alarm is set for {alarm_time}!") |
| while True: |
| now = datetime.now().time() |
| if (now.hour, now.minute) == (alarm_time.hour, alarm_time.minute): |
| st.balloons() |
| st.success("⏰ It's Time!") |
| break |
| time.sleep(20) |
|
|
| |
| elif menu == "Stopwatch": |
| st.header("⏱️ Stopwatch") |
| |
| if "stopwatch_running" not in st.session_state: |
| st.session_state.stopwatch_running = False |
| st.session_state.start_time = None |
| st.session_state.elapsed = 0 |
|
|
| col1, col2, col3 = st.columns(3) |
| |
| with col1: |
| if st.button("Start"): |
| if not st.session_state.stopwatch_running: |
| st.session_state.start_time = time.time() - st.session_state.elapsed |
| st.session_state.stopwatch_running = True |
|
|
| with col2: |
| if st.button("Pause"): |
| if st.session_state.stopwatch_running: |
| st.session_state.elapsed = time.time() - st.session_state.start_time |
| st.session_state.stopwatch_running = False |
|
|
| with col3: |
| if st.button("Reset"): |
| st.session_state.start_time = None |
| st.session_state.elapsed = 0 |
| st.session_state.stopwatch_running = False |
|
|
| st.subheader("Elapsed Time") |
| placeholder = st.empty() |
|
|
| while st.session_state.stopwatch_running: |
| elapsed = time.time() - st.session_state.start_time |
| placeholder.subheader(format_time(elapsed)) |
| time.sleep(0.1) |
| else: |
| placeholder.subheader(format_time(st.session_state.elapsed)) |
|
|
| |
| elif menu == "Timer": |
| st.header("⏲️ Timer") |
| |
| minutes = st.number_input("Set Timer (minutes)", min_value=0, max_value=120, value=1) |
| start_timer = st.button("Start Timer") |
|
|
| if start_timer: |
| total_seconds = minutes * 60 |
| placeholder = st.empty() |
|
|
| for remaining in range(total_seconds, -1, -1): |
| placeholder.subheader(format_time(remaining)) |
| time.sleep(1) |
| st.balloons() |
| st.success("⏳ Time's up!") |
|
|