Warn when the user passes a TZ-naive datetime object

TZ-naive datetime objects are looking for trouble.  We would prefer to
do all comparisons and work using TZ-aware datetime objects.
This commit is contained in:
Daniel Kahn Gillmor
2022-04-29 15:58:30 -04:00
parent c880d72902
commit 65613853fd
2 changed files with 8 additions and 0 deletions

View File

@@ -7,6 +7,7 @@ import copy
import hashlib
import os
import re
import warnings
from datetime import datetime, timezone
@@ -763,6 +764,8 @@ class PubKeyV4(PubKey):
@created.register(datetime)
def created_datetime(self, val):
if val.tzinfo is None:
warnings.warn("Passing TZ-naive datetime object to PubKeyV4 packet")
self._created = val
@created.register(int)
@@ -1183,6 +1186,8 @@ class LiteralData(Packet):
@mtime.register(datetime)
def mtime_datetime(self, val):
if val.tzinfo is None:
warnings.warn("Passing TZ-naive datetime object to LiteralData packet")
self._mtime = val
@mtime.register(int)

View File

@@ -4,6 +4,7 @@ Signature SubPackets
"""
import binascii
import calendar
import warnings
from datetime import datetime
from datetime import timedelta
@@ -230,6 +231,8 @@ class CreationTime(Signature):
@created.register(datetime)
def created_datetime(self, val):
if val.tzinfo is None:
warnings.warn("Passing TZ-naive datetime object to CreationTime subpacket")
self._created = val
@created.register(int)