From 69bc97d856a847067480071e2f3ad8e6756752c2 Mon Sep 17 00:00:00 2001 From: Svein Seldal Date: Wed, 28 Aug 2024 11:33:23 +0200 Subject: [PATCH] Add timezone to the jsod file (#33) --- setup.cfg | 1 + src/objdictgen/jsonod.py | 2 +- tests/test_jsonod.py | 29 ++++++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index c7e863e..5c3e844 100644 --- a/setup.cfg +++ b/setup.cfg @@ -58,6 +58,7 @@ dev = coverage pytest-cov pytest-mock + freezegun [options.entry_points] console_scripts = diff --git a/src/objdictgen/jsonod.py b/src/objdictgen/jsonod.py index 86458db..c1ad029 100644 --- a/src/objdictgen/jsonod.py +++ b/src/objdictgen/jsonod.py @@ -510,7 +510,7 @@ def node_todict(node: "Node", sort=False, rich=True, internal=False, validate=Tr '$version': JSON_INTERNAL_VERSION if internal else JSON_VERSION, '$description': JSON_DESCRIPTION, '$tool': str(objdictgen.ODG_PROGRAM) + ' ' + str(objdictgen.__version__), - '$date': datetime.isoformat(datetime.now()), + '$date': datetime.now().astimezone().isoformat(), 'name': node.Name, 'description': node.Description, 'type': node.Type, diff --git a/tests/test_jsonod.py b/tests/test_jsonod.py index a158ab3..98df538 100644 --- a/tests/test_jsonod.py +++ b/tests/test_jsonod.py @@ -1,5 +1,7 @@ -import pytest +import re from pprint import pprint +import datetime +from freezegun import freeze_time from objdictgen import Node from objdictgen.jsonod import generate_jsonc, generate_node, remove_jsonc from .test_odcompare import shave_equal @@ -103,3 +105,28 @@ def test_jsonod_roundtrip_internal(odjsoneds): a, b = shave_equal(m1, m2, ignore=["IndexOrder", "DefaultStringSize"]) assert a == b + + +def test_jsonod_timezone(): + """ Test timezone handling in the jsonod module. """ + + for now, offset, cmp in [ + ("2020-01-01 12:00:00", None, "2020-01-01T12:00:00"), + ("2020-01-01 12:00:00", 5, "2020-01-01T17:00:00"), + ("2020-01-01 12:00:00", -5, "2020-01-01T07:00:00"), + ]: + kw = {} + if offset is not None: + kw["tz_offset"] = offset + with freeze_time(now, **kw): + # The tzoffset is dependent on the current time, so we need to calculate it + tzoffset = datetime.datetime.now().astimezone().utcoffset() + tz = f"{tzoffset.seconds//3600:02}:{tzoffset.seconds%60:02}" + print(f"tzoffset: {tzoffset}, {type(tzoffset)} {tz}") + + od = Node() + out = generate_jsonc(od, compact=False, sort=False, internal=False, validate=True) + m = re.search(r'^\s+"\$date": "(.*?)",$', out, re.M) + if m: + print(m[1]) + assert m[1] == cmp + "+" + tz