Closed DuroCodes closed 2 hours ago
it doesn't even support all timezones. get mogged:
from datetime import datetime, timedelta, timezone timezones = { -10: ["HI"], -8: ["AK"], -7: ["CA", "NV", "OR", "WA"], -6: [ "AZ", "CO", "ID", "MT", "NM", "UT", "WY", "TX", ], -5: [ "AL", "AR", "IL", "IA", "KS", "LA", "MN", "MS", "MO", "NE", "ND", "OK", "SD", "TX", "WI", "KY", "TN", "IN", ], -4: [ "CT", "DE", "DC", "FL", "GA", "IN", "KY", "ME", "MD", "MA", "MI", "NJ", "NY", "NC", "OH", "PA", "RI", "SC", "TN", "VT", "VA", "WV", ], } state_to_abbreviation = { "Alabama": "AL", "Alaska": "AK", "Arizona": "AZ", "Arkansas": "AR", "California": "CA", "Colorado": "CO", "Connecticut": "CT", "Delaware": "DE", "District of Columbia": "DC", "Florida": "FL", "Georgia": "GA", "Hawaii": "HI", "Idaho": "ID", "Illinois": "IL", "Indiana": "IN", "Iowa": "IA", "Kansas": "KS", "Kentucky": "KY", "Louisiana": "LA", "Maine": "ME", "Maryland": "MD", "Massachusetts": "MA", "Michigan": "MI", "Minnesota": "MN", "Mississippi": "MS", "Missouri": "MO", "Montana": "MT", "Nebraska": "NE", "Nevada": "NV", "New Hampshire": "NH", "New Jersey": "NJ", "New Mexico": "NM", "New York": "NY", "North Carolina": "NC", "North Dakota": "ND", "Ohio": "OH", "Oklahoma": "OK", "Oregon": "OR", "Pennsylvania": "PA", "Rhode Island": "RI", "South Carolina": "SC", "South Dakota": "SD", "Tennessee": "TN", "Texas": "TX", "Utah": "UT", "Vermont": "VT", "Virginia": "VA", "Washington": "WA", "West Virginia": "WV", "Wisconsin": "WI", "Wyoming": "WY", } abbreviation_to_state = {v: k for k, v in state_to_abbreviation.items()} state_timezones = { state_abbreviation: tz for tz, states in timezones.items() for state_abbreviation in states } state_timezones_map: dict[str, list[int]] = {} for tz, states in timezones.items(): for state in states: if state not in state_timezones_map: state_timezones_map[state] = [] state_timezones_map[state].append(tz) def get_time_and_timezone(state: str) -> str: state = state.strip().upper() full_state = ( abbreviation_to_state[state] if state in abbreviation_to_state else state.title() ) state_abbr = state_to_abbreviation.get(full_state) if not state_abbr: return f"Error: '{state}' is not a valid US state." applicable_timezones = state_timezones_map.get(state_abbr) if not applicable_timezones: return f"Error: '{state}' is not a valid US state." now_utc = datetime.now(timezone.utc) local_times = [] for tz in applicable_timezones: local_time = now_utc + timedelta(hours=tz) current_time = local_time.strftime("%I:%M %p") local_times.append(f"UTC{tz}: {current_time}") local_times_str = "\n".join(f"- {x}" for x in local_times) multiple_times = len(applicable_timezones) > 1 return f"The current time{'s' if multiple_times else ''} in {full_state} {'are' if multiple_times else 'is'}:\n{local_times_str}" state_input = input("Enter a US state: ") result = get_time_and_timezone(state_input) print(result)
thanks
your welcome!
thanks for the gold kind stranger
it doesn't even support all timezones. get mogged: