Skip to content

gh-72346: Added isdst deprecation warning to email.utils.localtime #91450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
dadf5e2
Added isdst deprecation warning
alanfwilliams Apr 11, 2022
77414f9
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 11, 2022
add07f3
added documentation and used _deprecated
alanfwilliams Apr 12, 2022
854ebf8
Removed trailing whitespace.
alanfwilliams Apr 12, 2022
1b9a60f
added newline, moved whatsnew, reformatted warning
alanfwilliams Apr 13, 2022
6784b42
formatting corrections
alanfwilliams Apr 13, 2022
1bd66a2
Updated documentation and code on isdst
alanfwilliams Apr 28, 2022
d3fd3f2
removed trailing whitespace
alanfwilliams Apr 28, 2022
b30f940
corrected spacing
alanfwilliams Apr 28, 2022
354eff2
use deprecated-removed properly
alanfwilliams Apr 28, 2022
36365fe
further documentation edits
alanfwilliams Apr 29, 2022
44f025c
Added isdst deprecation warning
alanfwilliams Apr 11, 2022
7125e78
📜🤖 Added by blurb_it.
blurb-it[bot] Apr 11, 2022
cdea718
added documentation and used _deprecated
alanfwilliams Apr 12, 2022
6b0ccbb
Removed trailing whitespace.
alanfwilliams Apr 12, 2022
47474c9
added newline, moved whatsnew, reformatted warning
alanfwilliams Apr 13, 2022
0d3c5bf
formatting corrections
alanfwilliams Apr 13, 2022
9f59d33
Updated documentation and code on isdst
alanfwilliams Apr 28, 2022
ae687b0
removed trailing whitespace
alanfwilliams Apr 28, 2022
c214c4f
corrected spacing
alanfwilliams Apr 28, 2022
7cacc5f
use deprecated-removed properly
alanfwilliams Apr 28, 2022
0633911
further documentation edits
alanfwilliams Apr 29, 2022
e483ed6
Merge branch 'fix-gh-72346' of https://github.com/aw1231/cpython into…
alanfwilliams Aug 30, 2022
bf4b0c6
removed duplicated whatsnew/ moved to current ver
alanfwilliams Aug 30, 2022
a63a5e4
took one too many lines off old whatsnew
alanfwilliams Aug 30, 2022
8807fa1
Changed != to is not
alanfwilliams Aug 30, 2022
318ef9e
shifted versions to latest values
alanfwilliams Aug 30, 2022
718888c
Properly format documentation
alanfwilliams Aug 30, 2022
9389791
Revert incorrect change.
ezio-melotti Aug 30, 2022
672b5e5
Merge branch 'fix-gh-72346' of https://github.com/aw1231/cpython into…
alanfwilliams Sep 2, 2022
89eb11a
Merge branch 'main' into fix-gh-72346
alanfwilliams Sep 7, 2022
c2b4c94
Merge branch 'fix-gh-72346' of https://github.com/aw1231/cpython into…
alanfwilliams Sep 7, 2022
31a4c46
Modified existing tests/added test for deprecation
alanfwilliams Sep 8, 2022
54596ef
Merge branch 'main' into fix-gh-72346
alanfwilliams Sep 8, 2022
d0eda9d
Moved whatsnew note
alanfwilliams Sep 8, 2022
ecc3bff
Merge branch 'fix-gh-72346' of https://github.com/aw1231/cpython into…
alanfwilliams Sep 8, 2022
8151b5a
Merge branch 'main' into fix-gh-72346
alanfwilliams Oct 7, 2022
4c3413a
Adjust formatting
alanfwilliams Oct 7, 2022
7ff165e
Merge branch 'main' into fix-gh-72346
alanfwilliams Oct 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions Doc/library/email.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ module:

.. function:: localtime(dt=None)

Return local time as an aware datetime object. If called without
arguments, return current time. Otherwise *dt* argument should be a
:class:`~datetime.datetime` instance, and it is converted to the local time
zone according to the system time zone database. If *dt* is naive (that
is, ``dt.tzinfo`` is ``None``), it is assumed to be in local time. In this
case, a positive or zero value for *isdst* causes ``localtime`` to presume
initially that summer time (for example, Daylight Saving Time) is or is not
(respectively) in effect for the specified time. A negative value for
*isdst* causes the ``localtime`` to attempt to divine whether summer time
is in effect for the specified time.

.. versionadded:: 3.3
Return local time as an aware datetime object. If called without
arguments, return current time. Otherwise *dt* argument should be a
:class:`~datetime.datetime` instance, and it is converted to the local time
zone according to the system time zone database. If *dt* is naive (that
is, ``dt.tzinfo`` is ``None``), it is assumed to be in local time. The
*isdst* parameter is ignored.

.. versionadded:: 3.3

.. deprecated-removed:: 3.12 3.14
The *isdst* parameter.

.. function:: make_msgid(idstring=None, domain=None)

Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ Pending Removal in Python 3.14
* Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable
bases using the C API.

* Deprecated the *isdst* parameter in :func:`email.utils.localtime`.
(Contributed by Alan Williams in :gh:`72346`.)

* ``__package__`` and ``__cached__`` will cease to be set or taken
into consideration by the import system (:gh:`97879`).

Expand Down
40 changes: 11 additions & 29 deletions Lib/email/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,41 +331,23 @@ def collapse_rfc2231_value(value, errors='replace',
# better than not having it.
#

def localtime(dt=None, isdst=-1):
def localtime(dt=None, isdst=None):
"""Return local time as an aware datetime object.

If called without arguments, return current time. Otherwise *dt*
argument should be a datetime instance, and it is converted to the
local time zone according to the system time zone database. If *dt* is
naive (that is, dt.tzinfo is None), it is assumed to be in local time.
In this case, a positive or zero value for *isdst* causes localtime to
presume initially that summer time (for example, Daylight Saving Time)
is or is not (respectively) in effect for the specified time. A
negative value for *isdst* causes the localtime() function to attempt
to divine whether summer time is in effect for the specified time.
The isdst parameter is ignored.

"""
if isdst is not None:
import warnings
warnings._deprecated(
"The 'isdst' parameter to 'localtime'",
message='{name} is deprecated and slated for removal in Python {remove}',
remove=(3, 14),
)
if dt is None:
return datetime.datetime.now(datetime.timezone.utc).astimezone()
if dt.tzinfo is not None:
return dt.astimezone()
# We have a naive datetime. Convert to a (localtime) timetuple and pass to
# system mktime together with the isdst hint. System mktime will return
# seconds since epoch.
tm = dt.timetuple()[:-1] + (isdst,)
seconds = time.mktime(tm)
localtm = time.localtime(seconds)
try:
delta = datetime.timedelta(seconds=localtm.tm_gmtoff)
tz = datetime.timezone(delta, localtm.tm_zone)
except AttributeError:
# Compute UTC offset and compare with the value implied by tm_isdst.
# If the values match, use the zone name implied by tm_isdst.
delta = dt - datetime.datetime(*time.gmtime(seconds)[:6])
dst = time.daylight and localtm.tm_isdst > 0
gmtoff = -(time.altzone if dst else time.timezone)
if delta == datetime.timedelta(seconds=gmtoff):
tz = datetime.timezone(delta, time.tzname[dst])
else:
tz = datetime.timezone(delta)
return dt.replace(tzinfo=tz)
dt = datetime.datetime.now()
return dt.astimezone()
13 changes: 9 additions & 4 deletions Lib/test/test_email/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,30 @@ def test_localtime_is_tz_aware_daylight_false(self):
def test_localtime_daylight_true_dst_false(self):
test.support.patch(self, time, 'daylight', True)
t0 = datetime.datetime(2012, 3, 12, 1, 1)
t1 = utils.localtime(t0, isdst=-1)
t1 = utils.localtime(t0)
t2 = utils.localtime(t1)
self.assertEqual(t1, t2)

def test_localtime_daylight_false_dst_false(self):
test.support.patch(self, time, 'daylight', False)
t0 = datetime.datetime(2012, 3, 12, 1, 1)
t1 = utils.localtime(t0, isdst=-1)
t1 = utils.localtime(t0)
t2 = utils.localtime(t1)
self.assertEqual(t1, t2)

@test.support.run_with_tz('Europe/Minsk')
def test_localtime_daylight_true_dst_true(self):
test.support.patch(self, time, 'daylight', True)
t0 = datetime.datetime(2012, 3, 12, 1, 1)
t1 = utils.localtime(t0, isdst=1)
t1 = utils.localtime(t0)
t2 = utils.localtime(t1)
self.assertEqual(t1, t2)

@test.support.run_with_tz('Europe/Minsk')
def test_localtime_daylight_false_dst_true(self):
test.support.patch(self, time, 'daylight', False)
t0 = datetime.datetime(2012, 3, 12, 1, 1)
t1 = utils.localtime(t0, isdst=1)
t1 = utils.localtime(t0)
t2 = utils.localtime(t1)
self.assertEqual(t1, t2)

Expand Down Expand Up @@ -157,6 +157,11 @@ def test_variable_tzname(self):
t1 = utils.localtime(t0)
self.assertEqual(t1.tzname(), 'EET')

def test_isdst_deprecation(self):
with self.assertWarns(DeprecationWarning):
t0 = datetime.datetime(1990, 1, 1)
t1 = utils.localtime(t0, isdst=True)

# Issue #24836: The timezone files are out of date (pre 2011k)
# on Mac OS X Snow Leopard.
@test.support.requires_mac_ver(10, 7)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added deprecation warning to *isdst* parameter of :func:`email.utils.localtime`.