File size: 1,311 Bytes
806b48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from datetime import datetime
import pytz

# List of available timezones
TIMEZONES = pytz.all_timezones

def convert_time(time_str, from_timezone, to_timezone):
    # Parse the input time string
    time_obj = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
    
    # Set the from timezone
    from_tz = pytz.timezone(from_timezone)
    time_obj = from_tz.localize(time_obj)
    
    # Convert to the destination timezone
    to_tz = pytz.timezone(to_timezone)
    converted_time = time_obj.astimezone(to_tz)
    
    return converted_time.strftime('%Y-%m-%d %H:%M:%S')

def main():
    st.title('Time Zone Converter')

    # Input the time, from and to time zones
    time_input = st.text_input("Enter time (YYYY-MM-DD HH:MM:SS)", "2024-12-10 12:00:00")
    
    from_timezone = st.selectbox("From Time Zone", TIMEZONES)
    to_timezone = st.selectbox("To Time Zone", TIMEZONES)

    if st.button('Convert'):
        if time_input:
            try:
                converted_time = convert_time(time_input, from_timezone, to_timezone)
                st.write(f"Converted Time: {converted_time}")
            except Exception as e:
                st.error(f"Error: {str(e)}")
        else:
            st.error("Please enter a valid time.")
    
if __name__ == "__main__":
    main()