Skip to content

Commit

Permalink
Merge pull request #415 from dkg/tz-aware-datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
Commod0re committed Nov 9, 2022
2 parents d8a7289 + 6561385 commit b5cc2f3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
15 changes: 10 additions & 5 deletions pgpy/packet/packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import hashlib
import os
import re
import warnings

from datetime import datetime
from datetime import datetime, timezone

import six

Expand Down Expand Up @@ -763,11 +764,13 @@ def created(self):

@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)
def created_int(self, val):
self.created = datetime.utcfromtimestamp(val)
self.created = datetime.fromtimestamp(val, timezone.utc)

@created.register(bytes)
@created.register(bytearray)
Expand Down Expand Up @@ -846,7 +849,7 @@ def fingerprint(self):

def __init__(self):
super(PubKeyV4, self).__init__()
self.created = datetime.utcnow()
self.created = datetime.now(timezone.utc)
self.pkalg = 0
self.keymaterial = None

Expand Down Expand Up @@ -1183,11 +1186,13 @@ def mtime(self):

@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)
def mtime_int(self, val):
self.mtime = datetime.utcfromtimestamp(val)
self.mtime = datetime.fromtimestamp(val, timezone.utc)

@mtime.register(bytes)
@mtime.register(bytearray)
Expand All @@ -1208,7 +1213,7 @@ def __init__(self):
super(LiteralData, self).__init__()
self.format = 'b'
self.filename = ''
self.mtime = datetime.utcnow()
self.mtime = datetime.now(timezone.utc)
self._contents = bytearray()

def __bytearray__(self):
Expand Down
8 changes: 6 additions & 2 deletions pgpy/packet/subpackets/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"""
import binascii
import calendar
import warnings

from datetime import datetime
from datetime import timedelta
from datetime import timezone

import six

Expand Down Expand Up @@ -229,19 +231,21 @@ def created(self):

@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)
def created_int(self, val):
self.created = datetime.utcfromtimestamp(val)
self.created = datetime.fromtimestamp(val, timezone.utc)

@created.register(bytearray)
def created_bytearray(self, val):
self.created = self.bytes_to_int(val)

def __init__(self):
super(CreationTime, self).__init__()
self.created = datetime.utcnow()
self.created = datetime.now(timezone.utc)

def __bytearray__(self):
_bytes = super(CreationTime, self).__bytearray__()
Expand Down
14 changes: 7 additions & 7 deletions pgpy/pgp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import six

from datetime import datetime
from datetime import datetime, timezone

from cryptography.hazmat.primitives import hashes

Expand Down Expand Up @@ -179,7 +179,7 @@ def is_expired(self):
"""
expires_at = self.expires_at
if expires_at is not None and expires_at != self.created:
return expires_at < datetime.utcnow()
return expires_at < datetime.now(timezone.utc)

return False

Expand Down Expand Up @@ -321,7 +321,7 @@ def new(cls, sigtype, pkalg, halg, signer, created=None):
sig = PGPSignature()

if created is None:
created=datetime.utcnow()
created=datetime.now(timezone.utc)
sigpkt = SignatureV4()
sigpkt.header.tag = 2
sigpkt.header.version = 4
Expand Down Expand Up @@ -720,7 +720,7 @@ def attested_to(self, certifications):
return
mostrecent = None
attestations = []
now = datetime.utcnow()
now = datetime.now(timezone.utc)
fpr = self.parent.fingerprint
keyid = self.parent.fingerprint.keyid
for sig in self._signatures:
Expand Down Expand Up @@ -1122,7 +1122,7 @@ def new(cls, message, **kwargs):
charset = kwargs.pop('encoding', None)

filename = ''
mtime = datetime.utcnow()
mtime = datetime.now(timezeone.utc)

msg = PGPMessage()

Expand All @@ -1136,7 +1136,7 @@ def new(cls, message, **kwargs):
if file and os.path.isfile(message):
filename = message
message = bytearray(os.path.getsize(filename))
mtime = datetime.utcfromtimestamp(os.path.getmtime(filename))
mtime = datetime.fromtimestamp(os.path.getmtime(filename), timezone.utc)

with open(filename, 'rb') as mf:
mf.readinto(message)
Expand Down Expand Up @@ -1418,7 +1418,7 @@ def is_expired(self):
"""``True`` if this key is expired, otherwise ``False``"""
expires = self.expires_at
if expires is not None:
return expires <= datetime.utcnow()
return expires <= datetime.now(timezone.utc)

return False

Expand Down
4 changes: 2 additions & 2 deletions tests/test_03_armor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import glob
import os
from datetime import datetime
from datetime import datetime, timezone

from pgpy.constants import HashAlgorithm
from pgpy.constants import PubKeyAlgorithm
Expand Down Expand Up @@ -187,7 +187,7 @@
b'\x55\x5d'),
('cipherprefs', []),
('compprefs', []),
('created', datetime.utcfromtimestamp(1402615373)),
('created', datetime.fromtimestamp(1402615373, timezone.utc)),
('embedded', False),
('exportable', True),
('features', set()),
Expand Down
8 changes: 4 additions & 4 deletions tests/test_05_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
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
Expand Down Expand Up @@ -325,7 +325,7 @@ def test_add_altuid(self, pkspec):
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():
Expand Down Expand Up @@ -697,7 +697,7 @@ def test_sign_string(self, targette_sec, targette_pub, string):
# 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
Expand Down Expand Up @@ -738,7 +738,7 @@ def test_verify_message(self, targette_pub, message):

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)

Expand Down

0 comments on commit b5cc2f3

Please sign in to comment.