我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.utils.formats.get_format()。
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 == "0": return None if y and m and d: 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: return '%s-%s-%s' % (y, m, d) else: date_value = datetime_safe.new_date(date_value) return date_value.strftime(input_format) else: return '%s-%s-%s' % (y, m, d) return data.get(name)
def get_formats(): """ Returns 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' ) result = {} for module in [settings] + get_format_modules(reverse=True): for attr in FORMAT_SETTINGS: result[attr] = get_format(attr) formats = {} for k, v in result.items(): if isinstance(v, (six.string_types, int)): formats[k] = smart_text(v) elif isinstance(v, (tuple, list)): formats[k] = [smart_text(value) for value in v] return formats
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, six.string_types): if settings.USE_L10N: try: input_format = get_format('DATE_INPUT_FORMATS')[0] d = datetime.datetime.strptime(force_str(value), input_format) year, month, day = d.year, d.month, d.day except ValueError: pass match = self.date_re.match(value) if match: year, month, day = [int(val) for val in match.groups()] return {'year': year, 'month': month, 'day': day}
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 == "0": return None if y and m and d: 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: return '%s-%s-%s' % (y, m, d) else: date_value = datetime_safe.new_date(date_value) return date_value.strftime(input_format) else: return '%s-%s-%s' % (y, m, d) return data.get(name, None)
def get_formats(): """ Returns 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' ) result = {} for attr in FORMAT_SETTINGS: result[attr] = get_format(attr) formats = {} for k, v in result.items(): if isinstance(v, (six.string_types, int)): formats[k] = force_text(v) elif isinstance(v, (tuple, list)): formats[k] = [force_text(value) for value in v] return formats
def get_context_data(self, **kwargs): context_data = super(Export, self).get_context_data(**kwargs) context_data['capabilities'] = dsmr_backend.services.get_capabilities() day_statistics = DayStatistics.objects.all().order_by('pk') try: context_data['start_date'] = day_statistics[0].day context_data['end_date'] = day_statistics.order_by('-pk')[0].day except IndexError: context_data['start_date'] = None context_data['end_date'] = None context_data['datepicker_locale_format'] = formats.get_format('DSMR_DATEPICKER_LOCALE_FORMAT') context_data['datepicker_date_format'] = 'DSMR_DATEPICKER_DATE_FORMAT' return context_data
def get_context_data(self, **kwargs): context_data = super(Archive, self).get_context_data(**kwargs) context_data['capabilities'] = dsmr_backend.services.get_capabilities() context_data['frontend_settings'] = FrontendSettings.get_solo() day_statistics = DayStatistics.objects.all().order_by('day') try: context_data['start_date'] = day_statistics[0].day context_data['end_date'] = day_statistics.order_by('-day')[0].day except IndexError: pass context_data['datepicker_locale_format'] = formats.get_format('DSMR_DATEPICKER_LOCALE_FORMAT') context_data['datepicker_date_format'] = 'DSMR_DATEPICKER_DATE_FORMAT' return context_data
def _parse_date_fmt(): fmt = get_format('DATE_FORMAT') escaped = False output = [] for char in fmt: if escaped: escaped = False elif char == '\\': escaped = True elif char in 'Yy': output.append('year') #if not self.first_select: self.first_select = 'year' elif char in 'bEFMmNn': output.append('month') #if not self.first_select: self.first_select = 'month' elif char in 'dj': output.append('day') #if not self.first_select: self.first_select = 'day' return output
def render(self, name, value, attrs=None): if attrs: format = formats.get_format( self.format_key )[self.format_index] attrs.update({ 'class': attrs.get('class', '') + ' flatpickr', 'data-date-format': format.replace('%', '').replace('M', 'i'), }) if hasattr(self, 'additional_attrs'): attrs.update(self.additional_attrs) if value: attrs.update({ 'data-default-date': self._format_value(value) }) input = mark_safe(super().render(name, value, attrs)) return input
def strToDate(string): correct_format = formats.get_format("SHORT_DATE_FORMAT") correct_format = correct_format.split('/') correct_format = ["%" + x for x in correct_format] slash_format = '/'.join(correct_format) hiphen_format = '-'.join(correct_format) try: search_date = datetime.strptime(string, slash_format) search_date = timezone.make_aware(search_date, timezone.get_current_timezone()) except ValueError: try: search_date = datetime.strptime(string, hiphen_format) search_date = timezone.make_aware(search_date, timezone.get_current_timezone()) except ValueError: search_date = datetime.fromtimestamp(0) return search_date
def _format_value(self, value): return formats.localize_input(value, self.format or formats.get_format(self.format_key)[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'
def render(self, name, value, attrs=None): try: year_val, month_val, day_val = value.year, value.month, value.day except AttributeError: year_val = month_val = day_val = None if isinstance(value, six.string_types): if settings.USE_L10N: try: input_format = get_format('DATE_INPUT_FORMATS')[0] v = datetime.datetime.strptime(force_str(value), input_format) year_val, month_val, day_val = v.year, v.month, v.day except ValueError: pass if year_val is None: match = self.date_re.match(value) if match: year_val, month_val, day_val = [int(val) for val in match.groups()] html = {} choices = [(i, i) for i in self.years] html['year'] = self.create_select(name, self.year_field, value, year_val, choices, self.year_none_value) choices = list(self.months.items()) html['month'] = self.create_select(name, self.month_field, value, month_val, choices, self.month_none_value) choices = [(i, i) for i in range(1, 32)] html['day'] = self.create_select(name, self.day_field, value, day_val, choices, self.day_none_value) output = [] for field in self._parse_date_fmt(): output.append(html[field]) return mark_safe('\n'.join(output))
def format_value(self, value): return formats.localize_input(value, self.format or formats.get_format(self.format_key)[0])
def do_dateformat(value, use_format='c'): """Formats a `value` date using `format`. :param value: a datetime object. :param use_format: a format string accepted by :func:`django.utils.formats.get_format` or :func:`django.utils.dateformat.format`. If none is set, the current locale's default format will be used. """ try: use_format = get_format(use_format) except AttributeError: pass return dateformat.format(value, use_format)