Exemple #1
0
def get_formats():
    """Return all formats strings required for i18n to work."""
    FORMAT_SETTINGS = ('DATE_FORMAT', 'DATETIME_FORMAT', 'TIME_FORMAT',
                       'YEAR_MONTH_FORMAT', 'MONTH_DAY_FORMAT',
                       'SHORT_DATE_FORMAT', 'SHORT_DATETIME_FORMAT',
                       'FIRST_DAY_OF_WEEK', 'DECIMAL_SEPARATOR',
                       'THOUSAND_SEPARATOR', 'NUMBER_GROUPING',
                       'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS',
                       'DATETIME_INPUT_FORMATS')
    return {attr: get_format(attr) for attr in FORMAT_SETTINGS}
Exemple #2
0
 def _parse_date_fmt():
     fmt = get_format('DATE_FORMAT')
     escaped = False
     for char in fmt:
         if escaped:
             escaped = False
         elif char == '\\':
             escaped = True
         elif char in 'Yy':
             yield 'year'
         elif char in 'bEFMmNn':
             yield 'month'
         elif char in 'dj':
             yield 'day'
Exemple #3
0
 def value_from_datadict(self, data, files, name):
     y = data.get(self.year_field % name)
     m = data.get(self.month_field % name)
     d = data.get(self.day_field % name)
     if y == m == d == '':
         return None
     if y is not None and m is not None and d is not None:
         if settings.USE_L10N:
             input_format = get_format('DATE_INPUT_FORMATS')[0]
             try:
                 date_value = datetime.date(int(y), int(m), int(d))
             except ValueError:
                 pass
             else:
                 date_value = datetime_safe.new_date(date_value)
                 return date_value.strftime(input_format)
         # Return pseudo-ISO dates with zeros for any unselected values,
         # e.g. '2017-0-23'.
         return '%s-%s-%s' % (y or 0, m or 0, d or 0)
     return data.get(name)
Exemple #4
0
 def format_value(self, value):
     """
     Return a dict containing the year, month, and day of the current value.
     Use dict instead of a datetime to allow invalid dates such as February
     31 to display correctly.
     """
     year, month, day = None, None, None
     if isinstance(value, (datetime.date, datetime.datetime)):
         year, month, day = value.year, value.month, value.day
     elif isinstance(value, str):
         match = self.date_re.match(value)
         if match:
             # Convert any zeros in the date to empty strings to match the
             # empty option value.
             year, month, day = [int(val) or '' for val in match.groups()]
         elif settings.USE_L10N:
             input_format = get_format('DATE_INPUT_FORMATS')[0]
             try:
                 d = datetime.datetime.strptime(value, input_format)
             except ValueError:
                 pass
             else:
                 year, month, day = d.year, d.month, d.day
     return {'year': year, 'month': month, 'day': day}
Exemple #5
0
 def format_value(self, value):
     return formats.localize_input(value, self.format or formats.get_format(self.format_key)[0])