Skip to content

Commit 171bc98

Browse files
author
Johannes Otepka
committed
tzinfo got lost when storing in mongodb
1 parent 102ec7d commit 171bc98

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

ipyparallel/controller/mongodb.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from traitlets import Dict, Instance, List, Unicode
1616

1717
from .dictdb import BaseDB
18+
from datetime import datetime, timezone
1819

1920
# we need to determine the pymongo version because of API changes. see
2021
# https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html
@@ -25,6 +26,16 @@
2526
# MongoDB class
2627
# -----------------------------------------------------------------------------
2728

29+
def _ensure_utc(obj):
30+
if isinstance(obj, datetime):
31+
obj = obj.replace(tzinfo=timezone.utc)
32+
return obj
33+
34+
def _ensure_utc_for_record(rec):
35+
for key in ('submitted', 'started', 'completed', 'received'):
36+
if key in rec:
37+
rec[key] = _ensure_utc(rec[key])
38+
2839

2940
class MongoDB(BaseDB):
3041
"""MongoDB TaskRecord backend."""
@@ -91,6 +102,7 @@ def get_record(self, msg_id):
91102
if not r:
92103
# r will be '' if nothing is found
93104
raise KeyError(msg_id)
105+
_ensure_utc_for_record(r)
94106
return r
95107

96108
def update_record(self, msg_id, rec):
@@ -125,6 +137,7 @@ def find_records(self, check, keys=None):
125137
matches = list(self._records.find(check, keys))
126138
for rec in matches:
127139
rec.pop('_id')
140+
_ensure_utc_for_record(rec)
128141
return matches
129142

130143
def get_history(self):

0 commit comments

Comments
 (0)