From 84448427c50c4d8678b2848e13b10b0f86ce6093 Mon Sep 17 00:00:00 2001 From: emme1t <149944796+emme1t@users.noreply.github.com> Date: Sat, 21 Nov 2026 01:04:57 -0800 Subject: [PATCH] Type numeric UTC offsets for now() --- docs/docs/instantiation.md | 5 ++++- src/pendulum/__init__.py | 2 +- src/pendulum/datetime.py | 12 +----------- tests/test_main.py | 11 +++++++++++ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/docs/docs/instantiation.md b/docs/docs/instantiation.md index a49c6a26c..3e495f33f 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 16ae0865c..ca089b6df 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 96aff25b2..01a89eb38 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 76033450a..9e0392b00 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"