Ticket #17263: 17263.patch

File 17263.patch, 6.5 KB (added by Aymeric Augustin, 13 years ago)
  • docs/topics/i18n/timezones.txt

     
    106106----------------------------------------
    107107
    108108When :setting:`USE_TZ` is ``True``, Django still accepts naive datetime
    109 objects, in order to preserve backwards-compatibility. It attempts to make them
    110 aware by interpreting them in the :ref:`default time zone
    111 <default-current-time-zone>`.
     109objects, in order to preserve backwards-compatibility. When the database layer
     110receives one, it attempts to make it aware by interpreting it in the
     111:ref:`default time zone <default-current-time-zone>` and raises a warning.
    112112
    113113Unfortunately, during DST transitions, some datetimes don't exist or are
    114114ambiguous. In such situations, pytz_ raises an exception. Other
     
    421421So the second step is to refactor your code wherever you instanciate datetime
    422422objects to make them aware. This can be done incrementally.
    423423:mod:`django.utils.timezone` defines some handy helpers for compatibility
    424 code: :func:`~django.utils.timezone.is_aware`,
     424code: :func:`~django.utils.timezone.now`,
     425:func:`~django.utils.timezone.is_aware`,
    425426:func:`~django.utils.timezone.is_naive`,
    426427:func:`~django.utils.timezone.make_aware`, and
    427428:func:`~django.utils.timezone.make_naive`.
    428429
     430Finally, in order to help you locate code that needs upgrading, Django raises
     431a warning when you attempt to save a naive datetime to the database. During
     432development, you can turn such warnings into exceptions and get a traceback
     433by adding to your settings file::
     434
     435    import warnings
     436    warnings.filterwarnings(
     437            'error', r"DateTimeField received a naive datetime",
     438            RuntimeWarning, r'django\.db\.models\.fields')
     439
    429440.. _pytz: http://pytz.sourceforge.net/
    430441.. _these issues: http://pytz.sourceforge.net/#problems-with-localtime
    431442.. _tz database: http://en.wikipedia.org/wiki/Tz_database
  • django/db/models/fields/__init__.py

     
    22import datetime
    33import decimal
    44import math
     5import warnings
    56from itertools import tee
    67
    78from django.db import connection
     
    789790            # For backwards compatibility, interpret naive datetimes in local
    790791            # time. This won't work during DST change, but we can't do much
    791792            # about it, so we let the exceptions percolate up the call stack.
     793            warnings.warn(u"DateTimeField received a naive datetime (%s)"
     794                          u" while time zone support is active." % value,
     795                          RuntimeWarning)
    792796            default_timezone = timezone.get_default_timezone()
    793797            value = timezone.make_aware(value, default_timezone)
    794798        return value
  • tests/modeltests/timezones/fixtures/users.xml

     
    99        <field type="BooleanField" name="is_staff">True</field>
    1010        <field type="BooleanField" name="is_active">True</field>
    1111        <field type="BooleanField" name="is_superuser">True</field>
    12         <field type="DateTimeField" name="last_login">2007-05-30 13:20:10</field>
    13         <field type="DateTimeField" name="date_joined">2007-05-30 13:20:10</field>
     12        <field type="DateTimeField" name="last_login">2001-01-01 00:00:00+00:00</field>
     13        <field type="DateTimeField" name="date_joined">2001-01-01 00:00:00+00:00</field>
    1414        <field to="auth.group" name="groups" rel="ManyToManyRel"></field>
    1515        <field to="auth.permission" name="user_permissions" rel="ManyToManyRel"></field>
    1616    </object>
    17 </django-objects>
    18  No newline at end of file
     17</django-objects>
  • tests/modeltests/timezones/tests.py

     
    33import datetime
    44import os
    55import time
     6import warnings
    67
    78try:
    89    import pytz
     
    238239
    239240    def test_naive_datetime(self):
    240241        dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
    241         Event.objects.create(dt=dt)
     242        with warnings.catch_warnings(record=True) as recorded:
     243            Event.objects.create(dt=dt)
     244            self.assertEqual(len(recorded), 1)
     245            msg = str(recorded[0].message)
     246            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
    242247        event = Event.objects.get()
    243248        # naive datetimes are interpreted in local time
    244249        self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
     
    246251    @skipUnlessDBFeature('supports_microsecond_precision')
    247252    def test_naive_datetime_with_microsecond(self):
    248253        dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
    249         Event.objects.create(dt=dt)
     254        with warnings.catch_warnings(record=True) as recorded:
     255            Event.objects.create(dt=dt)
     256            self.assertEqual(len(recorded), 1)
     257            msg = str(recorded[0].message)
     258            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
    250259        event = Event.objects.get()
    251260        # naive datetimes are interpreted in local time
    252261        self.assertEqual(event.dt, dt.replace(tzinfo=EAT))
     
    254263    @skipIfDBFeature('supports_microsecond_precision')
    255264    def test_naive_datetime_with_microsecond_unsupported(self):
    256265        dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
    257         Event.objects.create(dt=dt)
     266        with warnings.catch_warnings(record=True) as recorded:
     267            Event.objects.create(dt=dt)
     268            self.assertEqual(len(recorded), 1)
     269            msg = str(recorded[0].message)
     270            self.assertTrue(msg.startswith("DateTimeField received a naive datetime"))
    258271        event = Event.objects.get()
    259272        # microseconds are lost during a round-trip in the database
    260273        # naive datetimes are interpreted in local time
     
    294307        self.assertEqual(event.dt, dt)
    295308
    296309    def test_auto_now_and_auto_now_add(self):
    297         now = datetime.datetime.utcnow().replace(tzinfo=UTC)
     310        now = timezone.now()
    298311        past = now - datetime.timedelta(seconds=2)
    299312        future = now + datetime.timedelta(seconds=2)
    300313        Timestamp.objects.create()
Back to Top