Skip to content

Commit

Permalink
[FIX] core: use of legacy timezone in groupby test
Browse files Browse the repository at this point in the history
Legacy timezones have been moved into an ancillary package in Ubuntu
Noble, and are thus not recognized anymore. Avoid their use in tz
contexts.

Also add a warning when grouping with a legacy timezone in the
context.

Part-of: odoo#170995
Signed-off-by: Xavier Morel (xmo) <[email protected]>
  • Loading branch information
xmo-odoo committed Jun 27, 2024
1 parent c05d7b2 commit f794f85
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions odoo/addons/test_read_group/tests/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def setUpClass(cls):
def test_usecase_with_timezones(self):
# Monday, it is the 5th week in UTC and the 6th in NZ
self.Model.create({"value": "98", "datetime": "2023-02-05 23:55:00"})
result = (self.Model.with_context({'tz': 'NZ'}) # GMT+12
result = (self.Model.with_context({'tz': 'Pacific/Auckland'}) # GMT+12
.read_group([],
fields=['datetime', 'value'],
groupby=['datetime:iso_week_number']))
Expand All @@ -382,7 +382,7 @@ def test_usecase_with_timezones(self):
'value': 98,
'__domain': [('datetime.iso_week_number', '=', 6)]
}])
result = self.Model.with_context({'tz': 'NZ'}).search(result[0]['__domain'])
result = self.Model.with_context({'tz': 'Pacific/Auckland'}).search(result[0]['__domain'])
self.assertEqual(len(result), 1)
self.assertEqual(result.value, 98)

Expand Down
7 changes: 5 additions & 2 deletions odoo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1991,8 +1991,11 @@ def _read_group_groupby(self, groupby_spec: str, query: Query) -> SQL:
else:
sql_expr = self._field_to_sql(self._table, fname, query)

if field.type == 'datetime' and self.env.context.get('tz') in pytz.all_timezones_set:
sql_expr = SQL("timezone(%s, timezone('UTC', %s))", self.env.context['tz'], sql_expr)
if field.type == 'datetime' and (tz := self.env.context.get('tz')):
if tz in pytz.all_timezones_set:
sql_expr = SQL("timezone(%s, timezone('UTC', %s))", self.env.context['tz'], sql_expr)
else:
_logger.warning("Grouping in unknown / legacy timezone %r", tz)

if field.type in ('datetime', 'date') or (field.type == 'properties' and granularity):
if not granularity:
Expand Down

0 comments on commit f794f85

Please sign in to comment.