def parse(self, timestr, default=None, ignoretz=False, tzinfos=None, **kwargs): if not default: default = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) 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 callable(tzinfos) or tzinfos and res.tzname in tzinfos: if callable(tzinfos): tzdata = tzinfos(res.tzname, res.tzoffset) else: tzdata = tzinfos.get(res.tzname) if isinstance(tzdata, datetime.tzinfo): tzinfo = tzdata elif isinstance(tzdata, basestring): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, int): 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)) return ret
def parse(self, timestr, default=None, ignoretz=False, tzinfos=None, pastweekdays=False, **kwargs): if not default: default = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) 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: wd = relativedelta.weekdays[res.weekday](pastweekdays and -1 or +1) ret = ret+relativedelta.relativedelta(weekday=wd) if not ignoretz: if callable(tzinfos) or tzinfos and res.tzname in tzinfos: if callable(tzinfos): tzdata = tzinfos(res.tzname, res.tzoffset) else: tzdata = tzinfos.get(res.tzname) if isinstance(tzdata, datetime.tzinfo): tzinfo = tzdata elif isinstance(tzdata, basestring): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, int): 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)) return ret
def parse(self, timestr, default=None, ignoretz=False, tzinfos=None, fuzzyreturn=False, **kwargs): if not default: default = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) 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 callable(tzinfos) or tzinfos and res.tzname in tzinfos: if callable(tzinfos): tzdata = tzinfos(res.tzname, res.tzoffset) else: tzdata = tzinfos.get(res.tzname) if isinstance(tzdata, datetime.tzinfo): tzinfo = tzdata elif isinstance(tzdata, basestring): tzinfo = tz.tzstr(tzdata) elif isinstance(tzdata, int): 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 fuzzyreturn: if not repl.has_key("month"): # return a fuzzy answer: an object representing a whole year return relativedelta.relativedelta(year=ret.year, years=+1) elif not repl.has_key("day"): # return a fuzzy answer: an object representing a whole month return relativedelta.relativedelta(year=ret.year, month=ret.month, months=+1) elif not repl.has_key("hour"): # return a fuzzy answer: an object representing a whole day return relativedelta.relativedelta(year=ret.year, month=ret.month, day=ret.day, days=+1) elif not repl.has_key("minute"): # return a fuzzy answer: an object representing a whole hour return relativedelta.relativedelta(year=ret.year, month=ret.month, day=ret.day, hour=ret.hour, hours=+1) elif not repl.has_key("second"): # return a fuzzy answer: an object representing a whole minute return relativedelta.relativedelta( year=ret.year, month=ret.month, day=ret.day, hour=ret.hour, minute=ret.minute, minutes=+1 ) elif not repl.has_key("microsecond"): # return a fuzzy answer: an object representing a whole second return relativedelta.relativedelta( year=ret.year, month=ret.month, day=ret.day, hour=ret.hour, minute=ret.minute, second=ret.second, seconds=+1, ) return ret