support utc_offset
This commit is contained in:
parent
5428d97335
commit
b112692d9d
1 changed files with 23 additions and 13 deletions
|
@ -20,6 +20,13 @@ def index(request):
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def city_time(request, city_name):
|
def city_time(request, city_name):
|
||||||
try:
|
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)
|
geolocator = Nominatim(user_agent="Ubottu", timeout=10)
|
||||||
location = geolocator.geocode(city_name, exactly_one=True, language='en')
|
location = geolocator.geocode(city_name, exactly_one=True, language='en')
|
||||||
if location is None:
|
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)
|
return Response({'error': 'Timezone not found for the given location'}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
timezone = pytz.timezone(timezone_str)
|
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
|
||||||
|
|
Loading…
Reference in a new issue