def convert_TIMESTAMP(ts): val=ts.value if havemx and isinstance(val, mx.DateTime.DateTimeType): return TimestampFromMx(val) elif isinstance(val, datetime.datetime): return psycopg.TimestampFromTicks(time.mktime(ts.timetuple())) elif isinstance(val, (int, float, long)): return psycopg.TimestampFromTicks(val) elif isinstance(val, (tuple, list)) and len(val)==9: return psycopg.TimestampFromTicks(time.mktime(val)) elif isinstance(val, (str, unicode)): for f in timestamp_formats: try: t=time.strptime(val, f) except ValueError: continue else: return psycopg.TimestampFromTicks(time.mktime(t)) else: raise ValueError, "cannot parse timestamp format: '%s'" % val raise ValueError, val
def test_timestamp_from_ticks(ticks, want): s = psycopg.TimestampFromTicks(ticks) want = dt.datetime.strptime(want, "%Y-%m-%dT%H:%M:%S.%f%z") assert s == want
def TimestampFromMx(x): return psycopg.TimestampFromTicks(x.ticks())
return psycopg.TimestampFromTicks(val) elif isinstance(val, (tuple, list)) and len(val)==9: return psycopg.TimestampFromTicks(time.mktime(val)) elif isinstance(val, (str, unicode)): for f in timestamp_formats: try: t=time.strptime(val, f) except ValueError: continue else: return psycopg.TimestampFromTicks(time.mktime(t)) else: raise ValueError, "cannot parse timestamp format: '%s'" % val raise ValueError, val _converters={datetime.datetime: lambda x: psycopg.TimestampFromTicks(time.mktime(x.timetuple())), datetime.date: lambda x: psycopg.Date(x.year, x.month, x.day), DATE: convert_DATE, TIMESTAMP: convert_TIMESTAMP, BINARY: lambda x: psycopg.Binary(x.value), INTERVAL: lambda x: x.value} if havemx: # add automatic wrapping for mx.DateTime types _converters[mx.DateTime.DateTimeType]=TimestampFromMx _converters[mx.DateTime.DateTimeDeltaType]=lambda x: x.strftime("%d:%H:%M:%S") class PsycopgConverter(BindingConverter): converters=_converters class PsycopgDBI(DBIBase):
# Using types to insert data - Chapter 14 - typeinsert.py # Adjust the connect() call below for your database. import psycopg, time dsn = 'dbname=jgoerzen user=jgoerzen' print "Connecting to %s" % dsn dbh = psycopg.connect(dsn) print "Connection successful." cur = dbh.cursor() cur.execute("""CREATE TABLE ch14types ( mydate DATE, mytimestamp TIMESTAMP, mytime TIME, mystring varchar(30))""") query = """INSERT INTO ch14types VALUES ( %(mydate)s, %(mytimestamp)s, %(mytime)s, %(mystring)s)""" rows = ( \ {'mydate': psycopg.Date(2000, 12, 25), 'mytimestamp': psycopg.Timestamp(2000, 12, 15, 06, 30, 00), 'mytime': psycopg.Time(6, 30, 00), 'mystring': 'Christmas - Wake Up!'}, {'mydate': psycopg.DateFromTicks(time.time()), 'mytime': psycopg.TimeFromTicks(time.time()), 'mytimestamp': psycopg.TimestampFromTicks(time.time()), 'mystring': None}) cur.executemany(query, rows) dbh.commit() dbh.close()
elif isinstance(val, basestring): for f in timestamp_formats: try: t = time.strptime(val, f) except ValueError: continue else: return psycopg.TimestampFromTicks(time.mktime(t)) else: raise ValueError, "cannot parse timestamp format: '%s'" % val raise ValueError, val _converters = { datetime.datetime: lambda x: psycopg.TimestampFromTicks(time.mktime(x.timetuple())), datetime.date: lambda x: psycopg.Date(x.year, x.month, x.day), DATE: convert_DATE, TIMESTAMP: convert_TIMESTAMP, BINARY: lambda x: psycopg.Binary(x.value), INTERVAL: lambda x: x.value } if havemx: # add automatic wrapping for mx.DateTime types _converters[mx.DateTime.DateTimeType] = TimestampFromMx