def parse_time(value, length, cursor): if value is None: return None if isinstance(value, six.text_type): value = unicode_to_bytes(value) try: return datetime.time(*_parse_time_to_args(value, cursor)) except (TypeError, ValueError): raise DataError("bad datetime: '%s'" % value)
def parse_date(value, length, cursor): if value is None: return None if isinstance(value, six.text_type): value = unicode_to_bytes(value) if value == b'infinity': return datetime.date.max elif value == b'-infinity': return datetime.date.min try: return datetime.date(*[int(x) for x in value.split(b'-')]) except (TypeError, ValueError): if value.endswith(b'BC'): raise ValueError('BC dates not supported') raise DataError("bad datetime: '%s'" % bytes_to_unicode(value))
def parse_datetime(value, length, cursor): if value is None: return None if isinstance(value, six.text_type): value = unicode_to_bytes(value) if value == b'infinity': return datetime.datetime.max elif value == b'-infinity': return datetime.datetime.min try: date, time = value.split(b' ') date_args = date.split(b'-') return datetime.datetime( int(date_args[0]), int(date_args[1]), int(date_args[2]), *_parse_time_to_args(time, cursor)) except (TypeError, ValueError): if value.endswith(b'BC'): raise ValueError('BC dates not supported') raise DataError("bad datetime: '%s'" % bytes_to_unicode(value))
def parse_interval(value, length, cursor): """Typecast an interval to a datetime.timedelta instance. For example, the value '2 years 1 mon 3 days 10:01:39.100' is converted to `datetime.timedelta(763, 36099, 100)`. """ if value is None: return None if isinstance(value, six.text_type): value = unicode_to_bytes(value) m = _re_interval.match(value) if not m: raise ValueError("failed to parse interval: '%s'" % value) years, months, days, sign, hours, mins, secs, frac = m.groups() days = int(days) if days is not None else 0 if months is not None: days += int(months) * 30 if years is not None: days += int(years) * 365 if hours is not None: secs = int(hours) * 3600 + int(mins) * 60 + int(secs) if frac is not None: micros = int((frac + (b'0' * (6 - len(frac))))[:6]) else: micros = 0 if sign == b'-': secs = -secs micros = -micros else: secs = micros = 0 return datetime.timedelta(days, secs, micros)
def getquoted(self): return b''.join([b"'", unicode_to_bytes(str(self._uuid)), b"'::uuid"])