File size: 3,489 Bytes
eca58c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import streamlit as st
import time
import pytz
from datetime import datetime, timedelta

# Helper Functions
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}"

# Streamlit App
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"))

# --- World Clock --- #
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()

# --- Alarm Clock --- #
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)

# --- Stopwatch --- #
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))

# --- Timer --- #
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!")