1
0
mirror of https://github.com/django/django.git synced 2025-02-09 08:55:32 +00:00

ticket #34699. fixed documentation spel chek error. Reformatted example to be inside warning block.

This commit is contained in:
wesley 2024-10-09 18:10:56 -04:00
parent 99446762fc
commit a603dbabea

View File

@ -660,38 +660,36 @@ Usage example:
.. warning::
Trunc functions, at the database level, return a timezone naive value which
is converted to a timezone aware value by the ORM. When you use a Trunc
function in a filter you will need to remember that it is a timezone naive
value. This can lead to unexpected results if you are using timezones other
than UTC. Django will store date/time values in the database in the UTC
timezone. The following example demonstrates what happens when using the
timezone "Europe/Berlin" and how to adjust for this:
``Trunc`` functions, at the database level, return a timezone naive value
which is converted to a timezone aware value by the ORM. When you use a
``Trunc`` function in a filter you will need to remember that it is a
timezone naive value. This can lead to unexpected results if you are using
timezones other than UTC. Django will store date/time values in the
database in the UTC timezone. The following example demonstrates what
happens when using the timezone "Europe/Berlin" and how to adjust for this:
Filter example using the "Europe/Berlin" timezone.:
.. code-block:: pycon
.. code-block:: pycon
>>> from django.utils import timezone
>>> from datetime import datetime
>>> from django.db.models.functions import TruncSecond
>>> import zoneinfo
>>> start = datetime(2015, 6, 15, 14, 30, 50, 321)
>>> start = timezone.make_aware(start)
>>> exp = Experiment.objects.create(start_datetime=start)
>>> find_this_exp = Experiment.objects.annotate(
... trunc_start=TruncSecond("start_datetime")
... ).filter(trunc_start__lte=start)
>>> find_this_exp.count() # We expect to find one result but 0 are found
...
0
>>> start_adjusted = timezone.localtime(start).replace(tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> find_this_exp_adjusted = Experiment.objects.annotate(
... trunc_start=TruncSecond("start_datetime")
... ).filter(trunc_start__lte=start_adjusted)
>>> find_this_exp.count()
...
1
>>> from django.utils import timezone
>>> from datetime import datetime
>>> from django.db.models.functions import TruncSecond
>>> import zoneinfo
>>> start = datetime(2015, 6, 15, 14, 30, 50, 321)
>>> start = timezone.make_aware(start)
>>> exp = Experiment.objects.create(start_datetime=start)
>>> find_this_exp = Experiment.objects.annotate(
... trunc_start=TruncSecond("start_datetime")
... ).filter(trunc_start__lte=start)
>>> find_this_exp.count() # We expect to find one result but 0 are found
...
0
>>> start_adjusted = timezone.localtime(start).replace(tzinfo=zoneinfo.ZoneInfo(key='UTC'))
>>> find_this_exp_adjusted = Experiment.objects.annotate(
... trunc_start=TruncSecond("start_datetime")
... ).filter(trunc_start__lte=start_adjusted)
>>> find_this_exp.count()
...
1
``DateField`` truncation
~~~~~~~~~~~~~~~~~~~~~~~~