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 @@
"__version__",
"get",
"now",
"timezone",
"utcnow",
"Arrow",
"ArrowFactory",
Expand Down
19 changes: 18 additions & 1 deletion arrow/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from arrow.arrow import TZ_EXPR, Arrow
from arrow.constants import DEFAULT_LOCALE
from arrow.factory import ArrowFactory
from arrow.parser import TzinfoParser

# internal default factory.
_factory = ArrowFactory()
Expand Down Expand Up @@ -118,4 +119,20 @@ def factory(type: Type[Arrow]) -> ArrowFactory:
return ArrowFactory(type)


__all__ = ["get", "utcnow", "now", "factory"]
def timezone(tz_str: str) -> dt_tzinfo:
"""Returns a timezone object from a timezone string expression.

:param tz_str: A timezone string expression, e.g. ``"US/Pacific"``, ``"UTC"``, ``"local"``.

Usage::

>>> arrow.timezone("US/Pacific")
zoneinfo.ZoneInfo(key='US/Pacific')
>>> arrow.timezone("UTC")
datetime.timezone.utc

"""
return TzinfoParser.parse(tz_str)


__all__ = ["get", "utcnow", "now", "factory", "timezone"]
18 changes: 18 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import arrow


Expand Down Expand Up @@ -25,3 +27,19 @@ class MockCustomArrowClass(arrow.Arrow):

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

def test_timezone_utc(self):
tz = arrow.timezone("UTC")
assert str(tz) == "UTC"

def test_timezone_zoneinfo(self):
tz = arrow.timezone("US/Pacific")
assert str(tz) == "US/Pacific"

def test_timezone_local(self):
tz = arrow.timezone("local")
assert tz is not None

def test_timezone_invalid(self):
with pytest.raises(arrow.parser.ParserError):
arrow.timezone("not/a/timezone")
Loading