Skip to content
Open
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 utcdatetime/utcdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def from_string(cls, string):

@classmethod
def from_datetime(cls, dt):
if dt.tzinfo is None or dt.tzinfo.utcoffset(dt) is None:
raise ValueError(
"Cannot construct utcdatetime from naive datetime"
)

dt_utc = dt.astimezone(UTC)

return cls(dt_utc.year, dt_utc.month, dt_utc.day, dt_utc.hour,
Expand Down