diff --git a/docs/docs/instantiation.md b/docs/docs/instantiation.md index a49c6a26..3e495f33 100644 --- a/docs/docs/instantiation.md +++ b/docs/docs/instantiation.md @@ -52,7 +52,8 @@ timezone to the local timezone. `local()` is just an alias for `datetime(..., tz='local')`. -There is also the `now()` method. +The `now()` method accepts a timezone name, a `tzinfo` object, or a numeric +UTC offset in hours. It uses the local timezone by default. ```python >>> import pendulum @@ -62,6 +63,8 @@ There is also the `now()` method. >>> now_in_london_tz = pendulum.now('Europe/London') >>> now_in_london_tz.timezone_name 'Europe/London' +>>> pendulum.now(5.5).timezone_name +'+05:30' ``` To accompany `now()`, a few other static instantiation helpers exist to create known instances. diff --git a/src/pendulum/__init__.py b/src/pendulum/__init__.py index 16ae0865..ca089b6d 100644 --- a/src/pendulum/__init__.py +++ b/src/pendulum/__init__.py @@ -238,7 +238,7 @@ def instance( return DateTime.instance(obj, tz=tz) -def now(tz: str | Timezone | None = None) -> DateTime: +def now(tz: str | float | _datetime.tzinfo | None = None) -> DateTime: """ Get a DateTime instance for the current date and time. """ diff --git a/src/pendulum/datetime.py b/src/pendulum/datetime.py index 96aff25b..01a89eb3 100644 --- a/src/pendulum/datetime.py +++ b/src/pendulum/datetime.py @@ -147,18 +147,8 @@ def instance( fold=dt.fold, ) - @overload - @classmethod - def now(cls, tz: datetime.tzinfo | None = None) -> Self: ... - - @overload @classmethod - def now(cls, tz: str | Timezone | FixedTimezone | None = None) -> Self: ... - - @classmethod - def now( - cls, tz: str | Timezone | FixedTimezone | datetime.tzinfo | None = None - ) -> Self: + def now(cls, tz: str | float | datetime.tzinfo | None = None) -> Self: """ Get a DateTime instance for the current date and time. """ diff --git a/tests/test_main.py b/tests/test_main.py index 76033450..9e0392b0 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -15,6 +15,17 @@ from pendulum.tz.timezone import Timezone +def test_now_with_numeric_offsets() -> None: + assert pendulum.now(5.5).timezone_name == "+05:30" + assert pendulum.DateTime.now(-3.5).timezone_name == "-03:30" + + +def test_now_with_zoneinfo() -> None: + assert ( + pendulum.now(zoneinfo.ZoneInfo("Europe/Paris")).timezone_name == "Europe/Paris" + ) + + def test_instance_with_naive_datetime_defaults_to_utc() -> None: now = pendulum.instance(datetime.now()) assert now.timezone_name == "UTC"