Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion arrow/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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#!?")