diff --git a/django/contrib/sessions/backends/base.py b/django/contrib/sessions/backends/base.py index 920b3e68cd..4ddb35a832 100644 --- a/django/contrib/sessions/backends/base.py +++ b/django/contrib/sessions/backends/base.py @@ -12,6 +12,7 @@ except ImportError: from django.conf import settings from django.core.exceptions import SuspiciousOperation from django.utils.crypto import constant_time_compare, salted_hmac +from django.utils import timezone # Use the system (hardware-based) random number generator if it exists. if hasattr(random, 'SystemRandom'): @@ -188,7 +189,7 @@ class SessionBase(object): return settings.SESSION_COOKIE_AGE if not isinstance(expiry, datetime): return expiry - delta = expiry - datetime.now() + delta = expiry - timezone.now() return delta.days * 86400 + delta.seconds def get_expiry_date(self): @@ -198,7 +199,7 @@ class SessionBase(object): return expiry if not expiry: # Checks both None and 0 cases expiry = settings.SESSION_COOKIE_AGE - return datetime.now() + timedelta(seconds=expiry) + return timezone.now() + timedelta(seconds=expiry) def set_expiry(self, value): """ @@ -223,7 +224,7 @@ class SessionBase(object): pass return if isinstance(value, timedelta): - value = datetime.now() + value + value = timezone.now() + value self['_session_expiry'] = value def get_expire_at_browser_close(self): diff --git a/django/contrib/sessions/backends/db.py b/django/contrib/sessions/backends/db.py index 220046fefb..7d4c535fd4 100644 --- a/django/contrib/sessions/backends/db.py +++ b/django/contrib/sessions/backends/db.py @@ -1,8 +1,8 @@ -import datetime from django.contrib.sessions.backends.base import SessionBase, CreateError from django.core.exceptions import SuspiciousOperation from django.db import IntegrityError, transaction, router from django.utils.encoding import force_unicode +from django.utils import timezone class SessionStore(SessionBase): @@ -16,7 +16,7 @@ class SessionStore(SessionBase): try: s = Session.objects.get( session_key = self.session_key, - expire_date__gt=datetime.datetime.now() + expire_date__gt=timezone.now() ) return self.decode(force_unicode(s.session_data)) except (Session.DoesNotExist, SuspiciousOperation): diff --git a/django/contrib/sessions/tests.py b/django/contrib/sessions/tests.py index 776e03c081..ce39fef0ad 100644 --- a/django/contrib/sessions/tests.py +++ b/django/contrib/sessions/tests.py @@ -16,6 +16,7 @@ from django.core.exceptions import ImproperlyConfigured, SuspiciousOperation from django.http import HttpResponse from django.test import TestCase, RequestFactory from django.test.utils import override_settings +from django.utils import timezone from django.utils import unittest @@ -187,7 +188,7 @@ class SessionTestsMixin(object): def test_custom_expiry_seconds(self): # Using seconds self.session.set_expiry(10) - delta = self.session.get_expiry_date() - datetime.now() + delta = self.session.get_expiry_date() - timezone.now() self.assertTrue(delta.seconds in (9, 10)) age = self.session.get_expiry_age() @@ -196,7 +197,7 @@ class SessionTestsMixin(object): def test_custom_expiry_timedelta(self): # Using timedelta self.session.set_expiry(timedelta(seconds=10)) - delta = self.session.get_expiry_date() - datetime.now() + delta = self.session.get_expiry_date() - timezone.now() self.assertTrue(delta.seconds in (9, 10)) age = self.session.get_expiry_age() @@ -204,8 +205,8 @@ class SessionTestsMixin(object): def test_custom_expiry_datetime(self): # Using fixed datetime - self.session.set_expiry(datetime.now() + timedelta(seconds=10)) - delta = self.session.get_expiry_date() - datetime.now() + self.session.set_expiry(timezone.now() + timedelta(seconds=10)) + delta = self.session.get_expiry_date() - timezone.now() self.assertTrue(delta.seconds in (9, 10)) age = self.session.get_expiry_age() @@ -279,10 +280,17 @@ class DatabaseSessionTests(SessionTestsMixin, TestCase): self.assertEqual(self.session['y'], 2) +DatabaseSessionWithTimeZoneTests = override_settings(USE_TZ=True)(DatabaseSessionTests) + + class CacheDBSessionTests(SessionTestsMixin, TestCase): backend = CacheDBSession + +CacheDBSessionWithTimeZoneTests = override_settings(USE_TZ=True)(CacheDBSessionTests) + + # Don't need DB flushing for these tests, so can use unittest.TestCase as base class class FileSessionTests(SessionTestsMixin, unittest.TestCase):