From 2e490f1c95946014222ccc1e9fc2910c301a6fc8 Mon Sep 17 00:00:00 2001 From: Joren Van Onder Date: Thu, 9 Jan 2020 01:04:54 +0000 Subject: [PATCH] [FIX] hr_holidays: default date should be local to the user's timezone Before this the following happened: - Be in PST (UTC -8) - Create a 1 day leave starting Jan 1 (clicking in the calendar view) The default request_date_from is Jan 1, default request_date_to is Jan 2. It happens because the dates are extracted from the UTC time, which in this case are different (15:00 AM Jan 1 - 3:00 AM Jan 2). To solve it set dates local to the user. opw-2169863 X-original-commit: 740f39d31f2480ee7ae9cd3472b733087f1cf280 --- addons/hr_holidays/models/hr_leave.py | 4 ++-- .../hr_holidays/tests/test_leave_requests.py | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/addons/hr_holidays/models/hr_leave.py b/addons/hr_holidays/models/hr_leave.py index 6672ddaa555..fb97d73eb84 100644 --- a/addons/hr_holidays/models/hr_leave.py +++ b/addons/hr_holidays/models/hr_leave.py @@ -92,12 +92,12 @@ class HolidaysRequest(models.Model): user_tz = self.env.user.tz or 'UTC' localized_dt = timezone('UTC').localize(values['date_from']).astimezone(timezone(user_tz)) global_from = localized_dt.time().hour == 7 and localized_dt.time().minute == 0 - new_values['request_date_from'] = values['date_from'].date() + new_values['request_date_from'] = localized_dt.date() if values.get('date_to'): user_tz = self.env.user.tz or 'UTC' localized_dt = timezone('UTC').localize(values['date_to']).astimezone(timezone(user_tz)) global_to = localized_dt.time().hour == 19 and localized_dt.time().minute == 0 - new_values['request_date_to'] = values['date_to'].date() + new_values['request_date_to'] = localized_dt.date() if global_from and global_to: new_values['request_unit_custom'] = True return new_values diff --git a/addons/hr_holidays/tests/test_leave_requests.py b/addons/hr_holidays/tests/test_leave_requests.py index c127bf26298..a0cbcc0c79f 100644 --- a/addons/hr_holidays/tests/test_leave_requests.py +++ b/addons/hr_holidays/tests/test_leave_requests.py @@ -365,3 +365,24 @@ class TestLeaveRequests(TestHrHolidaysCommon): self.assertEqual(leave1.number_of_hours_display, 24) leave1.action_validate() self.assertEqual(leave1.number_of_hours_display, 24) + + @mute_logger('odoo.models.unlink', 'odoo.addons.mail.models.mail_mail') + def test_leave_defaults_with_timezones(self): + """ Make sure that leaves start with correct defaults for non-UTC timezones """ + self.user_employee.tz = 'US/Pacific' + + # Mimic what is done by the calendar widget when clicking on a day. It + # will take the local datetime from 7:00 to 19:00 and then convert it + # to UTC before sending it. Values here are for PST (UTC -8) and + # represent a leave on 2019/1/1 from 7:00 to 19:00 local time. + webclient_values = { + 'date_from': datetime(2019, 1, 1, 15, 0, 0), + 'date_to': datetime(2019, 1, 2, 3, 0), # note that this is the next day in UTC + } + values = self.env['hr.leave'].sudo(self.user_employee_id)._default_get_request_parameters(webclient_values) + + # Dates should be local to the user, they do not and cannot be + # converted using the user's timezone. + first_of_january = date(2019, 1, 1) + self.assertEqual(values['request_date_from'], first_of_january) + self.assertEqual(values['request_date_to'], first_of_january)