Fixed #23668 -- Changed make_aware() and make_naive() to use the current timezone by default

Thanks Aymeric Augustin for review.
This commit is contained in:
Jon Dufresne 2014-10-15 19:52:41 -07:00 committed by Tim Graham
parent 740934b507
commit 59026bfbf9
2 changed files with 20 additions and 6 deletions

View File

@ -350,10 +350,12 @@ def is_naive(value):
return value.tzinfo is None or value.tzinfo.utcoffset(value) is None
def make_aware(value, timezone):
def make_aware(value, timezone=None):
"""
Makes a naive datetime.datetime in a given time zone aware.
"""
if timezone is None:
timezone = get_current_timezone()
if hasattr(timezone, 'localize'):
# This method is available for pytz time zones.
return timezone.localize(value, is_dst=None)
@ -366,10 +368,12 @@ def make_aware(value, timezone):
return value.replace(tzinfo=timezone)
def make_naive(value, timezone):
def make_naive(value, timezone=None):
"""
Makes an aware datetime.datetime naive in a given time zone.
"""
if timezone is None:
timezone = get_current_timezone()
# If `value` is naive, astimezone() will raise a ValueError,
# so we don't need to perform a redundant check.
value = value.astimezone(timezone)

View File

@ -954,20 +954,30 @@ appropriate entities.
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)
.. function:: make_aware(value, timezone=None)
Returns an aware :class:`~datetime.datetime` that represents the same
point in time as ``value`` in ``timezone``, ``value`` being a naive
:class:`~datetime.datetime`.
:class:`~datetime.datetime`. If ``timezone`` is set to ``None``, it
defaults to the :ref:`current time zone <default-current-time-zone>`.
This function can raise an exception if ``value`` doesn't exist or is
ambiguous because of DST transitions.
.. function:: make_naive(value, timezone)
.. versionchanged:: 1.8
In older versions of Django, ``timezone`` was a required argument.
.. function:: make_naive(value, timezone=None)
Returns an naive :class:`~datetime.datetime` that represents in
``timezone`` the same point in time as ``value``, ``value`` being an
aware :class:`~datetime.datetime`
aware :class:`~datetime.datetime`. If ``timezone`` is set to ``None``, it
defaults to the :ref:`current time zone <default-current-time-zone>`.
.. versionchanged:: 1.8
In older versions of Django, ``timezone`` was a required argument.
.. _pytz: http://pytz.sourceforge.net/