Skip to content

Commit b3d3381

Browse files
authored
Finally remove get_next_timezone_transition (#958)
1 parent a87422f commit b3d3381

3 files changed

Lines changed: 1 addition & 140 deletions

File tree

babel/dates.py

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import zoneinfo
2929

3030
import datetime
31-
from bisect import bisect_right
3231
from collections.abc import Iterable
3332

3433
from babel import localtime
@@ -254,121 +253,6 @@ def get_timezone(zone: str | datetime.tzinfo | None = None) -> datetime.tzinfo:
254253
raise LookupError(f"Unknown timezone {zone}") from exc
255254

256255

257-
def get_next_timezone_transition(zone: datetime.tzinfo | None = None, dt: _Instant = None) -> TimezoneTransition:
258-
"""Given a timezone it will return a :class:`TimezoneTransition` object
259-
that holds the information about the next timezone transition that's going
260-
to happen. For instance this can be used to detect when the next DST
261-
change is going to happen and how it looks like.
262-
263-
The transition is calculated relative to the given datetime object. The
264-
next transition that follows the date is used. If a transition cannot
265-
be found the return value will be `None`.
266-
267-
Transition information can only be provided for timezones returned by
268-
the :func:`get_timezone` function.
269-
270-
This function is pending deprecation with no replacement planned in the
271-
Babel library.
272-
273-
:param zone: the timezone for which the transition should be looked up.
274-
If not provided the local timezone is used.
275-
:param dt: the date after which the next transition should be found.
276-
If not given the current time is assumed.
277-
"""
278-
warnings.warn(
279-
"get_next_timezone_transition() is deprecated and will be "
280-
"removed in the next version of Babel. "
281-
"Please see https://github.com/python-babel/babel/issues/716 "
282-
"for discussion.",
283-
category=DeprecationWarning,
284-
)
285-
zone = get_timezone(zone)
286-
dt = _get_datetime(dt).replace(tzinfo=None)
287-
288-
if not hasattr(zone, '_utc_transition_times'):
289-
raise TypeError('Given timezone does not have UTC transition '
290-
'times. This can happen because the operating '
291-
'system fallback local timezone is used or a '
292-
'custom timezone object')
293-
294-
try:
295-
idx = max(0, bisect_right(zone._utc_transition_times, dt))
296-
old_trans = zone._transition_info[idx - 1]
297-
new_trans = zone._transition_info[idx]
298-
old_tz = zone._tzinfos[old_trans]
299-
new_tz = zone._tzinfos[new_trans]
300-
except (LookupError, ValueError):
301-
return None
302-
303-
return TimezoneTransition(
304-
activates=zone._utc_transition_times[idx],
305-
from_tzinfo=old_tz,
306-
to_tzinfo=new_tz,
307-
reference_date=dt
308-
)
309-
310-
311-
class TimezoneTransition:
312-
"""A helper object that represents the return value from
313-
:func:`get_next_timezone_transition`.
314-
315-
This class is pending deprecation with no replacement planned in the
316-
Babel library.
317-
318-
:field activates:
319-
The time of the activation of the timezone transition in UTC.
320-
:field from_tzinfo:
321-
The timezone from where the transition starts.
322-
:field to_tzinfo:
323-
The timezone for after the transition.
324-
:field reference_date:
325-
The reference date that was provided. This is the `dt` parameter
326-
to the :func:`get_next_timezone_transition`.
327-
"""
328-
329-
def __init__(
330-
self,
331-
activates: datetime.datetime,
332-
from_tzinfo: datetime.tzinfo,
333-
to_tzinfo: datetime.tzinfo,
334-
reference_date: datetime.datetime | None = None,
335-
) -> None:
336-
warnings.warn(
337-
"TimezoneTransition is deprecated and will be "
338-
"removed in the next version of Babel. "
339-
"Please see https://github.com/python-babel/babel/issues/716 "
340-
"for discussion.",
341-
category=DeprecationWarning,
342-
)
343-
self.activates = activates
344-
self.from_tzinfo = from_tzinfo
345-
self.to_tzinfo = to_tzinfo
346-
self.reference_date = reference_date
347-
348-
@property
349-
def from_tz(self) -> str:
350-
"""The name of the timezone before the transition."""
351-
return self.from_tzinfo._tzname
352-
353-
@property
354-
def to_tz(self) -> str:
355-
"""The name of the timezone after the transition."""
356-
return self.to_tzinfo._tzname
357-
358-
@property
359-
def from_offset(self) -> int:
360-
"""The UTC offset in seconds before the transition."""
361-
return int(self.from_tzinfo._utcoffset.total_seconds())
362-
363-
@property
364-
def to_offset(self) -> int:
365-
"""The UTC offset in seconds after the transition."""
366-
return int(self.to_tzinfo._utcoffset.total_seconds())
367-
368-
def __repr__(self) -> str:
369-
return f"<TimezoneTransition {self.from_tz} -> {self.to_tz} ({self.activates})>"
370-
371-
372256
def get_period_names(width: Literal['abbreviated', 'narrow', 'wide'] = 'wide',
373257
context: _Context = 'stand-alone', locale: Locale | str | None = LC_TIME) -> LocaleDataDict:
374258
"""Return the names for day periods (AM/PM) used by the locale.

docs/api/dates.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ Timezone Functionality
3232

3333
.. autofunction:: get_timezone_name
3434

35-
.. autofunction:: get_next_timezone_transition
36-
3735
.. data:: UTC
3836

3937
A timezone object for UTC.

docs/dates.rst

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -308,28 +308,7 @@ applied to ``format_time``, but because the actual date is unknown in that
308308
case, the current day is assumed to determine whether DST or standard time
309309
should be used.
310310

311-
For many timezones it's also possible to ask for the next timezone
312-
transition. This for instance is useful to answer the question “when do I
313-
have to move the clock forward next”:
314-
315-
.. warning:: ``get_next_timezone_transition`` is deprecated and will be removed
316-
in the next version of Babel
317-
318-
.. code-block:: pycon
319-
320-
>>> t = get_next_timezone_transition('Europe/Vienna', datetime(2011, 3, 2))
321-
>>> t
322-
<TimezoneTransition CET -> CEST (2011-03-27 01:00:00)>
323-
>>> t.from_offset
324-
3600.0
325-
>>> t.to_offset
326-
7200.0
327-
>>> t.from_tz
328-
'CET'
329-
>>> t.to_tz
330-
'CEST'
331-
332-
Lastly Babel also provides support for working with the local timezone of
311+
Babel also provides support for working with the local timezone of
333312
your operating system. It's provided through the ``LOCALTZ`` constant:
334313

335314
.. code-block:: pycon

0 commit comments

Comments
 (0)