[1.7.x] Updated timezone docs to use timezone.now()

Thanks James Cleveland for the report and Aymeric for suggesting
the solution.

Backport of da59902250 from master
This commit is contained in:
Tim Graham 2014-07-31 12:54:11 -04:00
parent 063e7e56bc
commit 42bb047d33
1 changed files with 6 additions and 13 deletions

View File

@ -78,20 +78,13 @@ the current time, you would write::
now = datetime.datetime.now() now = datetime.datetime.now()
When time zone support is enabled, Django uses time-zone-aware datetime When time zone support is enabled (:setting:`USE_TZ=True <USE_TZ>`), Django uses
objects. If your code creates datetime objects, they should be aware too. In time-zone-aware datetime objects. If your code creates datetime objects, they
this mode, the example above becomes:: should be aware too. In this mode, the example above becomes::
import datetime from django.utils import timezone
from django.utils.timezone import utc
now = datetime.datetime.utcnow().replace(tzinfo=utc) now = timezone.now()
.. note::
:mod:`django.utils.timezone` provides a
:func:`~django.utils.timezone.now()` function that returns a naive or
aware datetime object according to the value of :setting:`USE_TZ`.
.. warning:: .. warning::
@ -554,7 +547,7 @@ Troubleshooting
>>> import datetime >>> import datetime
>>> from django.utils import timezone >>> from django.utils import timezone
>>> naive = datetime.datetime.utcnow() >>> naive = datetime.datetime.utcnow()
>>> aware = naive.replace(tzinfo=timezone.utc) >>> aware = timezone.now()
>>> naive == aware >>> naive == aware
Traceback (most recent call last): Traceback (most recent call last):
... ...