Skip to content
Snippets Groups Projects
Verified Commit 9c844fd7 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: table json converter: timezone handling for datetime

parent c8ea9f90
Branches
Tags
2 merge requests!107Release v0.11.0,!103xlsx -> json conversion
Pipeline #50705 passed
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
from __future__ import annotations from __future__ import annotations
import datetime
import pathlib import pathlib
from types import SimpleNamespace from types import SimpleNamespace
from typing import Any, Optional, TextIO, Union from typing import Any, Optional, TextIO, Union
...@@ -155,6 +156,7 @@ class TemplateFiller: ...@@ -155,6 +156,7 @@ class TemplateFiller:
def _handle_data(self, data: dict, current_path: list[str] = None, def _handle_data(self, data: dict, current_path: list[str] = None,
context: TemplateFiller.Context = None, context: TemplateFiller.Context = None,
only_collect_insertables: bool = False, only_collect_insertables: bool = False,
utc: bool = False,
) -> Optional[dict[str, Any]]: ) -> Optional[dict[str, Any]]:
"""Handle the data and write it into ``workbook``. """Handle the data and write it into ``workbook``.
...@@ -176,6 +178,8 @@ context: TemplateFiller.Context, optional ...@@ -176,6 +178,8 @@ context: TemplateFiller.Context, optional
only_collect_insertables: bool, optional only_collect_insertables: bool, optional
If True, do not insert anything on this level, but return a dict with entries to be inserted. If True, do not insert anything on this level, but return a dict with entries to be inserted.
utc: bool, optional
If True, store times as UTC. Else store as local time on a best-effort base.
Returns Returns
------- -------
...@@ -233,6 +237,13 @@ out: union[dict, None] ...@@ -233,6 +237,13 @@ out: union[dict, None]
value = content[0] value = content[0]
if isinstance(value, str): if isinstance(value, str):
value = ILLEGAL_CHARACTERS_RE.sub("", value) value = ILLEGAL_CHARACTERS_RE.sub("", value)
if isinstance(value, datetime.datetime):
if value.tzinfo is not None:
if utc:
value = value.astimezone(datetime.timezone.utc).replace(tzinfo=None)
else:
# Remove timezone, store in local timezone.
value = value.astimezone().replace(tzinfo=None)
path_str = p2s(path) path_str = p2s(path)
assert path_str not in insertables assert path_str not in insertables
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment