def parse_date_string(date_str, dayfirst=True, yearfirst=True): """ Try hard to parse datetime string, leveraging dateutil plus some extras Parameters ---------- arg : date string dayfirst : bool, yearfirst : bool Returns ------- datetime, datetime format string (for `strftime` or `strptime`) or None if unable parse date str """ if not isinstance(date_str, basestring): return None arg = date_str.upper() parse_info = DEFAULTPARSER.info if len(arg) in (7, 8): mresult = _attempt_monthly(arg) log.debug('mresult={0}'.format(mresult)) if mresult: return mresult parsed_datetime = DEFAULTPARSER.parse(StringIO(str(arg)), dayfirst=dayfirst, yearfirst=yearfirst, fuzzy=True) log.debug('parsed_datetime={0}'.format(parsed_datetime)) if parsed_datetime: date_format = _guess_datetime_format(date_str, parsed_datetime, dayfirst=dayfirst) return parsed_datetime, date_format
def parse_date_string(date_str, dayfirst=False, yearfirst=True): """ Try hard to parse datetime string, leveraging dateutil plus some extras Parameters ---------- arg : date string dayfirst : bool, yearfirst : bool Returns ------- datetime, datetime format string (for `strftime` or `strptime`) or None if unable parse date str """ if not isinstance(date_str, basestring): return None arg = date_str.upper() parse_info = DEFAULTPARSER.info if len(arg) in (7, 8): mresult = _attempt_monthly(arg) log.debug('mresult={0}'.format(mresult)) if mresult: return mresult parsed_datetime = DEFAULTPARSER.parse(StringIO(str(arg)), dayfirst=dayfirst, yearfirst=yearfirst, fuzzy=True) log.debug('parsed_datetime={0}'.format(parsed_datetime)) if parsed_datetime: date_format = _guess_datetime_format(date_str, parsed_datetime, dayfirst=dayfirst) return parsed_datetime, date_format
def _clean_datetime(self, value): try: datetime_value = DEFAULTPARSER.parse(value, dayfirst='-' not in value) return make_aware( datetime_value ) if datetime_value.tzinfo is None else datetime_value except ValueError: raise FilterValueError( ugettext('Value must be in format ISO 8601.'))
def get_filter_term(self, request): splitted_filter_key = self.filter_key.split('__') if len(splitted_filter_key) == 2 and splitted_filter_key[1] in self.comparators: if splitted_filter_key[1] in self.comparators: value = DEFAULTPARSER.parse(self.value, dayfirst=True) return {self.filter_key: value} else: return super(DateFilter, self).get_filter_term(request) parse = DEFAULTPARSER._parse(self.value, dayfirst=True) if parse is None: raise ValueError() res, _ = parse filter_terms = {} for attr in self.extra_suffixes: value = getattr(res, attr) if value: filter_terms['__'.join((self.filter_key, attr))] = value return filter_terms
def get_filter_term(self, request): suffix = self._check_suffix() if suffix in self.comparators: value = DEFAULTPARSER.parse(self.value, dayfirst=True) value = make_aware(value, get_current_timezone()) return {self.full_filter_key: value} elif suffix: return super(DateFilter, self).get_filter_term(request) else: parse = DEFAULTPARSER._parse(self.value, dayfirst=True) if parse is None: raise ValueError() res = parse[0] if isinstance(parse, tuple) else parse filter_term = {} for attr in self.extra_suffixes: value = getattr(res, attr) if value: filter_term['__'.join((self.full_filter_key, attr))] = value return filter_term
def get_filter_term(self, request): splitted_filter_key = self.filter_key.split('__') if len(splitted_filter_key ) == 2 and splitted_filter_key[1] in self.comparators: if splitted_filter_key[1] in self.comparators: value = DEFAULTPARSER.parse(self.value, dayfirst=True) return {self.filter_key: value} else: return super(DateFilter, self).get_filter_term(request) parse = DEFAULTPARSER._parse(self.value, dayfirst=True) if parse is None: raise ValueError() res, _ = parse filter_terms = {} for attr in self.extra_suffixes: value = getattr(res, attr) if value: filter_terms['__'.join((self.filter_key, attr))] = value return filter_terms
def _clean_datetime(self, value): try: datetime_value = DEFAULTPARSER.parse(value, dayfirst='-' not in value) return make_aware(datetime_value) if datetime_value.tzinfo is None else datetime_value except ValueError: raise FilterValueError(ugettext('Value must be in format ISO 8601.'))
def to_python(self, value): return dateparser.parse(value)