util/records: always return an utc timestamp

We must at all cost avoid going through the local time
utilities when dealing with naive dates.
2 files changed, 27 insertions(+), 0 deletions(-)

M test/test_util.py
M tshistory/util.py
M test/test_util.py +23 -0
@@ 53,6 53,29 @@ def test_make_snapshot_record():
     assert bstart == 42
     assert offset == 155
 
+    rec = make_snapshot_record(
+        1,
+        datetime(2020, 1, 1),
+        datetime(2020, 1, 2),
+        0,
+        True,
+        42,
+        155
+    )
+    assert len(rec) == 28
+    assert isinstance(rec, bytearray)
+
+    rid, start, end, parent, packed, bstart, offset = unpack_snapshot_record(
+        bytes(rec)
+    )
+    assert rid == 2
+    assert start == utcdt(2020, 1, 1)
+    assert end == utcdt(2020, 1, 2)
+    assert parent == 0
+    assert packed
+    assert bstart == 42
+    assert offset == 155
+
 
 def test_patch():
     s1 = pd.Series(

          
M tshistory/util.py +4 -0
@@ 427,6 427,10 @@ def _fromjson(jsonb, tsname):
 # file binary serialisation
 
 def make_snapshot_record(lastid, start, end, parent, packed, bstart, offset):
+    if start.tzinfo is None:
+        start = start.replace(tzinfo=pytz.utc)
+    if end.tzinfo is None:
+        end = end.replace(tzinfo=pytz.utc)
     # everything consumes 4 octets
     buffer = bytearray(28)
     newid = struct.pack_into('!I', buffer, 0, lastid + 1)