support utc_offset

This commit is contained in:
Nils Büchner 2024-09-14 12:21:02 +02:00
parent 5428d97335
commit b112692d9d

View file

@ -20,24 +20,34 @@ def index(request):
@api_view(['GET']) @api_view(['GET'])
def city_time(request, city_name): def city_time(request, city_name):
try: try:
geolocator = Nominatim(user_agent="Ubottu", timeout=10) if city_name.upper() == 'UTC':
location = geolocator.geocode(city_name, exactly_one=True, language='en') city_name = 'UTC'
if location is None: datetime_obj = datetime.now(pytz.utc)
# If the location wasn't found, return an appropriate response local_time = datetime_obj.strftime('%A, %d %B %Y, %H:%M')
return Response({'error': 'Location not found'}, status=status.HTTP_404_NOT_FOUND) data = {'location': '', 'city': city_name, 'local_time': local_time, 'utc_offset': 'UTC+00:00'}
return Response(data)
else:
geolocator = Nominatim(user_agent="Ubottu", timeout=10)
location = geolocator.geocode(city_name, exactly_one=True, language='en')
if location is None:
# If the location wasn't found, return an appropriate response
return Response({'error': 'Location not found'}, status=status.HTTP_404_NOT_FOUND)
tf = TimezoneFinder() tf = TimezoneFinder()
timezone_str = tf.timezone_at(lat=location.latitude, lng=location.longitude) # Get the timezone name timezone_str = tf.timezone_at(lat=location.latitude, lng=location.longitude) # Get the timezone name
if timezone_str is None:
# If the timezone wasn't found, return an appropriate response
return Response({'error': 'Timezone not found for the given location'}, status=status.HTTP_404_NOT_FOUND)
if timezone_str is None: timezone = pytz.timezone(timezone_str)
# If the timezone wasn't found, return an appropriate response
return Response({'error': 'Timezone not found for the given location'}, status=status.HTTP_404_NOT_FOUND)
timezone = pytz.timezone(timezone_str)
datetime_obj = datetime.now(timezone) datetime_obj = datetime.now(timezone)
local_time = datetime_obj.strftime('%A, %d %B %Y, %H:%M') local_time = datetime_obj.strftime('%A, %d %B %Y, %H:%M')
city_name = str(location).split(',')[0] city_name = str(location).split(',')[0]
data = {'location': str(location), 'city': city_name, 'local_time': local_time} utc_offset = datetime_obj.strftime('%z')
formatted_offset = f"UTC{utc_offset[:3]}:{utc_offset[3:]}"
data = {'location': str(location), 'city': city_name, 'local_time': local_time, 'utc_offset': formatted_offset}
return Response(data) return Response(data)
except Exception as e: except Exception as e:
# Log the exception if needed # Log the exception if needed