如果我理解正确,你必须改变当地人
DateTime
UTC
import pytz
from datetime import datetime
def convert_to_UTC(local_tz,dt_1,dt_2):
"""
local_tz : any possible timezone which supports pytz lib (https://stackoverflow.com/questions/13866926/is-there-a-list-of-pytz-timezones)
dt_1 and dt_2 : local datetime in string in this format ->> '%Y-%m-%dT%H:%M:%S'
return a list as ->> [utc_equivalent_of_dt_1_in_string,utc_equivalent_of_dt_2_in_string]
"""
format='%Y-%m-%dT%H:%M:%S'
pytz_local_tz = pytz.timezone(local_time_zone)
dt_obj_from_date_time = datetime.strptime(dt_1,format)
dt_obj_to_date_time = datetime.strptime(dt_2,format)
return [pytz_local_tz.localize(dt_obj_from_date_time).astimezone(tz=pytz.utc).strftime(format),
pytz_local_tz.localize(dt_obj_to_date_time).astimezone(tz=pytz.utc).strftime(format)]
要使用此功能,请更改
get_queryset()
def get_queryset(self):
user = self.request.user
entities = Entity.objects.filter(owner=user)
date_from = self.request.query_params.get('date_from')
date_to = self.request.query_params.get('date_to')
if date_from and date_to:
entities = entities.filter(date__range=convert_to_UTC('Asia/Kolkata', date_from, date_to))
return entities
/api/v1/entity/?date_from=2018-06-18T23:00:00&date_to=2018-06-19T23:00:00