在date util模块中使用parse函数进行python

时间:2016-01-13 00:41:27

标签: python-2.7

我正在尝试使用带有python的date util模块附带的下面的函数来解析表示日期的字符串。我理解除了“self”之外,函数中的所有输入都是什么。任何人都可以帮助我吗?

def parse(self, timestr, default=None, ignoretz=False, tzinfos=None,
      **kwargs):

    default_specified = default is not None

    if not default_specified:
        default = datetime.datetime.now().replace(hour=0, minute=0,
                                                  second=0, microsecond=0)

    if kwargs.get('fuzzy_with_tokens', False):
        res, skipped_tokens = self._parse(timestr, **kwargs)
    else:
        res = self._parse(timestr, **kwargs)

    if res is None:
        raise ValueError("Unknown string format")

    repl = {}
    for attr in ["year", "month", "day", "hour",
                 "minute", "second", "microsecond"]:
        value = getattr(res, attr)
        if value is not None:
            repl[attr] = value

    ret = default.replace(**repl)

    if res.weekday is not None and not res.day:
        ret = ret+relativedelta.relativedelta(weekday=res.weekday)

    if not ignoretz:
        if (isinstance(tzinfos, collections.Callable) or
                tzinfos and res.tzname in tzinfos):

            if isinstance(tzinfos, collections.Callable):
                tzdata = tzinfos(res.tzname, res.tzoffset)
            else:
                tzdata = tzinfos.get(res.tzname)

            if isinstance(tzdata, datetime.tzinfo):
                tzinfo = tzdata
            elif isinstance(tzdata, text_type):
                tzinfo = tz.tzstr(tzdata)
            elif isinstance(tzdata, integer_types):
                tzinfo = tz.tzoffset(res.tzname, tzdata)
            else:
                raise ValueError("Offset must be tzinfo subclass, "
                                 "tz string, or int offset.")
            ret = ret.replace(tzinfo=tzinfo)
        elif res.tzname and res.tzname in time.tzname:
            ret = ret.replace(tzinfo=tz.tzlocal())
        elif res.tzoffset == 0:
            ret = ret.replace(tzinfo=tz.tzutc())
        elif res.tzoffset:
            ret = ret.replace(tzinfo=tz.tzoffset(res.tzname, res.tzoffset))

    if kwargs.get('fuzzy_with_tokens', False):
        return ret, skipped_tokens
    else:
    return ret

1 个答案:

答案 0 :(得分:0)

self是包含解析的对象。当self._parse它调用相同的函数解析时。