|
15 | 15 | from traitlets import Dict, Instance, List, Unicode |
16 | 16 |
|
17 | 17 | from .dictdb import BaseDB |
| 18 | +from datetime import datetime, timezone |
18 | 19 |
|
19 | 20 | # we need to determine the pymongo version because of API changes. see |
20 | 21 | # https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html |
|
25 | 26 | # MongoDB class |
26 | 27 | # ----------------------------------------------------------------------------- |
27 | 28 |
|
| 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 | + |
28 | 39 |
|
29 | 40 | class MongoDB(BaseDB): |
30 | 41 | """MongoDB TaskRecord backend.""" |
@@ -91,6 +102,7 @@ def get_record(self, msg_id): |
91 | 102 | if not r: |
92 | 103 | # r will be '' if nothing is found |
93 | 104 | raise KeyError(msg_id) |
| 105 | + _ensure_utc_for_record(r) |
94 | 106 | return r |
95 | 107 |
|
96 | 108 | def update_record(self, msg_id, rec): |
@@ -125,6 +137,7 @@ def find_records(self, check, keys=None): |
125 | 137 | matches = list(self._records.find(check, keys)) |
126 | 138 | for rec in matches: |
127 | 139 | rec.pop('_id') |
| 140 | + _ensure_utc_for_record(rec) |
128 | 141 | return matches |
129 | 142 |
|
130 | 143 | def get_history(self): |
|
0 commit comments