Skip to content
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

Allow timezones to be created directly within arrow #969

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion arrow/__init__.py
@@ -1,5 +1,5 @@
from ._version import __version__
from .api import get, now, utcnow
from .api import get, now, timezone, utcnow
from .arrow import Arrow
from .factory import ArrowFactory
from .formatter import (
Expand All @@ -23,6 +23,7 @@
"get",
"now",
"utcnow",
"timezone",
"Arrow",
"ArrowFactory",
"FORMAT_ATOM",
Expand Down
6 changes: 5 additions & 1 deletion arrow/api.py
Expand Up @@ -112,6 +112,10 @@ def now(tz: Optional[TZ_EXPR] = None) -> Arrow:
now.__doc__ = _factory.now.__doc__


def timezone(zone_name: str) -> dt_tzinfo:
return _factory.timezone(zone_name)


def factory(type: Type[Arrow]) -> ArrowFactory:
"""Returns an :class:`.ArrowFactory` for the specified :class:`Arrow <arrow.arrow.Arrow>`
or derived type.
Expand All @@ -123,4 +127,4 @@ def factory(type: Type[Arrow]) -> ArrowFactory:
return ArrowFactory(type)


__all__ = ["get", "utcnow", "now", "factory"]
__all__ = ["get", "utcnow", "now", "factory", "timezone"]
7 changes: 7 additions & 0 deletions arrow/factory.py
Expand Up @@ -346,3 +346,10 @@ def now(self, tz: Optional[TZ_EXPR] = None) -> Arrow:
tz = parser.TzinfoParser.parse(tz)

return self.type.now(tz)

@staticmethod
def timezone(zone_name: str) -> dt_tzinfo:
"""docstring here"""
zone = parser.TzinfoParser.parse(zone_name)

return zone
9 changes: 9 additions & 0 deletions tests/test_api.py
Expand Up @@ -25,3 +25,12 @@ class MockCustomArrowClass(arrow.Arrow):

assert isinstance(result, arrow.factory.ArrowFactory)
assert isinstance(result.utcnow(), MockCustomArrowClass)

def test_timezone(self, mocker):
mocker.patch(
"arrow.api._factory.timezone",
zone_name="zone_name",
return_value="timezone",
)

assert arrow.api.timezone("zone_name") == "timezone"
12 changes: 12 additions & 0 deletions tests/test_factory.py
Expand Up @@ -434,3 +434,15 @@ def test_tzinfo(self):
def test_tz_str(self):

assert_datetime_equality(self.factory.now("EST"), datetime.now(tz.gettz("EST")))


@pytest.mark.usefixtures("arrow_factory")
class TestTimezone:
def test_timezone(self):

assert self.factory.timezone("Australia/Darwin") == tz.gettz("Australia/Darwin")

def test_bad_input(self):

with pytest.raises(ParserError):
self.factory.timezone("absolute garbage#!?")