app.py
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 |
from geopy.geocoders import Nominatim from timezonefinder import TimezoneFinder from datetime import datetime import pytz def get_time_in_city(city_name): # Get coordinates from city name geolocator = Nominatim(user_agent="city_time_app") location = geolocator.geocode(city_name) if location: lat, lon = location.latitude, location.longitude print(f"Coordinates of {city_name}: Latitude: {lat}, Longitude: {lon}") # Get timezone using latitude and longitude tz_finder = TimezoneFinder() timezone_str = tz_finder.timezone_at(lng=lon, lat=lat) if timezone_str: print(f"Timezone of {city_name}: {timezone_str}") # Get current time in that timezone timezone = pytz.timezone(timezone_str) city_time = datetime.now(timezone) print(f"Current time in {city_name}: {city_time.strftime('%Y-%m-%d %H:%M:%S')}") else: print(f"Could not find the timezone for {city_name}") else: print(f"City {city_name} not found.") # Example usage city_name = input("Enter the city name: ") get_time_in_city(city_name) |