Skip to content

Commit

Permalink
Use built-in isoformat support (#383)
Browse files Browse the repository at this point in the history
The ISO formatted string can be stored in multiple formats. Use the built-in datetime.isoformat() and datetime.fromisoformat() functions, which able to handle all variants.
Replace 'Z' with '+00:00' to ensure compatibility with Python < 3.11.

Fixes: #382
  • Loading branch information
City-busz authored Mar 11, 2024
1 parent 4c8a1cc commit 18ec462
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions pykeepass/pykeepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
BLANK_DATABASE_FILENAME = "blank_database.kdbx"
BLANK_DATABASE_LOCATION = os.path.join(os.path.dirname(os.path.realpath(__file__)), BLANK_DATABASE_FILENAME)
BLANK_DATABASE_PASSWORD = "password"
DT_ISOFORMAT = "%Y-%m-%dT%H:%M:%S%fZ"

class PyKeePass():
"""Open a KeePass database
Expand Down Expand Up @@ -804,7 +803,7 @@ def _encode_time(self, value):
struct.pack('<Q', diff_seconds)
).decode('utf-8')
else:
return value.strftime(DT_ISOFORMAT)
return value.isoformat()

def _decode_time(self, text):
"""datetime.datetime: Convert base64 time or plaintext time to datetime"""
Expand All @@ -819,9 +818,9 @@ def _decode_time(self, text):
)
)
except BinasciiError:
return datetime.strptime(text, DT_ISOFORMAT).replace(tzinfo=timezone.utc)
return datetime.fromisoformat(text.replace('Z','+00:00')).replace(tzinfo=timezone.utc)
else:
return datetime.strptime(text, DT_ISOFORMAT).replace(tzinfo=timezone.utc)
return datetime.fromisoformat(text.replace('Z','+00:00')).replace(tzinfo=timezone.utc)

def create_database(
filename, password=None, keyfile=None, transformed_key=None
Expand Down

0 comments on commit 18ec462

Please sign in to comment.