@@ -3,6 +3,7 @@ from datetime import (
33 timezone,
44)
55import zoneinfo
6+ from zoneinfo._zoneinfo import ZoneInfo as _ZoneInfo
67
78from pandas.compat._optional import import_optional_dependency
89
@@ -210,6 +211,8 @@ cdef object tz_cache_key(tzinfo tz):
210211 " of passing a timezone object. See "
211212 " https://github.com/pandas-dev/pandas/pull/7362" )
212213 return " dateutil" + tz._filename
214+ elif is_zoneinfo(tz):
215+ return " zoneinfo/" + tz.key
213216 else :
214217 return None
215218
@@ -238,7 +241,11 @@ cpdef inline bint is_fixed_offset(tzinfo tz):
238241 else :
239242 return 0
240243 elif is_zoneinfo(tz):
241- return 0
244+ tz_py = _ZoneInfo(tz.key)
245+ if tz_py._fixed_offset:
246+ return 1
247+ else :
248+ return 0
242249 # This also implicitly accepts datetime.timezone objects which are
243250 # considered fixed
244251 return 1
@@ -278,6 +285,99 @@ cdef int64_t[::1] unbox_utcoffsets(object transinfo):
278285# Daylight Savings
279286
280287
288+ cdef tuple _get_zoneinfo_trans_and_deltas(tzinfo tz):
289+ """
290+ Get transition times and UTC offsets for a ZoneInfo timezone.
291+
292+ Uses zoneinfo's Python fallback implementation to get transition data,
293+ including future transitions generated from POSIX TZ rules.
294+
295+ Parameters
296+ ----------
297+ tz : ZoneInfo
298+
299+ Returns
300+ -------
301+ trans : ndarray[int64_t]
302+ Nanosecond UTC times of DST transitions.
303+ deltas : ndarray[int64_t]
304+ Nanosecond UTC offsets corresponding to DST transitions.
305+ is_fixed : bint
306+ True if this is a fixed-offset timezone with no transitions.
307+ """
308+ cdef:
309+ int64_t fixed_offset_seconds, last_hist_ts, start_utc, end_utc
310+ list trans_utc, deltas_seconds, future_trans
311+ int year, last_year, std_offset, dst_offset
312+
313+ tz_py = _ZoneInfo(tz.key)
314+
315+ if tz_py._fixed_offset:
316+ fixed_offset_seconds = int (tz_py._tz_after.utcoff.total_seconds())
317+ trans = np.array([NPY_NAT + 1 ], dtype = np.int64)
318+ deltas = np.array([fixed_offset_seconds], dtype = " i8" ) * 1 _000_000_000
319+ return trans, deltas, True
320+
321+ trans_utc = list (tz_py._trans_utc)
322+ deltas_seconds = [int (info.utcoff.total_seconds()) for info in tz_py._ttinfos]
323+
324+ has_future_dst = (
325+ hasattr (tz_py, " _tz_after" )
326+ and tz_py._tz_after is not None
327+ and hasattr (tz_py._tz_after, " transitions" )
328+ )
329+ if has_future_dst:
330+ # ZoneInfo's _trans_utc only contains historical data (typically up to
331+ # the late 1990s for many timezones). POSIX TZ rules stored in _tz_after
332+ # are required to generate transitions for current and future dates.
333+ # Without this block, DST-observing timezones like Europe/Amsterdam
334+ # would return incorrect offsets for any date after ~1996.
335+ tz_after = tz_py._tz_after
336+ std_offset = int (tz_after.std.utcoff.total_seconds())
337+ dst_offset = int (tz_after.dst.utcoff.total_seconds())
338+
339+ if trans_utc:
340+ last_hist_ts = trans_utc[- 1 ]
341+ try :
342+ last_year = datetime.fromtimestamp(last_hist_ts, timezone.utc).year
343+ except (OSError , OverflowError , ValueError ):
344+ last_year = 1970
345+ else :
346+ last_hist_ts = 0
347+ last_year = 1970
348+
349+ future_trans = []
350+ for year in range (last_year, 2100 ):
351+ try :
352+ year_trans = tz_after.transitions(year)
353+ if not year_trans:
354+ break
355+ start_local, end_local = year_trans
356+ start_utc = start_local - std_offset
357+ end_utc = end_local - dst_offset
358+ if start_utc > last_hist_ts:
359+ future_trans.append((start_utc, dst_offset))
360+ if end_utc > last_hist_ts:
361+ future_trans.append((end_utc, std_offset))
362+ except Exception :
363+ break
364+
365+ future_trans.sort()
366+
367+ for t, d in future_trans:
368+ trans_utc.append(t)
369+ deltas_seconds.append(d)
370+
371+ trans = np.array(trans_utc, dtype = " i8" ) * 1 _000_000_000
372+ trans = np.hstack([np.array([NPY_NAT + 1 ], dtype = np.int64), trans])
373+
374+ first_offset_seconds = int (tz_py._tti_before.utcoff.total_seconds())
375+ deltas = np.array(deltas_seconds, dtype = " i8" ) * 1 _000_000_000
376+ deltas = np.hstack([[first_offset_seconds * 1 _000_000_000], deltas])
377+
378+ return trans, deltas, False
379+
380+
281381cdef object get_dst_info(tzinfo tz):
282382 """
283383 Returns
@@ -340,6 +440,11 @@ cdef object get_dst_info(tzinfo tz):
340440 # (under the just-deleted code that returned empty arrays)
341441 raise AssertionError (" dateutil tzinfo is not a FixedOffset "
342442 " and has an empty `_trans_list`." , tz)
443+
444+ elif is_zoneinfo(tz):
445+ trans, deltas, is_fixed = _get_zoneinfo_trans_and_deltas(tz)
446+ typ = " fixed" if is_fixed else " zoneinfo"
447+
343448 else :
344449 # static tzinfo, we can get here with pytz.StaticTZInfo
345450 # which are not caught by treat_tz_as_pytz
0 commit comments