Added support for time zones. Thanks Luke Plant for the review. Fixed #2626.

For more information on this project, see this thread:
cf0423bbb8



git-svn-id: http://code.djangoproject.com/svn/django/trunk@17106 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2011-11-18 13:01:06 +00:00
parent 01f70349c9
commit 9b1cb755a2
58 changed files with 2720 additions and 284 deletions

View file

@ -546,6 +546,12 @@ Examples::
>>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day')
[datetime.datetime(2005, 3, 20)]
.. warning::
When :doc:`time zone support </topics/i18n/timezones>` is enabled, Django
uses UTC in the database connection, which means the aggregation is
performed in UTC. This is a known limitation of the current implementation.
none
~~~~
@ -1953,6 +1959,13 @@ Note this will match any record with a ``pub_date`` that falls on a Monday (day
2 of the week), regardless of the month or year in which it occurs. Week days
are indexed with day 1 being Sunday and day 7 being Saturday.
.. warning::
When :doc:`time zone support </topics/i18n/timezones>` is enabled, Django
uses UTC in the database connection, which means the ``year``, ``month``,
``day`` and ``week_day`` lookups are performed in UTC. This is a known
limitation of the current implementation.
.. fieldlookup:: isnull
isnull

View file

@ -1810,6 +1810,7 @@ Default::
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages")
A tuple of callables that are used to populate the context in ``RequestContext``.
@ -1830,6 +1831,10 @@ of items to be merged into the context.
The ``django.core.context_processors.static`` context processor
was added in this release.
.. versionadded:: 1.4
The ``django.core.context_processors.tz`` context processor
was added in this release.
.. setting:: TEMPLATE_DEBUG
TEMPLATE_DEBUG
@ -1971,6 +1976,9 @@ Default: ``'America/Chicago'``
.. versionchanged:: 1.2
``None`` was added as an allowed value.
.. versionchanged:: 1.4
The meaning of this setting now depends on the value of :setting:`USE_TZ`.
A string representing the time zone for this installation, or
``None``. `See available choices`_. (Note that list of available
choices lists more than one on the same line; you'll want to use just
@ -1978,16 +1986,19 @@ one of the choices for a given time zone. For instance, one line says
``'Europe/London GB GB-Eire'``, but you should use the first bit of
that -- ``'Europe/London'`` -- as your :setting:`TIME_ZONE` setting.)
Note that this is the time zone to which Django will convert all
dates/times -- not necessarily the timezone of the server. For
example, one server may serve multiple Django-powered sites, each with
a separate time-zone setting.
Note that this isn't necessarily the timezone of the server. For example, one
server may serve multiple Django-powered sites, each with a separate time zone
setting.
Normally, Django sets the ``os.environ['TZ']`` variable to the time
zone you specify in the :setting:`TIME_ZONE` setting. Thus, all your views
and models will automatically operate in the correct time zone.
However, Django won't set the ``TZ`` environment variable under the
following conditions:
When :setting:`USE_TZ` is ``False``, this is the time zone in which Django will
store all datetimes. When :setting:`USE_TZ` is ``True``, this is the default
time zone that Django will use to display datetimes in templates and to
interpret datetimes entered in forms.
Django sets the ``os.environ['TZ']`` variable to the time zone you specify in
the :setting:`TIME_ZONE` setting. Thus, all your views and models will
automatically operate in this time zone. However, Django won't set the ``TZ``
environment variable under the following conditions:
* If you're using the manual configuration option as described in
:ref:`manually configuring settings
@ -2004,7 +2015,6 @@ to ensure your processes are running in the correct environment.
environment. If you're running Django on Windows, this variable
must be set to match the system timezone.
.. _See available choices: http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
.. setting:: URL_VALIDATOR_USER_AGENT
@ -2043,7 +2053,7 @@ This provides an easy way to turn it off, for performance. If this is set to
``False``, Django will make some optimizations so as not to load the
translation machinery.
See also :setting:`USE_L10N`
See also :setting:`LANGUAGE_CODE`, :setting:`USE_L10N` and :setting:`USE_TZ`.
.. setting:: USE_L10N
@ -2058,7 +2068,7 @@ A boolean that specifies if localized formatting of data will be enabled by
default or not. If this is set to ``True``, e.g. Django will display numbers and
dates using the format of the current locale.
See also :setting:`USE_I18N` and :setting:`LANGUAGE_CODE`
See also :setting:`LANGUAGE_CODE`, :setting:`USE_I18N` and :setting:`USE_TZ`.
.. note::
@ -2082,6 +2092,26 @@ When :setting:`USE_L10N` is set to ``True`` and if this is also set to
See also :setting:`DECIMAL_SEPARATOR`, :setting:`NUMBER_GROUPING` and
:setting:`THOUSAND_SEPARATOR`.
.. setting:: USE_TZ
USE_TZ
------
.. versionadded:: 1.4
Default: ``False``
A boolean that specifies if datetimes will be timezone-aware by default or not.
If this is set to ``True``, Django will use timezone-aware datetimes internally.
Otherwise, Django will use naive datetimes in local time.
See also :setting:`TIME_ZONE`, :setting:`USE_I18N` and :setting:`USE_L10N`.
.. note::
The default :file:`settings.py` file created by
:djadmin:`django-admin.py startproject <startproject>` includes
``USE_TZ = True`` for convenience.
.. setting:: USE_X_FORWARDED_HOST
USE_X_FORWARDED_HOST

View file

@ -2318,8 +2318,45 @@ Value Argument Outputs
if no mapping for None is given)
========== ====================== ==================================
Other tags and filter libraries
-------------------------------
Internationalization tags and filters
-------------------------------------
Django provides template tags and filters to control each aspect of
`internationalization </topics/i18n/index>`_ in templates. They allow for
granular control of translations, formatting, and time zone conversions.
i18n
^^^^
This library allows specifying translatable text in templates.
To enable it, set :setting:`USE_I18N` to ``True``, then load it with
``{% load i18n %}``.
See :ref:`specifying-translation-strings-in-template-code`.
l10n
^^^^
This library provides control over the localization of values in templates.
You only need to load the library using ``{% load l10n %}``, but you'll often
set :setting:`USE_L10N` to ``True`` so that localization is active by default.
See :ref:`topic-l10n-templates`.
tz
^^
.. versionadded:: 1.4
This library provides control over time zone conversions in templates.
Like ``l10n``, you only need to load the library using ``{% load tz %}``,
but you'll usually also set :setting:`USE_TZ` to ``True`` so that conversion
to local time happens by default.
See :ref:`time-zones-in-templates`.
Other tags and filters libraries
--------------------------------
Django comes with a couple of other template-tag libraries that you have to
enable explicitly in your :setting:`INSTALLED_APPS` setting and enable in your
@ -2348,28 +2385,6 @@ django.contrib.webdesign
A collection of template tags that can be useful while designing a Web site,
such as a generator of Lorem Ipsum text. See :doc:`/ref/contrib/webdesign`.
i18n
^^^^
Provides a couple of templatetags that allow specifying translatable text in
Django templates. It is slightly different from the libraries described
above because you don't need to add any application to the
:setting:`INSTALLED_APPS` setting but rather set :setting:`USE_I18N` to True,
then loading it with ``{% load i18n %}``.
See :ref:`specifying-translation-strings-in-template-code`.
l10n
^^^^
Provides a couple of templatetags that allow control over the localization of
values in Django templates. It is slightly different from the libraries
described above because you don't need to add any application to the
:setting:`INSTALLED_APPS`; you only need to load the library using
``{% load l10n %}``.
See :ref:`topic-l10n-templates`.
static
^^^^^^

View file

@ -131,6 +131,41 @@ results. Instead do::
SortedDict([('b', 1), ('a', 2), ('c', 3)])
``django.utils.dateparse``
==========================
.. versionadded:: 1.4
.. module:: django.utils.dateparse
:synopsis: Functions to parse datetime objects.
The functions defined in this module share the following properties:
- They raise :exc:`ValueError` if their input is well formatted but isn't a
valid date or time.
- They return ``None`` if it isn't well formatted at all.
- They accept up to picosecond resolution in input, but they truncate it to
microseconds, since that's what Python supports.
.. function:: parse_date(value)
Parses a string and returns a :class:`datetime.date`.
.. function:: parse_time(value)
Parses a string and returns a :class:`datetime.time`.
UTC offsets aren't supported; if ``value`` describes one, the result is
``None``.
.. function:: parse_datetime(value)
Parses a string and returns a :class:`datetime.datetime`.
UTC offsets are supported; if ``value`` describes one, the result's
``tzinfo`` attribute is a :class:`~django.utils.tzinfo.FixedOffset`
instance.
``django.utils.encoding``
=========================
@ -573,6 +608,96 @@ For a complete discussion on the usage of the following see the
so by translating the Django translation tags into standard gettext function
invocations.
.. _time-zone-selection-functions:
``django.utils.timezone``
=========================
.. versionadded:: 1.4
.. module:: django.utils.timezone
:synopsis: Timezone support.
.. data:: utc
:class:`~datetime.tzinfo` instance that represents UTC.
.. function:: get_default_timezone()
Returns a :class:`~datetime.tzinfo` instance that represents the
:ref:`default time zone <default-current-time-zone>`.
.. function:: get_default_timezone_name()
Returns the name of the :ref:`default time zone
<default-current-time-zone>`.
.. function:: get_current_timezone()
Returns a :class:`~datetime.tzinfo` instance that represents the
:ref:`current time zone <default-current-time-zone>`.
.. function:: get_current_timezone_name()
Returns the name of the :ref:`current time zone
<default-current-time-zone>`.
.. function:: activate(timezone)
Sets the :ref:`current time zone <default-current-time-zone>`. The
``timezone`` argument must be an instance of a :class:`~datetime.tzinfo`
subclass or, if pytz_ is available, a time zone name.
.. function:: deactivate()
Unsets the :ref:`current time zone <default-current-time-zone>`.
.. function:: override(timezone)
This is a Python context manager that sets the :ref:`current time zone
<default-current-time-zone>` on entry with :func:`activate()`, and restores
the previously active time zone on exit. If the ``timezone`` argument is
``None``, the :ref:`current time zone <default-current-time-zone>` is unset
on entry with :func:`deactivate()` instead.
.. function:: aslocaltime(value, use_tz=None)
This function is used by the template engine to convert datetimes to local
time where appropriate.
.. function:: now()
Returns an aware or naive :class:`~datetime.datetime` that represents the
current point in time when :setting:`USE_TZ` is ``True`` or ``False``
respectively.
.. function:: is_aware(value)
Returns ``True`` if ``value`` is aware, ``False`` if it is naive. This
function assumes that ``value`` is a :class:`~datetime.datetime`.
.. function:: is_naive(value)
Returns ``True`` if ``value`` is naive, ``False`` if it is aware. This
function assumes that ``value`` is a :class:`~datetime.datetime`.
.. function:: make_aware(value, timezone)
Returns an aware :class:`~datetime.datetime` that represents the same
point in time as ``value`` in ``timezone``, ``value`` being a naive
:class:`~datetime.datetime`.
This function can raise an exception if ``value`` doesn't exist or is
ambiguous because of DST transitions.
.. function:: make_naive(value, timezone)
Returns an naive :class:`~datetime.datetime` that represents in
``timezone`` the same point in time as ``value``, ``value`` being an
aware :class:`~datetime.datetime`
.. _pytz: http://pytz.sourceforge.net/
``django.utils.tzinfo``
=======================