# This file is part of Hypothesis, which may be found at
# Most of this work is copyright (C) 2013-2020 David R. MacIver
# (firstname.lastname@example.org), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# 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/.
# END HEADER
This module provides ``dateutil`` timezones.
You can use this strategy to make
:py:func:`hypothesis.strategies.times` produce timezone-aware values.
import datetime as dt
from dateutil import tz, zoneinfo # type: ignore
import hypothesis.strategies._internal.core as st
__all__ = ["timezones"]
"""Sort by absolute UTC offset at reference date,
positive first, with ties broken by name.
assert zone is not None
offset = zone.utcoffset(dt.datetime(2000, 1, 1))
offset = 999 if offset is None else offset
return (abs(offset), -offset, str(zone))
def timezones() -> st.SearchStrategy[dt.tzinfo]:
"""Any timezone in 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
Note that the timezones generated by the strategy may vary depending on the
configuration of your machine. See the dateutil documentation for more
all_timezones = sorted(
(tz.gettz(t) for t in zoneinfo.get_zonefile_instance().zones),
# 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.
assert None not in all_timezones
return st.sampled_from([z for z in all_timezones if z is not None])