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,6 +20,13 @@ def index(request):
@api_view(['GET'])
def city_time(request, city_name):
try:
if city_name.upper() == 'UTC':
city_name = 'UTC'
datetime_obj = datetime.now(pytz.utc)
local_time = datetime_obj.strftime('%A, %d %B %Y, %H:%M')
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:
@ -34,10 +41,13 @@ def city_time(request, city_name):
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)
local_time = datetime_obj.strftime('%A, %d %B %Y, %H:%M')
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)
except Exception as e:
# Log the exception if needed