# This file is part of Hypothesis, which may be found at# https://github.com/HypothesisWorks/hypothesis/## Copyright the Hypothesis Authors.# Individual contributors are listed in AUTHORS.rst and the git log.## This Source Code Form is subject to the terms of the Mozilla Public License,# v. 2.0. If a copy of the MPL was not distributed with this file, You can# obtain one at https://mozilla.org/MPL/2.0/."""This module provides :pypi:`dateutil <python-dateutil>` timezones.You can use this strategy to make :func:`~hypothesis.strategies.datetimes`and :func:`~hypothesis.strategies.times` produce timezone-aware values... tip:: Consider using the stdlib :mod:`zoneinfo` module, via :func:`st.timezones() <hypothesis.strategies.timezones>`."""importdatetimeasdtfromdateutilimporttz,zoneinfo# type: ignorefromhypothesisimportstrategiesasstfromhypothesis.strategies._internal.utilsimportcacheable,defines_strategy__all__=["timezones"]def__zone_sort_key(zone):"""Sort by absolute UTC offset at reference date, positive first, with ties broken by name. """assertzoneisnotNoneoffset=zone.utcoffset(dt.datetime(2000,1,1))offset=999ifoffsetisNoneelseoffsetreturn(abs(offset),-offset,str(zone))
[docs]@cacheable@defines_strategy()deftimezones()->st.SearchStrategy[dt.tzinfo]:"""Any timezone from :pypi:`dateutil <python-dateutil>`. This strategy minimises to UTC, or the timezone with the smallest offset from UTC as of 2000-01-01, and is designed for use with :py:func:`~hypothesis.strategies.datetimes`. Note that the timezones generated by the strategy may vary depending on the configuration of your machine. See the dateutil documentation for more information. """all_timezones=sorted((tz.gettz(t)fortinzoneinfo.get_zonefile_instance().zones),key=__zone_sort_key,)all_timezones.insert(0,tz.UTC)# We discard Nones in the list comprehension because Mypy knows that# tz.gettz may return None. However this should never happen for known# zone names, so we assert that it's impossible first.assertNonenotinall_timezonesreturnst.sampled_from([zforzinall_timezonesifzisnotNone])