|
22 | 22 | # 3. This notice may not be removed or altered from any source distribution. |
23 | 23 |
|
24 | 24 | import datetime |
| 25 | +import time |
| 26 | + |
| 27 | +from _sqlite3 import * |
25 | 28 |
|
26 | 29 | paramstyle = "qmark" |
27 | 30 |
|
28 | 31 | threadsafety = 1 |
29 | 32 |
|
30 | 33 | apilevel = "2.0" |
31 | 34 |
|
32 | | -from _sqlite3 import * |
33 | | - |
34 | | -import datetime, time |
35 | | - |
36 | 35 | Date = datetime.date |
37 | 36 |
|
38 | 37 | Time = datetime.time |
39 | 38 |
|
40 | 39 | Timestamp = datetime.datetime |
41 | 40 |
|
42 | 41 | def DateFromTicks(ticks): |
43 | | - return apply(Date,time.localtime(ticks)[:3]) |
| 42 | + return apply(Date, time.localtime(ticks)[:3]) |
44 | 43 |
|
45 | 44 | def TimeFromTicks(ticks): |
46 | | - return apply(Time,time.localtime(ticks)[3:6]) |
| 45 | + return apply(Time, time.localtime(ticks)[3:6]) |
47 | 46 |
|
48 | 47 | def TimestampFromTicks(ticks): |
49 | | - return apply(Timestamp,time.localtime(ticks)[:6]) |
| 48 | + return apply(Timestamp, time.localtime(ticks)[:6]) |
50 | 49 |
|
51 | | -_major, _minor, _micro = version.split(".") |
52 | | -version_info = (int(_major), int(_minor), _micro) |
53 | | -_major, _minor, _micro = sqlite_version.split(".") |
54 | | -sqlite_version_info = (int(_major), int(_minor), _micro) |
| 50 | +version_info = tuple([int(x) for x in version.split(".")]) |
| 51 | +sqlite_version_info = tuple([int(x) for x in sqlite_version.split(".")]) |
55 | 52 |
|
56 | 53 | Binary = buffer |
57 | 54 |
|
58 | | -def adapt_date(val): |
59 | | - return val.isoformat() |
| 55 | +def register_adapters_and_converters(): |
| 56 | + def adapt_date(val): |
| 57 | + return val.isoformat() |
| 58 | + |
| 59 | + def adapt_datetime(val): |
| 60 | + return val.isoformat(" ") |
| 61 | + |
| 62 | + def convert_date(val): |
| 63 | + return datetime.date(*map(int, val.split("-"))) |
| 64 | + |
| 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: |
| 71 | + microseconds = int(float("0." + timepart_full[1]) * 1000000) |
| 72 | + else: |
| 73 | + microseconds = 0 |
60 | 74 |
|
61 | | -def adapt_datetime(val): |
62 | | - return val.isoformat(" ") |
| 75 | + val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds) |
| 76 | + return val |
63 | 77 |
|
64 | | -def convert_date(val): |
65 | | - return datetime.date(*map(int, val.split("-"))) |
66 | 78 |
|
67 | | -def 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 |
| 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) |
76 | 83 |
|
77 | | - val = datetime.datetime(year, month, day, hours, minutes, seconds, microseconds) |
78 | | - return val |
| 84 | +register_adapters_and_converters() |
79 | 85 |
|
| 86 | +# Clean up namespace |
80 | 87 |
|
81 | | -register_adapter(datetime.date, adapt_date) |
82 | | -register_adapter(datetime.datetime, adapt_datetime) |
83 | | -register_converter("date", convert_date) |
84 | | -register_converter("timestamp", convert_timestamp) |
| 88 | +del(register_adapters_and_converters) |
0 commit comments