Generate and use TZ-aware datetime objects

pgpy has been using TZ-naive datetime objects, despite all OpenPGP
wire-format dates being essentially UTC.

Better to represent the datetime objects explicitly as UTC.

Closes: #401
This commit is contained in:
Daniel Kahn Gillmor
2022-04-29 15:46:31 -04:00
parent 473d3651e2
commit c880d72902
5 changed files with 21 additions and 20 deletions

View File

@@ -4,7 +4,7 @@ import pytest
import glob
import os
from datetime import datetime
from datetime import datetime, timezone
from pgpy.constants import HashAlgorithm
from pgpy.constants import PubKeyAlgorithm
@@ -187,7 +187,7 @@ block_attrs = {
b'\x55\x5d'),
('cipherprefs', []),
('compprefs', []),
('created', datetime.utcfromtimestamp(1402615373)),
('created', datetime.fromtimestamp(1402615373, timezone.utc)),
('embedded', False),
('exportable', True),
('features', set()),

View File

@@ -14,7 +14,7 @@ import itertools
import os
import time
import warnings
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from pgpy import PGPKey
from pgpy import PGPMessage
@@ -325,7 +325,7 @@ class TestPGPKey_Management(object):
key = self.keys[pkspec]
uid = PGPUID.new('T. Keyerson', 'Secondary UID', 'testkey@localhost.local')
expiration = datetime.utcnow() + timedelta(days=2)
expiration = datetime.now(timezone.utc) + timedelta(days=2)
# add all of the sbpackets that only work on self-certifications
with warnings.catch_warnings():
@@ -697,7 +697,7 @@ class TestPGPKey_Actions(object):
# assert sig.sig.signer_uid == "{:s}".format(sec.userids[0])
assert next(iter(sig._signature.subpackets['SignersUserID'])).userid == "{:s}".format(targette_sec.userids[0])
# if not sig.is_expired:
# time.sleep((sig.expires_at - datetime.utcnow()).total_seconds())
# time.sleep((sig.expires_at - datetime.now(timezone.utc)).total_seconds())
assert sig.is_expired is False
self.sigs['string'] = sig
@@ -738,7 +738,7 @@ class TestPGPKey_Actions(object):
def test_sign_ctmessage(self, targette_sec, targette_pub, ctmessage):
# test signing a cleartext message
expire_at = datetime.utcnow() + timedelta(days=1)
expire_at = datetime.now(timezone.utc) + timedelta(days=1)
sig = targette_sec.sign(ctmessage, expires=expire_at)