[FIX] read_group: date format: use natural year along with month names, do not mix with ISO week-year

The ISO week-year notation can produce confusing values
when the first week of the year is so short that it
becomes week 0 and is considered the last week of the
previous year, depending on the locale.

For instance, using ISO notation:
  'W53 2015' == dates.format_date(
      date(2015,1,1), format="'W'w YYYY", locale='en_GB')
  'W53 2005' == dates.format_date(
      date(2006,1,1), format="'W'w YYYY", locale='de_DE')

This is surprising but actually valid.

However it definitely yields wrong output when combined with
months formats:
  'January 2014' == dates.format_date(
      date(2015,1,1), format="MMMM YYYY", locale='en_GB')

As a result we must always use `y` to denote the year in
any date format, *except* when it is combined with the
week number `w`, in which case we must use `Y`.

See the documentation at:
   http://babel.pocoo.org/docs/dates/#date-fields
This commit is contained in:
Olivier Dony 2014-10-13 18:42:23 +02:00
parent c8e14f301f
commit c0644a5474
1 changed files with 14 additions and 5 deletions

View File

@ -1975,11 +1975,20 @@ class BaseModel(object):
qualified_field = self._inherits_join_calc(split[0], query)
if temporal:
display_formats = {
'day': 'dd MMM YYYY',
'week': "'W'w YYYY",
'month': 'MMMM YYYY',
'quarter': 'QQQ YYYY',
'year': 'YYYY'
# Careful with week/year formats:
# - yyyy (lower) must always be used, *except* for week+year formats
# - YYYY (upper) must always be used for week+year format
# e.g. 2006-01-01 is W52 2005 in some locales (de_DE),
# and W1 2006 for others
#
# Mixing both formats, e.g. 'MMM YYYY' would yield wrong results,
# such as 2006-01-01 being formatted as "January 2005" in some locales.
# Cfr: http://babel.pocoo.org/docs/dates/#date-fields
'day': 'dd MMM yyyy', # yyyy = normal year
'week': "'W'w YYYY", # w YYYY = ISO week-year
'month': 'MMMM yyyy',
'quarter': 'QQQ yyyy',
'year': 'yyyy',
}
time_intervals = {
'day': dateutil.relativedelta.relativedelta(days=1),