From e7231e33e2d3e5fea40ff3a85895a68de7501457 Mon Sep 17 00:00:00 2001 From: Augusto Destrero Date: Thu, 28 Jul 2016 14:54:57 +0200 Subject: [PATCH] Make start and end dates aware only if USE_TZ=True in django settings --- schedule/views.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/schedule/views.py b/schedule/views.py index e115d81c..a0c0abe8 100644 --- a/schedule/views.py +++ b/schedule/views.py @@ -15,6 +15,7 @@ from django.views.generic.edit import ( UpdateView, CreateView, DeleteView, ModelFormMixin, ProcessFormView) from django.utils.http import is_safe_url +from django.conf import settings from schedule.conf.settings import (GET_EVENTS_FUNC, OCCURRENCE_CANCEL_REDIRECT, EVENT_NAME_PLACEHOLDER, CHECK_EVENT_PERM_FUNC, @@ -305,7 +306,7 @@ def api_occurrences(request): def _api_occurrences(start, end, calendar_slug): - utc = pytz.UTC + if not start or not end: raise ValueError('Start and end parameters are required') # version 2 of full calendar @@ -319,8 +320,14 @@ def convert(ddatetime): def convert(ddatetime): return datetime.datetime.utcfromtimestamp(float(ddatetime)) - start = utc.localize(convert(start)) - end = utc.localize(convert(end)) + start = convert(start) + end = convert(end) + # If USE_TZ is True, make start and end dates aware in UTC timezone + if settings.USE_TZ: + utc = pytz.UTC + start = utc.localize(start) + end = utc.localize(end) + if calendar_slug: # will raise DoesNotExist exception if no match calendars = [Calendar.objects.get(slug=calendar_slug)]