From fcfadf1663338d7ded87fc98751c75b03b088182 Mon Sep 17 00:00:00 2001 From: "Levi Siuzdak (sile)" Date: Mon, 15 Apr 2024 10:37:03 +0200 Subject: [PATCH] [FIX] hr_holidays: don't assume default date strings are datetimes Versions -------- - saas-16.3+ Steps ----- 1. Set timezone of User and browser to America/Los_Angeles; 2. go to Time Off; 3. click on calendar to create a new leave. Issue ----- Date defaults to the day before the selected date. Cause ----- Commit 0a0c6917b5e21e829e14ad271de6e2117e4a7126 added a TZ conversion in JS to add default start & end times for leaves. The issue is that it assumes the context values are always datetime strings, therefore always using `deserializeDateTime`, which does a timezone conversion from UTC to local time, which is incorrect when the context values are date strings. For a UTC-7 zone like America/Los_Angeles, it deserializes a date string like '2024-01-01' to '2023-12-12 17:00:00' (7 hours before midnight). It then sets the start hour to 7, and serializes it back to UTC, adding 7 hours, resulting in '2023-12-12 14:00:00'. Instead, Jan 1, 7 AM in America/Los_Angeles should convert to '2024-01-01 14:00:00' UTC. Solution -------- Use `deserializeDate` instead of `deserializeDateTime` when the `default_date_{from,to}` in the context is a date rather than a datetime. This way, '2024-01-01' gets deserialized into '2024-01-01 00:00:00' local time. When this value gets used for the default hours, '2024-01-01 07:00:00' local time will get serialized to '2024-01-01 14:00:00' UTC as expected. opw-3757712 closes odoo/odoo#162203 X-original-commit: 134455167734d101a8ce923043700e5fc8f497bd Signed-off-by: Levi Siuzdak --- .../static/src/views/calendar/calendar_model.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/addons/hr_holidays/static/src/views/calendar/calendar_model.js b/addons/hr_holidays/static/src/views/calendar/calendar_model.js index 17cc0b89a0c..baf496de1df 100644 --- a/addons/hr_holidays/static/src/views/calendar/calendar_model.js +++ b/addons/hr_holidays/static/src/views/calendar/calendar_model.js @@ -1,7 +1,12 @@ /** @odoo-module */ import { CalendarModel } from '@web/views/calendar/calendar_model'; -import { deserializeDateTime, serializeDate, serializeDateTime } from "@web/core/l10n/dates"; +import { + deserializeDate, + deserializeDateTime, + serializeDate, + serializeDateTime, +} from "@web/core/l10n/dates"; export class TimeOffCalendarModel extends CalendarModel { setup(params, services) { @@ -31,14 +36,18 @@ export class TimeOffCalendarModel extends CalendarModel { context["default_employee_id"] = this.employeeId; } + function deserialize(str) { + // "YYYY-MM-DD".length == 10 + return str.length > 10 ? deserializeDateTime(str) : deserializeDate(str); + } if ("default_date_from" in context) { context["default_date_from"] = serializeDateTime( - deserializeDateTime(context["default_date_from"]).set({ hours: 7 }) + deserialize(context["default_date_from"]).set({ hours: 7 }) ); } if ("default_date_to" in context) { context["default_date_to"] = serializeDateTime( - deserializeDateTime(context["default_date_to"]).set({ hours: 19 }) + deserialize(context["default_date_to"]).set({ hours: 19 }) ); } return context;