Exemplo n.º 1
0
    def parse(cls, datestring, settings):
        if not no_space_parser_eligibile(datestring):
            return

        datestring = datestring.replace(':', '')
        if not datestring:
            return
        tokens = tokenizer(datestring)
        if settings.DATE_ORDER:
            order = resolve_date_order(settings.DATE_ORDER)
            list_order = resolve_date_order(settings.DATE_ORDER, lst=True)
        else:
            order = cls._default_order
            list_order = cls._default_list_order
        nsp = cls()
        ambiguous_date = None
        for token, _ in tokens.tokenize():
            for fmt in nsp.date_formats[order]:
                try:
                    dt = strptime(token, fmt), cls._get_period(fmt), list_order
                    if len(str(dt[0].year)) < 4:
                        ambiguous_date = dt
                        continue
                    return dt
                except:
                    pass
        else:
            if ambiguous_date:
                return ambiguous_date
            else:
                raise ValueError('Unable to parse date from: %s' % datestring)
Exemplo n.º 2
0
 def __call__(self, timestring):
     _timestring = timestring
     for directive in self.time_directives:
         try:
             return strptime(timestring.strip(), directive).time()
         except ValueError:
             pass
     else:
         raise ValueError('%s does not seem to be a valid time string' % _timestring)
Exemplo n.º 3
0
 def _find_best_matching_date(cls, datestring):
     for fmt in cls._preferred_formats_ordered_8_digit:
         try:
             dt = strptime(datestring, fmt), cls._get_period(fmt)
             if len(str(dt[0].year)) == 4:
                 return dt
         except:
             pass
     return None
Exemplo n.º 4
0
    def parse(cls, datestring, settings):
        if not no_space_parser_eligibile(datestring):
            raise ValueError('Unable to parse date from: %s' % datestring)

        datestring = datestring.replace(':', '')
        if not datestring:
            raise ValueError("Empty string")
        tokens = tokenizer(datestring)
        if settings.DATE_ORDER:
            order = resolve_date_order(settings.DATE_ORDER)
        else:
            order = cls._default_order
            if EIGHT_DIGIT.match(datestring):
                dt = cls._find_best_matching_date(datestring)
                if dt is not None:
                    return dt
        nsp = cls()
        ambiguous_date = None
        for token, _ in tokens.tokenize():
            for fmt in nsp.date_formats[order]:
                try:
                    dt = strptime(token, fmt), cls._get_period(fmt)
                    if len(str(dt[0].year)) < 4:
                        ambiguous_date = dt
                        continue

                    missing = _get_missing_parts(fmt)
                    _check_strict_parsing(missing, settings)
                    return dt
                except:
                    pass
        else:
            if ambiguous_date:
                return ambiguous_date
            else:
                raise ValueError('Unable to parse date from: %s' % datestring)
Exemplo n.º 5
0
 def _get_date_obj(self, token, directive):
     return strptime(token, directive)
Exemplo n.º 6
0
 def when_date_string_is_parsed(self, date_string, fmt):
     try:
         self.result = strptime(date_string, fmt)
     except ValueError as e:
         self.result = e
Exemplo n.º 7
0
 def when_date_string_is_parsed(self, date_string, fmt):
     try:
         self.result = strptime(date_string, fmt)
     except ValueError as e:
         self.result = e