From fbe3e1a45ebf3cefd00a5c92b53f76bf60e4d5d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Inge=20Lea=20Bj=C3=B8rnsen?= Date: Tue, 18 Feb 2014 22:13:14 +0100 Subject: [PATCH 1/4] Time zone fix for epoch time calculation for SQL based date queries (#542). --- beets/dbcore/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 318a7fbe2..46516972f 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -330,7 +330,7 @@ class FalseQuery(Query): def _to_epoch_time(date): - epoch = datetime.utcfromtimestamp(0) + epoch = datetime.fromtimestamp(0) return int((date - epoch).total_seconds()) From 64fdd174b31c643b9dcccb9e9d667968c001915b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Inge=20Lea=20Bj=C3=B8rnsen?= Date: Tue, 18 Feb 2014 23:56:26 +0100 Subject: [PATCH 2/4] Time zone fix for SQL date clauses (#542). Query using the SQL function datetime instead of date. --- beets/dbcore/query.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 46516972f..e19a52e1b 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -454,7 +454,7 @@ class DateQuery(FieldQuery): date = datetime.utcfromtimestamp(timestamp) return self.interval.contains(date) - _clause_tmpl = "date({0}, 'unixepoch') {1} date(?, 'unixepoch')" + _clause_tmpl = "datetime({0}, 'unixepoch') {1} datetime(?, 'unixepoch')" def col_clause(self): clause_parts = [] @@ -474,5 +474,4 @@ class DateQuery(FieldQuery): else: # Match any date. clause = '1' - return clause, subvals From cf0a10b652fce346d64b0359dc90dabe8cda0509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Inge=20Lea=20Bj=C3=B8rnsen?= Date: Wed, 19 Feb 2014 00:30:29 +0100 Subject: [PATCH 3/4] Python 2.6 compatibility for date queries. The method datetime.timedelta.total_seconds() is only available since 2.7. --- beets/dbcore/query.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index e19a52e1b..4fefb6637 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -331,7 +331,14 @@ class FalseQuery(Query): def _to_epoch_time(date): epoch = datetime.fromtimestamp(0) - return int((date - epoch).total_seconds()) + delta = date - epoch + try: + return int((delta).total_seconds()) + except AttributeError: + # datetime.timedelta.total_seconds() is not available on Python 2.6 + return int((delta.microseconds + + (delta.seconds + delta.days * 24 * 3600) * 10 ** 6) + / 10.0 ** 6) def _parse_periods(pattern): From d65f2b8095eb65d5b2ce0c39bbe191baf8bfde49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stig=20Inge=20Lea=20Bj=C3=B8rnsen?= Date: Wed, 19 Feb 2014 20:50:13 +0100 Subject: [PATCH 4/4] Query date intervals by their numeric epoch time. SQLite store dates as epoch time offsets in UTC. By converting date interval endpoints to UTC epoch time in Python, using the SQLite date functions isn't necessary any more. --- beets/dbcore/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 4fefb6637..be4172692 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -461,7 +461,7 @@ class DateQuery(FieldQuery): date = datetime.utcfromtimestamp(timestamp) return self.interval.contains(date) - _clause_tmpl = "datetime({0}, 'unixepoch') {1} datetime(?, 'unixepoch')" + _clause_tmpl = "{0} {1} ?" def col_clause(self): clause_parts = []