Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | #-*- 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 | |
| 24 | import datetime |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 25 | import time |
| 26 | |
| 27 | from _sqlite3 import * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 28 | |
| 29 | paramstyle = "qmark" |
| 30 | |
| 31 | threadsafety = 1 |
| 32 | |
| 33 | apilevel = "2.0" |
| 34 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 35 | Date = datetime.date |
| 36 | |
| 37 | Time = datetime.time |
| 38 | |
| 39 | Timestamp = datetime.datetime |
| 40 | |
| 41 | def DateFromTicks(ticks): |
Guido van Rossum | 3b843cc | 2006-08-17 22:37:44 +0000 | [diff] [blame] | 42 | return Date(*time.localtime(ticks)[:3]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 43 | |
| 44 | def TimeFromTicks(ticks): |
Guido van Rossum | 3b843cc | 2006-08-17 22:37:44 +0000 | [diff] [blame] | 45 | return Time(*time.localtime(ticks)[3:6]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 46 | |
| 47 | def TimestampFromTicks(ticks): |
Guido van Rossum | 3b843cc | 2006-08-17 22:37:44 +0000 | [diff] [blame] | 48 | return Timestamp(*time.localtime(ticks)[:6]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 49 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 50 | version_info = tuple([int(x) for x in version.split(".")]) |
| 51 | sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")]) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 52 | |
| 53 | Binary = buffer |
| 54 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 55 | def register_adapters_and_converters(): |
| 56 | def adapt_date(val): |
| 57 | return val.isoformat() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 59 | def adapt_datetime(val): |
| 60 | return val.isoformat(" ") |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 61 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 62 | def convert_date(val): |
| 63 | return datetime.date(*map(int, val.split("-"))) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 64 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 65 | def convert_timestamp(val): |
| 66 | datepart, timepart = val.split(" ") |
| 67 | year, month, day = map(int, datepart.split("-")) |
| 68 | timepart_full = timepart.split(".") |
| 69 | hours, minutes, seconds = map(int, timepart_full[0].split(":")) |
| 70 | if len(timepart_full) == 2: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 71 | microseconds = int(timepart_full[1]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 72 | else: |
| 73 | microseconds = 0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 74 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 75 | val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds) |
| 76 | return val |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 77 | |
| 78 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 79 | register_adapter(datetime.date, adapt_date) |
| 80 | register_adapter(datetime.datetime, adapt_datetime) |
| 81 | register_converter("date", convert_date) |
| 82 | register_converter("timestamp", convert_timestamp) |
| 83 | |
| 84 | register_adapters_and_converters() |
| 85 | |
| 86 | # Clean up namespace |
| 87 | |
| 88 | del(register_adapters_and_converters) |