def dateutil_parse(timestr, default, ignoretz=False, tzinfos=None, **kwargs): """ lifted from dateutil to get resolution""" from dateutil import tz import time fobj = StringIO(str(timestr)) res = DEFAULTPARSER._parse(fobj, **kwargs) # dateutil 2.2 compat if isinstance(res, tuple): res, _ = res if res is None: raise ValueError("unknown string format") repl = {} reso = None for attr in [ "year", "month", "day", "hour", "minute", "second", "microsecond" ]: value = getattr(res, attr) if value is not None: repl[attr] = value reso = attr if reso is None: raise ValueError("Cannot parse date.") if reso == 'microsecond': if repl['microsecond'] == 0: reso = 'second' elif repl['microsecond'] % 1000 == 0: reso = 'millisecond' 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, compat.string_types): 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, reso
def _clean_datetime_to_parts(self, value): value = DEFAULTPARSER._parse(value, dayfirst='-' not in value) value = value[0] if isinstance(value, tuple) else value if value is None: raise FilterValueError(ugettext('Value cannot be parsed to partial datetime')) else: return value
def dateutil_parse(timestr, default, ignoretz=False, tzinfos=None, **kwargs): """ lifted from dateutil to get resolution""" from dateutil import tz import time fobj = StringIO(str(timestr)) res = DEFAULTPARSER._parse(fobj, **kwargs) # dateutil 2.2 compat if isinstance(res, tuple): res, _ = res if res is None: raise ValueError("unknown string format") repl = {} reso = None for attr in ["year", "month", "day", "hour", "minute", "second", "microsecond"]: value = getattr(res, attr) if value is not None: repl[attr] = value reso = attr if reso is None: raise ValueError("Cannot parse date.") if reso == "microsecond": if repl["microsecond"] == 0: reso = "second" elif repl["microsecond"] % 1000 == 0: reso = "millisecond" 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, compat.string_types): 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, reso
def _clean_datetime_to_parts(self, value): value = DEFAULTPARSER._parse(value, dayfirst='-' not in value) value = value[0] if isinstance(value, tuple) else value if value is None: raise FilterValueError( ugettext('Value cannot be parsed to partial datetime')) else: return value
def dateutil_parse(timestr, default, ignoretz=False, tzinfos=None, **kwargs): """ lifted from dateutil to get resolution""" from dateutil import tz import time res = DEFAULTPARSER._parse(StringIO(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 reso = attr if reso == 'microsecond' and repl['microsecond'] == 0: reso = 'second' 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, reso
def dateutil_parse(timestr, default, ignoretz=False, tzinfos=None, **kwargs): """ lifted from dateutil to get resolution""" res = DEFAULTPARSER._parse(StringIO(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 reso = attr if reso == 'microsecond' and repl['microsecond'] == 0: reso = 'second' 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, reso
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