diff --git a/addons/web/static/src/search/utils/dates.js b/addons/web/static/src/search/utils/dates.js index 5432f848ad7..6f83a3b7628 100644 --- a/addons/web/static/src/search/utils/dates.js +++ b/addons/web/static/src/search/utils/dates.js @@ -210,10 +210,7 @@ export function constructDateRange(params) { setParam.month = QUARTERS[setParam.quarter].coveredMonths[0]; delete setParam.quarter; } - const date = referenceMoment - .set(setParam) - .plus(plusParam || {}) - .setZone("utc", { keepLocalTime: true }); + const date = referenceMoment.set(setParam).plus(plusParam || {}); // compute domain const leftDate = date.startOf(granularity); const rightDate = date.endOf(granularity); diff --git a/addons/web/static/tests/search/search_utils_tests.js b/addons/web/static/tests/search/search_utils_tests.js index 6e541a105d1..5a7bcd73b5f 100644 --- a/addons/web/static/tests/search/search_utils_tests.js +++ b/addons/web/static/tests/search/search_utils_tests.js @@ -6,18 +6,78 @@ import { Domain } from "@web/core/domain"; import { localization } from "@web/core/l10n/localization"; import { patch, unpatch } from "@web/core/utils/patch"; import { patchDate } from "@web/../tests/helpers/utils"; +import { registerCleanup } from "@web/../tests/helpers/cleanup"; import { translatedTerms } from "@web/core/l10n/translation"; const { DateTime } = luxon; +function patchTimeZone(offset) { + const fixedZone = new luxon.FixedOffsetZone.instance(offset); + const originalZone = luxon.Settings.defaultZone; + luxon.Settings.defaultZone = fixedZone.name; + registerCleanup(() => { + luxon.Settings.defaultZone = originalZone; + }); +} + QUnit.module("Search", () => { - QUnit.module("SearchUtils"); + QUnit.module("SearchUtils", { + beforeEach() { + patchTimeZone(0); + }, + }); QUnit.test( "construct simple domain based on date field (no comparisonOptionId)", function (assert) { patchDate(2020, 5, 1, 13, 0, 0); - const referenceMoment = DateTime.utc(); + const referenceMoment = DateTime.local(); + assert.deepEqual(constructDateDomain(referenceMoment, "date_field", "date", []), { + domain: new Domain(`[]`), + description: "", + }); + assert.deepEqual( + constructDateDomain(referenceMoment, "date_field", "date", [ + "this_month", + "this_year", + ]), + { + domain: new Domain( + `["&", ("date_field", ">=", "2020-06-01"), ("date_field", "<=", "2020-06-30")]` + ), + description: "June 2020", + } + ); + assert.deepEqual( + constructDateDomain(referenceMoment, "date_field", "date", [ + "second_quarter", + "this_year", + ]), + { + domain: new Domain( + `["&", ("date_field", ">=", "2020-04-01"), ("date_field", "<=", "2020-06-30")]` + ), + description: "Q2 2020", + } + ); + assert.deepEqual( + constructDateDomain(referenceMoment, "date_field", "date", ["this_year"]), + { + domain: new Domain( + `["&", ("date_field", ">=", "2020-01-01"), ("date_field", "<=", "2020-12-31")]` + ), + description: "2020", + } + ); + } + ); + + QUnit.test( + "construct simple domain based on date field (no comparisonOptionId) - UTC+2", + (assert) => { + patchTimeZone(120); + patchDate(2020, 5, 1, 0, 0, 0); + const referenceMoment = DateTime.local(); assert.deepEqual(constructDateDomain(referenceMoment, "date_field", "date", []), { domain: new Domain(`[]`), description: "", @@ -62,7 +122,7 @@ QUnit.module("Search", () => { "construct simple domain based on datetime field (no comparisonOptionId)", function (assert) { patchDate(2020, 5, 1, 13, 0, 0); - const referenceMoment = DateTime.utc(); + const referenceMoment = DateTime.local(); assert.deepEqual( constructDateDomain(referenceMoment, "date_field", "datetime", [ "this_month", @@ -98,9 +158,52 @@ QUnit.module("Search", () => { ); } ); + + QUnit.test( + "construct simple domain based on datetime field (no comparisonOptionId) - UTC+2", + (assert) => { + patchTimeZone(120); + patchDate(2020, 5, 1, 0, 0, 0); + const referenceMoment = DateTime.local(); + assert.deepEqual( + constructDateDomain(referenceMoment, "date_field", "datetime", [ + "this_month", + "this_year", + ]), + { + domain: new Domain( + `["&", ("date_field", ">=", "2020-05-31 22:00:00"), ("date_field", "<=", "2020-06-30 21:59:59")]` + ), + description: "June 2020", + } + ); + assert.deepEqual( + constructDateDomain(referenceMoment, "date_field", "datetime", [ + "second_quarter", + "this_year", + ]), + { + domain: new Domain( + `["&", ("date_field", ">=", "2020-03-31 22:00:00"), ("date_field", "<=", "2020-06-30 21:59:59")]` + ), + description: "Q2 2020", + } + ); + assert.deepEqual( + constructDateDomain(referenceMoment, "date_field", "datetime", ["this_year"]), + { + domain: new Domain( + `["&", ("date_field", ">=", "2019-12-31 22:00:00"), ("date_field", "<=", "2020-12-31 21:59:59")]` + ), + description: "2020", + } + ); + } + ); + QUnit.test("construct domain based on date field (no comparisonOptionId)", function (assert) { patchDate(2020, 0, 1, 12, 0, 0); - const referenceMoment = DateTime.utc(); + const referenceMoment = DateTime.local(); assert.deepEqual( constructDateDomain(referenceMoment, "date_field", "date", [ "this_month", @@ -158,7 +261,7 @@ QUnit.module("Search", () => { "construct domain based on datetime field (no comparisonOptionId)", function (assert) { patchDate(2020, 0, 1, 12, 0, 0); - const referenceMoment = DateTime.utc(); + const referenceMoment = DateTime.local(); assert.deepEqual( constructDateDomain(referenceMoment, "date_field", "datetime", [ "this_month", @@ -217,7 +320,7 @@ QUnit.module("Search", () => { 'construct comparison domain based on date field and option "previous_period"', function (assert) { patchDate(2020, 0, 1, 12, 0, 0); - const referenceMoment = DateTime.utc(); + const referenceMoment = DateTime.local(); assert.deepEqual( constructDateDomain( referenceMoment, @@ -326,7 +429,7 @@ QUnit.module("Search", () => { 'construct comparison domain based on datetime field and option "previous_year"', function (assert) { patchDate(2020, 5, 1, 13, 0, 0); - const referenceMoment = DateTime.utc(); + const referenceMoment = DateTime.local(); assert.deepEqual( constructDateDomain( referenceMoment, @@ -393,7 +496,7 @@ QUnit.module("Search", () => { QUnit.test("Quarter option: custom translation", async function (assert) { patchDate(2020, 5, 1, 13, 0, 0); - const referenceMoment = DateTime.utc().setLocale("en"); + const referenceMoment = DateTime.local().setLocale("en"); patch(translatedTerms, "add_translations", { Q2: "Deuxième trimestre de l'an de grâce" }); assert.deepEqual( constructDateDomain(referenceMoment, "date_field", "date", [ @@ -413,7 +516,7 @@ QUnit.module("Search", () => { QUnit.test("Quarter option: right to left", async function (assert) { patchDate(2020, 5, 1, 13, 0, 0); - const referenceMoment = DateTime.utc().setLocale("en"); + const referenceMoment = DateTime.local().setLocale("en"); patch( localization, "rtl_localization", @@ -437,7 +540,7 @@ QUnit.module("Search", () => { QUnit.test("Quarter option: custom translation and right to left", async function (assert) { patchDate(2020, 5, 1, 13, 0, 0); - const referenceMoment = DateTime.utc().setLocale("en"); + const referenceMoment = DateTime.local().setLocale("en"); patch( localization, "rtl_localization",