From e0e7a0f99f844db33964b27c29b0b8d5f160202b Mon Sep 17 00:00:00 2001 From: Nathan Henrie Date: Fri, 19 Mar 2021 08:25:35 -0600 Subject: [PATCH] Add datetime parsing --- healthkit_to_sqlite/utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/healthkit_to_sqlite/utils.py b/healthkit_to_sqlite/utils.py index fea6bc7..4d683b0 100644 --- a/healthkit_to_sqlite/utils.py +++ b/healthkit_to_sqlite/utils.py @@ -1,3 +1,4 @@ +import datetime from xml.etree import ElementTree as ET @@ -22,9 +23,18 @@ def find_all_tags(fp, tags, progress_callback=None): def convert_xml_to_sqlite(fp, db, progress_callback=None, zipfile=None): activity_summaries = [] records = [] + + datefmt = "%Y-%m-%d %H:%M:%S %z" + date_tags = {"startDate", "endDate", "creationDate"} + for tag, el in find_all_tags( fp, {"Record", "Workout", "ActivitySummary"}, progress_callback ): + for key in date_tags: + val = el.get(key) + if val is not None: + dt = datetime.datetime.strptime(val, datefmt) + el.set(key, dt) if tag == "ActivitySummary": activity_summaries.append(dict(el.attrib)) if len(activity_summaries) >= 100: