2016-01-13 67 views
-2

我想使用下面的函數來與日期util模塊與python解析表示日期的字符串。我明白函數中的所有輸入都是「自我」的例外。誰能幫我?在python中使用date util模塊中的解析函數

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 

回答

0

self是包含解析的對象。什麼時候做self._parse它調用相同的函數解析。

+0

我明白你的意思了。我實際上想出了我需要做的事情,我只需要進行一些挖掘。謝謝你回到我身邊。 – robrwill

相關問題