Skip to content

Commit d96dc77

Browse files
committed
Use built-in isoformat support
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
1 parent 769ee25 commit d96dc77

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

pykeepass/pykeepass.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
BLANK_DATABASE_FILENAME = "blank_database.kdbx"
2929
BLANK_DATABASE_LOCATION = os.path.join(os.path.dirname(os.path.realpath(__file__)), BLANK_DATABASE_FILENAME)
3030
BLANK_DATABASE_PASSWORD = "password"
31-
DT_ISOFORMAT = "%Y-%m-%dT%H:%M:%S%fZ"
3231

3332
class PyKeePass():
3433
"""Open a KeePass database
@@ -804,7 +803,7 @@ def _encode_time(self, value):
804803
struct.pack('<Q', diff_seconds)
805804
).decode('utf-8')
806805
else:
807-
return value.strftime(DT_ISOFORMAT)
806+
return value.isoformat()
808807

809808
def _decode_time(self, text):
810809
"""datetime.datetime: Convert base64 time or plaintext time to datetime"""
@@ -819,9 +818,9 @@ def _decode_time(self, text):
819818
)
820819
)
821820
except BinasciiError:
822-
return datetime.strptime(text, DT_ISOFORMAT).replace(tzinfo=timezone.utc)
821+
return datetime.fromisoformat(text.replace('Z','+00:00')).replace(tzinfo=timezone.utc)
823822
else:
824-
return datetime.strptime(text, DT_ISOFORMAT).replace(tzinfo=timezone.utc)
823+
return datetime.fromisoformat(text.replace('Z','+00:00')).replace(tzinfo=timezone.utc)
825824

826825
def create_database(
827826
filename, password=None, keyfile=None, transformed_key=None

0 commit comments

Comments
 (0)