jsonifier: fix date handling

We should always keep UTC and let the consumer deal w/ it.
This change is backward compatible but allows to turn off
the old behavior w/ the ctx flag `jsonifier__date_user_tz=False`.
pull/2418/head
Simone Orsi 2022-05-03 17:45:53 +02:00 committed by Sébastien BEAU
parent 58582895e2
commit 4b4de6471e
1 changed files with 12 additions and 4 deletions

View File

@ -49,9 +49,10 @@ class Base(models.AbstractModel):
elif field.type == "datetime":
# Ensures value is a datetime
value = fields.Datetime.to_datetime(value)
# Get the timestamp converted to the client's timezone.
# This call also add the tzinfo into the datetime object
value = fields.Datetime.context_timestamp(self, value)
if self.env.context.get("jsonifier__date_user_tz"):
# Get the timestamp converted to the client's timezone.
# This call also add the tzinfo into the datetime object
value = fields.Datetime.context_timestamp(self, value)
value = value.isoformat()
elif field.type in ("many2one", "reference"):
value = value.display_name if value else None
@ -162,9 +163,16 @@ class Base(models.AbstractModel):
results = [{} for record in self]
parsers = {False: parser["fields"]} if "fields" in parser else parser["langs"]
records = self
if "jsonifier__date_user_tz" not in self.env.context:
# TODO: backward compat flag for v < 15.0
# Dates must always be UTC and the client/consumer of this data
# should deal w/ the format as preferred.
# Drop this flag for v15!
records = records.with_context(jsonifier__date_user_tz=True)
for lang in parsers:
translate = lang or parser.get("language_agnostic")
records = self.with_context(lang=lang) if translate else self
records = records.with_context(lang=lang) if translate else records
for record, json in zip(records, results):
self._jsonify_record(parsers[lang], record, json)