File size: 1,180 Bytes
3c8f4c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st

# Conversion factors for various time units
TIME_UNITS = {
    'Seconds': 1,
    'Minutes': 60,
    'Hours': 3600,
    'Days': 86400,
    'Weeks': 604800,
    'Months (30 days)': 2592000,
    'Years (365 days)': 31536000
}

def convert_time(value, from_unit, to_unit):
    # Convert the input value to seconds
    value_in_seconds = value * TIME_UNITS[from_unit]
    
    # Convert from seconds to the target unit
    converted_value = value_in_seconds / TIME_UNITS[to_unit]
    
    return converted_value

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

    # Input value and select time units
    value = st.number_input("Enter the value to convert", min_value=0.0, step=0.1)
    
    from_unit = st.selectbox("From unit", list(TIME_UNITS.keys()))
    to_unit = st.selectbox("To unit", list(TIME_UNITS.keys()))

    if st.button('Convert'):
        if value >= 0:
            converted_value = convert_time(value, from_unit, to_unit)
            st.write(f"{value} {from_unit} = {converted_value:.4f} {to_unit}")
        else:
            st.error("Please enter a valid value (greater than or equal to 0).")
    
if __name__ == "__main__":
    main()