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
5 changes: 4 additions & 1 deletion docs/docs/instantiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
12 changes: 1 addition & 11 deletions src/pendulum/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down