|
22 | 22 | LOCAL_TIMEZONE = tzlocal() |
23 | 23 |
|
24 | 24 |
|
| 25 | +def register_adapters_and_converters(): |
| 26 | + sqlite3.register_converter( |
| 27 | + 'timestamp', |
| 28 | + lambda d: datetime.fromtimestamp(float(d), LOCAL_TIMEZONE) |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +register_adapters_and_converters() |
| 33 | + |
| 34 | + |
25 | 35 | class cached_property: # noqa |
26 | 36 | '''A read-only @property that is only evaluated once. Only usable on class |
27 | 37 | instances' methods. |
@@ -383,7 +393,10 @@ def __init__(self, path): |
383 | 393 | self.cache_path = str(path) |
384 | 394 | os.makedirs(os.path.dirname(self.cache_path), exist_ok=True) |
385 | 395 |
|
386 | | - self._conn = sqlite3.connect(self.cache_path) |
| 396 | + self._conn = sqlite3.connect( |
| 397 | + str(self.cache_path), |
| 398 | + detect_types=sqlite3.PARSE_DECLTYPES, |
| 399 | + ) |
387 | 400 | self._conn.row_factory = sqlite3.Row |
388 | 401 | self._conn.execute("PRAGMA foreign_keys = ON") |
389 | 402 |
|
@@ -453,13 +466,13 @@ def create_tables(self): |
453 | 466 | "id" INTEGER PRIMARY KEY, |
454 | 467 | "uid" TEXT, |
455 | 468 | "summary" TEXT, |
456 | | - "due" INTEGER, |
457 | | - "start" INTEGER, |
| 469 | + "due" timestamp, |
| 470 | + "start" timestamp, |
458 | 471 | "priority" INTEGER, |
459 | | - "created_at" INTEGER, |
460 | | - "completed_at" INTEGER, |
| 472 | + "created_at" timestamp, |
| 473 | + "completed_at" timestamp, |
461 | 474 | "percent_complete" INTEGER, |
462 | | - "dtstamp" INTEGER, |
| 475 | + "dtstamp" timestamp, |
463 | 476 | "status" TEXT, |
464 | 477 | "description" TEXT, |
465 | 478 | "location" TEXT, |
@@ -758,22 +771,17 @@ def todos( |
758 | 771 | seen_paths.add(path) |
759 | 772 | yield todo |
760 | 773 |
|
761 | | - def _dt_from_db(self, dt): |
762 | | - if dt: |
763 | | - return datetime.fromtimestamp(dt, LOCAL_TIMEZONE) |
764 | | - return None |
765 | | - |
766 | 774 | def _todo_from_db(self, row): |
767 | 775 | todo = Todo() |
768 | 776 | todo.id = row['id'] |
769 | 777 | todo.uid = row['uid'] |
770 | 778 | todo.summary = row['summary'] |
771 | | - todo.due = self._dt_from_db(row['due']) |
772 | | - todo.start = self._dt_from_db(row['start']) |
| 779 | + todo.due = row['due'] |
| 780 | + todo.start = row['start'] |
773 | 781 | todo.priority = row['priority'] |
774 | | - todo.created_at = self._dt_from_db(row['created_at']) |
775 | | - todo.completed_at = self._dt_from_db(row['completed_at']) |
776 | | - todo.dtstamp = self._dt_from_db(row['dtstamp']) |
| 782 | + todo.created_at = row['created_at'] |
| 783 | + todo.completed_at = row['completed_at'] |
| 784 | + todo.dtstamp = row['dtstamp'] |
777 | 785 | todo.percent_complete = row['percent_complete'] |
778 | 786 | todo.status = row['status'] |
779 | 787 | todo.description = row['description'] |
|
0 commit comments