Skip to content

Commit

Permalink
fix: Manually remove nanoseconds in datetime string with python < 3.11
Browse files Browse the repository at this point in the history
Fix #10
  • Loading branch information
Rafiot committed Nov 2, 2023
1 parent eef3cb0 commit 8cbb361
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions vulnerabilitylookup/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import re
import sys
import configparser

Expand All @@ -10,9 +11,16 @@


def fromisoformat_wrapper(date_iso: str, /) -> datetime:
# Python < 3.11 cannot load times with a Z instead of +00:00
if sys.version_info < (3, 11) and date_iso.endswith('Z'):
date_iso = date_iso.replace('Z', '+00:00')
if sys.version_info < (3, 11):
if date_iso.endswith('Z'):
# Python < 3.11 cannot load times with a Z instead of +00:00
date_iso = date_iso.replace('Z', '+00:00')

# Sometimes, the string also contains nanoseconds for no good reason, and it fails with python < 3.11
# Is is useless so we strip that.
frac_sec = re.findall(r'[.](\d+)', date_iso)
if frac_sec and len(frac_sec[0]) > 6:
date_iso = date_iso.replace(frac_sec[0], frac_sec[0][:6])
return datetime.fromisoformat(date_iso)


Expand Down

0 comments on commit 8cbb361

Please sign in to comment.