blob: e0c8a84289220266c37d6ae14da67d716d4b276f [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001#-*- coding: ISO-8859-1 -*-
2# pysqlite2/dbapi2.py: the DB-API 2.0 interface
3#
4# Copyright (C) 2004-2005 Gerhard Häring <gh@ghaering.de>
5#
6# This file is part of pysqlite.
7#
8# This software is provided 'as-is', without any express or implied
9# warranty. In no event will the authors be held liable for any damages
10# arising from the use of this software.
11#
12# Permission is granted to anyone to use this software for any purpose,
13# including commercial applications, and to alter it and redistribute it
14# freely, subject to the following restrictions:
15#
16# 1. The origin of this software must not be misrepresented; you must not
17# claim that you wrote the original software. If you use this software
18# in a product, an acknowledgment in the product documentation would be
19# appreciated but is not required.
20# 2. Altered source versions must be plainly marked as such, and must not be
21# misrepresented as being the original software.
22# 3. This notice may not be removed or altered from any source distribution.
23
24import datetime
25
26paramstyle = "qmark"
27
28threadsafety = 1
29
30apilevel = "2.0"
31
32from _sqlite3 import *
33
34import datetime, time
35
36Date = datetime.date
37
38Time = datetime.time
39
40Timestamp = datetime.datetime
41
42def DateFromTicks(ticks):
43 return apply(Date,time.localtime(ticks)[:3])
44
45def TimeFromTicks(ticks):
46 return apply(Time,time.localtime(ticks)[3:6])
47
48def TimestampFromTicks(ticks):
49 return apply(Timestamp,time.localtime(ticks)[:6])
50
51_major, _minor, _micro = version.split(".")
52version_info = (int(_major), int(_minor), _micro)
53_major, _minor, _micro = sqlite_version.split(".")
54sqlite_version_info = (int(_major), int(_minor), _micro)
55
56Binary = buffer
57
58def adapt_date(val):
59 return val.isoformat()
60
61def adapt_datetime(val):
62 return val.isoformat(" ")
63
64def convert_date(val):
65 return datetime.date(*map(int, val.split("-")))
66
67def convert_timestamp(val):
68 datepart, timepart = val.split(" ")
69 year, month, day = map(int, datepart.split("-"))
70 timepart_full = timepart.split(".")
71 hours, minutes, seconds = map(int, timepart_full[0].split(":"))
72 if len(timepart_full) == 2:
73 microseconds = int(float("0." + timepart_full[1]) * 1000000)
74 else:
75 microseconds = 0
76
77 val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds)
78 return val
79
80
81register_adapter(datetime.date, adapt_date)
82register_adapter(datetime.datetime, adapt_datetime)
83register_converter("date", convert_date)
84register_converter("timestamp", convert_timestamp)