From c0bbe2264229b83836e0d2f236e8d3f3667b9198 Mon Sep 17 00:00:00 2001 From: Ivan Yelizariev Date: Thu, 18 Aug 2022 09:29:25 +0000 Subject: [PATCH] [FIX] core: apply user's tz on formatting Datetime field as Date STEPS in v14: 1. Set user's timezone to Asia/Qatar 2. Inventory > Config > Setting > Enable Lots & Serial Numbers, Expiration Dates, Display Lots & Serial Numbers on Delivery Slips, Display Expiration Dates on Delivery Slips 3. Create a product (storable, tracking by lots, expiration date) 4. Manually update the on-hand quantity for the product 5. Create a lot for the product created in Step 3 > Set the expiration date to 3/30/2025 00:00:00 6. Create an SO with the product > Confirm it to Delivery 7. Validate the Delivery > Print the Delivery Slip Before this commit the expiration date is rendered as 03/29/2025 instead of 03/30/2025 opw-2901367 closes odoo/odoo#98371 Signed-off-by: Julien Castiaux --- odoo/addons/base/tests/test_misc.py | 12 ++++++++++++ odoo/tools/misc.py | 3 +++ 2 files changed, 15 insertions(+) diff --git a/odoo/addons/base/tests/test_misc.py b/odoo/addons/base/tests/test_misc.py index 9b1c8b4d065..e7ddd91eedd 100644 --- a/odoo/addons/base/tests/test_misc.py +++ b/odoo/addons/base/tests/test_misc.py @@ -276,6 +276,18 @@ class TestFormatLangDate(TransactionCase): self.assertEqual(misc.format_time(lang.with_context(lang='fr_FR').env, time_part, time_format='short', lang_code='zh_CN'), '\u4e0b\u53484:30') self.assertEqual(misc.format_time(lang.with_context(lang='zh_CN').env, time_part, time_format='medium', lang_code='fr_FR'), '16:30:22') + def test_02_tz(self): + self.env.user.tz = 'Europe/Brussels' + datetime_str = '2016-12-31 23:55:00' + date_datetime = datetime.datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S") + + # While London is still in 2016, Brussels is already in 2017 + self.assertEqual(misc.format_date(self.env, date_datetime), '01/01/2017') + + # Force London timezone + date_datetime = date_datetime.replace(tzinfo=pytz.UTC) + self.assertEqual(misc.format_date(self.env, date_datetime), '12/31/2016', "User's tz must be ignored when tz is specifed in datetime object") + class TestCallbacks(BaseCase): def test_callback(self): diff --git a/odoo/tools/misc.py b/odoo/tools/misc.py index 4047ee88547..1d1cd53cb67 100644 --- a/odoo/tools/misc.py +++ b/odoo/tools/misc.py @@ -1374,6 +1374,9 @@ def format_date(env, value, lang_code=False, date_format=False): value = odoo.fields.Datetime.context_timestamp(env['res.lang'], value) else: value = odoo.fields.Datetime.from_string(value) + elif isinstance(value, datetime.datetime) and not value.tzinfo: + # a datetime, convert to correct timezone + value = odoo.fields.Datetime.context_timestamp(env['res.lang'], value) lang = get_lang(env, lang_code) locale = babel_locale_parse(lang.code)