FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Replace deprecated utcnow() and utcfromtimestamp() with timezone-aware equivalents by ADiTyaRaj8969 · Pull Request #967 · python-pendulum/pendulum · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 6 additions & 1 deletion src/pendulum/datetime.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,12 @@ def fromtimestamp(cls, t: float, tz: datetime.tzinfo | None = None) -> Self:

@classmethod
def utcfromtimestamp(cls, t: float) -> Self:
return cls.instance(datetime.datetime.utcfromtimestamp(t), tz=None)
return cls.instance(
datetime.datetime.fromtimestamp(t, tz=datetime.timezone.utc).replace(
tzinfo=None
),
tz=None,
)

@classmethod
def fromordinal(cls, n: int) -> Self:
Expand Down
31 changes: 30 additions & 1 deletion tests/datetime/test_behavior.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import date
from datetime import datetime
from datetime import time
from datetime import timezone as datetime_timezone

import pytest

Expand Down Expand Up @@ -102,11 +103,39 @@ def test_fromtimestamp():

def test_utcfromtimestamp():
p = pendulum.DateTime.utcfromtimestamp(0)
dt = datetime.utcfromtimestamp(0)
dt = datetime.fromtimestamp(0, tz=datetime_timezone.utc).replace(tzinfo=None)

assert p == dt


def test_utcfromtimestamp_known_value():
p = pendulum.DateTime.utcfromtimestamp(1577836800)

assert p.year == 2020
assert p.month == 1
assert p.day == 1
assert p.hour == 0
assert p.minute == 0
assert p.second == 0
assert p.microsecond == 0


def test_utcfromtimestamp_preserves_microseconds():
p = pendulum.DateTime.utcfromtimestamp(1577836800.123456)

assert p.year == 2020
assert p.month == 1
assert p.day == 1
assert p.microsecond == 123456


def test_utcfromtimestamp_returns_naive_pendulum_datetime():
p = pendulum.DateTime.utcfromtimestamp(1577836800)

assert isinstance(p, pendulum.DateTime)
assert p.tzinfo is None


def test_fromordinal():
assert datetime.fromordinal(730120) == pendulum.DateTime.fromordinal(730120)

Expand Down
5 changes: 3 additions & 2 deletions tests/datetime/test_construct.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

from datetime import datetime
from datetime import timezone as datetime_timezone

import pytest

Expand Down Expand Up @@ -41,7 +42,7 @@ def test_creates_an_instance_default_to_utcnow():
def test_setting_timezone():
tz = "Australia/Brisbane"
dtz = timezone(tz)
dt = datetime.utcnow()
dt = datetime.now(datetime_timezone.utc).replace(tzinfo=None)
offset = dtz.convert(dt).utcoffset().total_seconds() / 3600

p = pendulum.datetime(dt.year, dt.month, dt.day, tz=dtz)
Expand All @@ -52,7 +53,7 @@ def test_setting_timezone():
def test_setting_timezone_with_string():
tz = "Australia/Brisbane"
dtz = timezone(tz)
dt = datetime.utcnow()
dt = datetime.now(datetime_timezone.utc).replace(tzinfo=None)
offset = dtz.convert(dt).utcoffset().total_seconds() / 3600

p = pendulum.datetime(dt.year, dt.month, dt.day, tz=tz)
Expand Down

Back | FazBrowse Home | New Git URL