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: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ Supports Python **3.10 and newer**.
>>> now_in_paris
'2016-07-04T00:49:58.502116+02:00'

# Standard library timezone objects work too
>>> from datetime import timezone as datetime_timezone
>>> pendulum.now(datetime_timezone.utc).is_utc()
True

# Seamless timezone switching
>>> now_in_paris.in_timezone('UTC')
'2016-07-03T22:49:58.502116+00:00'
Expand Down
3 changes: 3 additions & 0 deletions docs/docs/instantiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ There is also the `now()` method.
'Europe/London'
```

`now()` also accepts standard library `datetime.tzinfo` objects, including
`datetime.timezone.utc` and `zoneinfo.ZoneInfo`.

To accompany `now()`, a few other static instantiation helpers exist to create known instances.
The only thing to really notice here is that `today()`, `tomorrow()` and `yesterday()`,
besides behaving as expected, all accept a timezone parameter
Expand Down
8 changes: 4 additions & 4 deletions src/pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,28 +238,28 @@ def instance(
return DateTime.instance(obj, tz=tz)


def now(tz: str | Timezone | None = None) -> DateTime:
def now(tz: str | _datetime.tzinfo | None = None) -> DateTime:
"""
Get a DateTime instance for the current date and time.
"""
return DateTime.now(tz)


def today(tz: str | Timezone = "local") -> DateTime:
def today(tz: str | _datetime.tzinfo = "local") -> DateTime:
"""
Create a DateTime instance for today.
"""
return now(tz).start_of("day")


def tomorrow(tz: str | Timezone = "local") -> DateTime:
def tomorrow(tz: str | _datetime.tzinfo = "local") -> DateTime:
"""
Create a DateTime instance for tomorrow.
"""
return today(tz).add(days=1)


def yesterday(tz: str | Timezone = "local") -> DateTime:
def yesterday(tz: str | _datetime.tzinfo = "local") -> DateTime:
"""
Create a DateTime instance for yesterday.
"""
Expand Down
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import date
from datetime import datetime
from datetime import time
from datetime import timezone as datetime_timezone

from dateutil import tz

Expand All @@ -15,6 +16,27 @@
from pendulum.tz.timezone import Timezone


def test_now_helpers_accept_stdlib_timezone() -> None:
for value in (
pendulum.now(datetime_timezone.utc),
pendulum.today(datetime_timezone.utc),
pendulum.tomorrow(datetime_timezone.utc),
pendulum.yesterday(datetime_timezone.utc),
):
assert value.timezone_name == "UTC"


def test_now_helpers_accept_zoneinfo_timezone() -> None:
paris = zoneinfo.ZoneInfo("Europe/Paris")
for value in (
pendulum.now(paris),
pendulum.today(paris),
pendulum.tomorrow(paris),
pendulum.yesterday(paris),
):
assert value.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