Skip to content

Commit

Permalink
Add timezone to the jsod file
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinse committed Aug 15, 2024
1 parent b059a05 commit 066e192
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dev =
coverage
pytest-cov
pytest-mock
freezegun

[options.entry_points]
console_scripts =
Expand Down
2 changes: 1 addition & 1 deletion src/objdictgen/jsonod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 28 additions & 1 deletion tests/test_jsonod.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 066e192

Please sign in to comment.