From e0b852bc6e4a849d6f227cfb3d5e6debb593c309 Mon Sep 17 00:00:00 2001 From: Mathieu Duckerts-Antoine Date: Mon, 17 Oct 2022 14:09:30 +0000 Subject: [PATCH] [FIX] web: core: offset in to_utc Take a PyDateTime representing the moment "2022-10-17 00:00:00" in the timezone of Brussels. Trying to get the related utc moment through to_utc gives wrongly "2022-10-16 23:00:00". This happens because the months are not numbered in the same way in Date or PyDateTime, so that in October for example, the offset applied was that of November which is -60 instead of -120 (summer/winter change). We fix that problem. closes odoo/odoo#103561 X-original-commit: a11e1fb5f702400fe51938362fe3f24570977ba9 Signed-off-by: Aaron Bohy (aab) Signed-off-by: Mathieu Duckerts-Antoine --- addons/web/static/src/core/py_js/py_date.js | 2 +- .../static/tests/core/py_js/py_date_tests.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/addons/web/static/src/core/py_js/py_date.js b/addons/web/static/src/core/py_js/py_date.js index 0d3ac955c4e..767c6144229 100644 --- a/addons/web/static/src/core/py_js/py_date.js +++ b/addons/web/static/src/core/py_js/py_date.js @@ -489,7 +489,7 @@ export class PyDateTime { * @returns {PyDateTime} */ to_utc() { - const d = new Date(this.year, this.month, this.day, this.hour, this.minute, this.second); + const d = new Date(this.year, this.month -1, this.day, this.hour, this.minute, this.second); const timedelta = PyTimeDelta.create({ minutes: d.getTimezoneOffset() }); return this.add(timedelta); } diff --git a/addons/web/static/tests/core/py_js/py_date_tests.js b/addons/web/static/tests/core/py_js/py_date_tests.js index 4058d2938f4..78c4e287620 100644 --- a/addons/web/static/tests/core/py_js/py_date_tests.js +++ b/addons/web/static/tests/core/py_js/py_date_tests.js @@ -71,6 +71,24 @@ QUnit.module("py", {}, () => { assert.strictEqual(JSON.stringify(evaluateExpr(expr)), `"2021-09-16 18:00:00"`); }); + QUnit.test("to_utc in october with winter/summer change", (assert) => { + patchDate(2021, 9, 17, 10, 0, 0); + patchWithCleanup(Date.prototype, { + getTimezoneOffset() { + const month = this.getMonth() // starts at 0; + if (10 <= month || month <= 2) { + //rough approximation + return -60; + } else { + return -120; + } + }, + }); + const expr = + "datetime.datetime(2022, 10, 17).to_utc()"; + assert.strictEqual(JSON.stringify(evaluateExpr(expr)), `"2022-10-16 22:00:00"`); + }); + QUnit.test("datetime.datetime.combine", (assert) => { const expr = "datetime.datetime.combine(context_today(), datetime.time(23,59,59)).strftime('%Y-%m-%d %H:%M:%S')";