From 930db42b3acdc88d11bf54322b6a0362d7715ebf Mon Sep 17 00:00:00 2001
From: Nicolas Lempereur <nle@odoo.com>
Date: Fri, 3 Jul 2020 13:40:53 +0000
Subject: [PATCH] [FIX] models.py: group by date with DST change

When we group by date with DST change within a range, we could get a
reocrd inside two date range grouping, or inside no grouping.

This is because we computed range just with [+ 1 month], so we possibly
had these ranges (in UTC):

- October 2019 : [('datetime', '>=', '2019-10-01 02:00:00')
                  ('datetime', '<', '2019-11-01 02:00:00')]

- November 2019 : [('datetime', '>=', '2019-11-01 01:00:00')
                   ('datetime', '<', '2019-12-01 01:00:00')]

So a record on 2019-11-01 01:30:00 would be both inside October and
November.

This happen because the DST is removed on happen on 27 October 2019 and
this was not taken into account when computing the end of the range.

With this changeset, for the given example aboth, we will have:

- October 2019 : [('datetime', '>=', '2019-10-01 02:00:00')
                  ('datetime', '<', '2019-11-01 01:00:00')]

Added test without the change fails with "AssertionError: Lists differ"
because:

- "Q1 2019" finished on 17:00:00 instead of 16:00:00
- "Q3 2019" finished on 16:00:00 instead of 17:00:00

opw-2278829
closes #54056

closes odoo/odoo#54345

Note: maxDiff added for test to work in 13.0
X-original-commit: af5d03de28fa300ebbaa37a3d226b41051ebdf0f
Signed-off-by: Nicolas Lempereur (nle) <nle@odoo.com>
---
 .../tests/test_fill_temporal.py               | 44 +++++++++++++++++++
 odoo/models.py                                |  2 +
 2 files changed, 46 insertions(+)

diff --git a/odoo/addons/test_read_group/tests/test_fill_temporal.py b/odoo/addons/test_read_group/tests/test_fill_temporal.py
index d7f6fb2856e2..89894914070b 100644
--- a/odoo/addons/test_read_group/tests/test_fill_temporal.py
+++ b/odoo/addons/test_read_group/tests/test_fill_temporal.py
@@ -531,6 +531,50 @@ class TestFillTemporal(common.TransactionCase):
 
         self.assertEqual(groups, expected)
 
+    def test_quarter_with_timezones(self):
+        """Test quarter with timezones.
+
+        We group year by quarter and check that it is consistent with timezone.
+        """
+        self.Model.create({'datetime': '2016-01-01 03:30:00', 'value': 2})
+        self.Model.create({'datetime': '2016-12-30 22:30:00', 'value': 3})
+
+        expected = [{
+            '__domain': ['&',
+                ('datetime', '>=', '2015-12-31 17:00:00'),
+                ('datetime', '<', '2016-03-31 16:00:00')],
+            'datetime:quarter': 'Q1 2016',
+            'datetime_count': 1,
+            'value': 2
+        }, {
+            '__domain': ['&',
+                       ('datetime', '>=', '2016-03-31 16:00:00'),
+                       ('datetime', '<', '2016-06-30 16:00:00')],
+            'datetime:quarter': 'Q2 2016',
+            'datetime_count': 0,
+            'value': False
+        }, {
+            '__domain': ['&',
+                       ('datetime', '>=', '2016-06-30 16:00:00'),
+                       ('datetime', '<', '2016-09-30 17:00:00')],
+            'datetime:quarter': 'Q3 2016',
+            'datetime_count': 0,
+            'value': False
+        }, {
+            '__domain': ['&',
+                       ('datetime', '>=', '2016-09-30 17:00:00'),
+                       ('datetime', '<', '2016-12-31 17:00:00')],
+            'datetime:quarter': 'Q4 2016',
+            'datetime_count': 1,
+            'value': 3
+        }]
+
+        model_fill = self.Model.with_context(tz='Asia/Hovd', fill_temporal=True)
+        groups = model_fill.read_group([], fields=['datetime', 'value'],
+                                       groupby=['datetime:quarter'])
+
+        self.assertEqual(groups, expected)
+
     def test_egde_fx_tz(self):
         """We test if different edge effect by using a different timezone from the user context
 
diff --git a/odoo/models.py b/odoo/models.py
index 99a45c0de00d..4f47b1696722 100644
--- a/odoo/models.py
+++ b/odoo/models.py
@@ -2104,6 +2104,8 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})):
                     if gb['tz_convert']:
                         tzinfo = range_start.tzinfo
                         range_start = range_start.astimezone(pytz.utc)
+                        # take into account possible hour change between start and end
+                        range_end = tzinfo.localize(range_end.replace(tzinfo=None))
                         range_end = range_end.astimezone(pytz.utc)
 
                     range_start = range_start.strftime(fmt)
-- 
GitLab