Skip to content
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

deepcopy on DateTime produces incorrect offset in copy during dst transition #767

Closed
2 tasks done
NorthWindH opened this issue Nov 1, 2023 · 0 comments · Fixed by #776
Closed
2 tasks done

deepcopy on DateTime produces incorrect offset in copy during dst transition #767

NorthWindH opened this issue Nov 1, 2023 · 0 comments · Fixed by #776

Comments

@NorthWindH
Copy link

  • I am on the latest Pendulum version.
  • I have searched the issues of this repo and believe that this is not a duplicate.
  • OS version and name: MacOS 12.0.1 Monterey
  • Pendulum version: 2.1.2
  • Python version: 3.11.5

Issue also presents on python 3.10.1 and python 3.10.13

Issue

When passing a DateTime undergoing dst transition to standard deepcopy, the produced DateTime has an invalid offset.

Reproduction

Code

import pendulum
from copy import deepcopy

d = pendulum.from_timestamp(1699174800.0, tz="US/Pacific")

print(f"Original: {d!r}")
print(d.isoformat())
print(d.timestamp())

d2 = deepcopy(d)
print(f"Copied: {d2!r}")
print(d2.isoformat())
print(d2.timestamp())

Output

Original: DateTime(2023, 11, 5, 1, 0, 0, tzinfo=Timezone('US/Pacific'))
2023-11-05T01:00:00-08:00
1699174800.0
Copied: DateTime(2023, 11, 5, 1, 0, 0, tzinfo=Timezone('US/Pacific'))
2023-11-05T01:00:00-07:00
1699171200.0

Workaround

An ugly hack can be used to work around the issue:

# UGLY HACK
from pendulum.datetime import DateTime
DateTime.__deepcopy__ = lambda d, *args: d

# Script from above
import pendulum
from copy import deepcopy

d = pendulum.from_timestamp(1699174800.0, tz="US/Pacific")

print(f"Original: {d!r}")
print(d.isoformat())
print(d.timestamp())

d2 = deepcopy(d)
print(f"Copied: {d2!r}")
print(d2.isoformat())
print(d2.timestamp())

Result:

Original: DateTime(2023, 11, 5, 1, 0, 0, tzinfo=Timezone('US/Pacific'))
2023-11-05T01:00:00-08:00
1699174800.0
Copied: DateTime(2023, 11, 5, 1, 0, 0, tzinfo=Timezone('US/Pacific'))
2023-11-05T01:00:00-08:00
1699174800.0

Relevance

Deepcopy on DateTimes occurs any time dataclass' module-level asdict is called on a dataclass object that contains DateTime objects, triggering this bug. asdict in turn is called by pydantic's json encoder function pydantic_encoder, which is how we found out about this in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant