From 4b4de6471ef528b77f1634c915d7c9c0dbd250bc Mon Sep 17 00:00:00 2001 From: Simone Orsi Date: Tue, 3 May 2022 17:45:53 +0200 Subject: [PATCH] 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`. --- jsonifier/models/models.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/jsonifier/models/models.py b/jsonifier/models/models.py index 226f771ab..bf0fa2112 100644 --- a/jsonifier/models/models.py +++ b/jsonifier/models/models.py @@ -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)