Skip to content

[SPARK-48695][PYTHON] TimestampNTZType.fromInternal not use the deprecated methods #47068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 8 additions & 0 deletions python/pyspark/sql/tests/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ def test_time_with_timezone(self):
self.assertEqual(now, now1)
self.assertEqual(now, utcnow1)

def test_ntz_from_internal(self):
for ts in [1, 22, 333, 44444444, 5555555555]:
t1 = datetime.datetime.utcfromtimestamp(ts // 1000000).replace(microsecond=ts % 1000000)
t2 = datetime.datetime.fromtimestamp(ts // 1000000, datetime.timezone.utc).replace(
microsecond=ts % 1000000, tzinfo=None
)
self.assertEqual(t1, t2)

# regression test for SPARK-19561
def test_datetime_at_epoch(self):
epoch = datetime.datetime.fromtimestamp(0)
Expand Down
4 changes: 2 additions & 2 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ def toInternal(self, dt: datetime.datetime) -> int:
def fromInternal(self, ts: int) -> datetime.datetime:
if ts is not None:
# using int to avoid precision loss in float
return datetime.datetime.utcfromtimestamp(ts // 1000000).replace(
microsecond=ts % 1000000
return datetime.datetime.fromtimestamp(ts // 1000000, datetime.timezone.utc).replace(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datetime.UTC was added in 3.11, so use datetime.timezone.utc to support 3.9/3.10

microsecond=ts % 1000000, tzinfo=None
)


Expand Down