mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Refs #34140 -- Applied rst code-block to non-Python examples.
Thanks to J.V. Zammit, Paolo Melchiorre, and Mariusz Felisiak for reviews.
This commit is contained in:
committed by
Mariusz Felisiak
parent
7bb741d787
commit
534ac48297
@@ -246,8 +246,6 @@ output for computers rather than for humans. The following filters and tags,
|
||||
provided by the ``tz`` template tag library, allow you to control the time zone
|
||||
conversions.
|
||||
|
||||
.. highlight:: html+django
|
||||
|
||||
Template tags
|
||||
-------------
|
||||
|
||||
@@ -263,7 +261,9 @@ This tag has exactly the same effects as the :setting:`USE_TZ` setting as far
|
||||
as the template engine is concerned. It allows a more fine grained control of
|
||||
conversion.
|
||||
|
||||
To activate or deactivate conversion for a template block, use::
|
||||
To activate or deactivate conversion for a template block, use:
|
||||
|
||||
.. code-block:: html+django
|
||||
|
||||
{% load tz %}
|
||||
|
||||
@@ -288,7 +288,7 @@ To activate or deactivate conversion for a template block, use::
|
||||
Sets or unsets the current time zone in the contained block. When the current
|
||||
time zone is unset, the default time zone applies.
|
||||
|
||||
::
|
||||
.. code-block:: html+django
|
||||
|
||||
{% load tz %}
|
||||
|
||||
@@ -306,7 +306,9 @@ time zone is unset, the default time zone applies.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
You can get the name of the current time zone using the
|
||||
``get_current_timezone`` tag::
|
||||
``get_current_timezone`` tag:
|
||||
|
||||
.. code-block:: html+django
|
||||
|
||||
{% get_current_timezone as TIME_ZONE %}
|
||||
|
||||
@@ -328,7 +330,9 @@ return aware datetimes.
|
||||
|
||||
Forces conversion of a single value to the current time zone.
|
||||
|
||||
For example::
|
||||
For example:
|
||||
|
||||
.. code-block:: html+django
|
||||
|
||||
{% load tz %}
|
||||
|
||||
@@ -341,7 +345,9 @@ For example::
|
||||
|
||||
Forces conversion of a single value to UTC.
|
||||
|
||||
For example::
|
||||
For example:
|
||||
|
||||
.. code-block:: html+django
|
||||
|
||||
{% load tz %}
|
||||
|
||||
@@ -357,14 +363,14 @@ Forces conversion of a single value to an arbitrary timezone.
|
||||
The argument must be an instance of a :class:`~datetime.tzinfo` subclass or a
|
||||
time zone name.
|
||||
|
||||
For example::
|
||||
For example:
|
||||
|
||||
.. code-block:: html+django
|
||||
|
||||
{% load tz %}
|
||||
|
||||
{{ value|timezone:"Europe/Paris" }}
|
||||
|
||||
.. highlight:: python
|
||||
|
||||
.. _time-zones-migration-guide:
|
||||
|
||||
Migration guide
|
||||
@@ -420,7 +426,9 @@ code: :func:`~django.utils.timezone.now`,
|
||||
:func:`~django.utils.timezone.make_naive`.
|
||||
|
||||
Finally, in order to help you locate code that needs upgrading, Django raises
|
||||
a warning when you attempt to save a naive datetime to the database::
|
||||
a warning when you attempt to save a naive datetime to the database:
|
||||
|
||||
.. code-block:: pytb
|
||||
|
||||
RuntimeWarning: DateTimeField ModelName.field_name received a naive
|
||||
datetime (2012-01-01 00:00:00) while time zone support is active.
|
||||
@@ -497,7 +505,9 @@ Setup
|
||||
their values should be in UTC (or both!).
|
||||
|
||||
Finally, our calendar system contains interesting edge cases. For example,
|
||||
you can't always subtract one year directly from a given date::
|
||||
you can't always subtract one year directly from a given date:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> import datetime
|
||||
>>> def one_year_before(value): # Wrong example.
|
||||
@@ -527,7 +537,9 @@ Troubleshooting
|
||||
#. **My application crashes with** ``TypeError: can't compare offset-naive``
|
||||
``and offset-aware datetimes`` **-- what's wrong?**
|
||||
|
||||
Let's reproduce this error by comparing a naive and an aware datetime::
|
||||
Let's reproduce this error by comparing a naive and an aware datetime:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from django.utils import timezone
|
||||
>>> aware = timezone.now()
|
||||
@@ -575,7 +587,9 @@ Troubleshooting
|
||||
method. You also consider that a :class:`~datetime.date` is a lot like a
|
||||
:class:`~datetime.datetime`, except that it's less accurate.
|
||||
|
||||
None of this is true in a time zone aware environment::
|
||||
None of this is true in a time zone aware environment:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> import datetime
|
||||
>>> import zoneinfo
|
||||
@@ -613,7 +627,9 @@ Troubleshooting
|
||||
|
||||
If you really need to do the conversion yourself, you must ensure the
|
||||
datetime is converted to the appropriate time zone first. Usually, this
|
||||
will be the current timezone::
|
||||
will be the current timezone:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from django.utils import timezone
|
||||
>>> timezone.activate(zoneinfo.ZoneInfo("Asia/Singapore"))
|
||||
@@ -640,7 +656,9 @@ Usage
|
||||
datetime?**
|
||||
|
||||
Here you need to create the required ``ZoneInfo`` instance and attach it to
|
||||
the naïve datetime::
|
||||
the naïve datetime:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> import zoneinfo
|
||||
>>> from django.utils.dateparse import parse_datetime
|
||||
@@ -663,7 +681,9 @@ Usage
|
||||
sufficient.
|
||||
|
||||
For the sake of completeness, though, if you really want the local time
|
||||
in the current time zone, here's how you can obtain it::
|
||||
in the current time zone, here's how you can obtain it:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from django.utils import timezone
|
||||
>>> timezone.localtime(timezone.now())
|
||||
|
||||
Reference in New Issue
Block a user