Skip to content

Commit

Permalink
Improve datetime usage
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasl committed Oct 2, 2023
1 parent 6045f1f commit 61d5464
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions librisxl-tools/scripts/lddb_json_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from datetime import datetime
import json
import os
import re


MAX_STATS = int(os.environ.get('MAX_STATS', '512'))
Expand Down Expand Up @@ -72,12 +71,10 @@ def count_value(k, v, shape):


def isodatetime(s):
# NOTE: fromisoformat with timezones requires Python 3.11+
try:
return datetime.fromisoformat(s)
except ValueError:
# Strip TZ info:
return datetime.fromisoformat(re.sub(r'(\+[0-9:]+|[A-Z]+)$', '', s))
# NOTE: fromisoformat with zulu time requires Python 3.11+
if s.endswith('Z'):
s = s[:-1] + '+00:00'
return datetime.fromisoformat(s)


if __name__ == '__main__':
Expand Down Expand Up @@ -108,8 +105,8 @@ def isodatetime(s):
if not outdir.is_dir():
outdir.mkdir(parents=True, exist_ok=True)

min_inc_created: datetime|None = isodatetime(args.min_created) if args.min_created else None
max_ex_created: datetime|None = isodatetime(args.max_created) if args.max_created else None
min_inc_created: datetime | None = isodatetime(args.min_created) if args.min_created else None
max_ex_created: datetime | None = isodatetime(args.max_created) if args.max_created else None
if min_inc_created:
print(f"Filter - min created (inclusive): {min_inc_created}", file=sys.stderr)
if max_ex_created:
Expand All @@ -136,7 +133,7 @@ def isodatetime(s):
try:
data = json.loads(l)

if '@graph' in data:
if (min_inc_created or max_ex_created) and '@graph' in data:
try:
created = isodatetime(data['@graph'][0]['created'])
if min_inc_created and created < min_inc_created:
Expand Down

0 comments on commit 61d5464

Please sign in to comment.