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