Fix failing test comparing date searching with old and new dbs

Ensure date comparison is done in the same timezone. This change only
affects legacy code path that is not actually used anywhere other than
for tests.
This commit is contained in:
Kovid Goyal 2025-07-18 11:22:25 +05:30
parent ff1df09c4a
commit 813db96cd3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 14 additions and 3 deletions

View File

@ -293,8 +293,7 @@ class ReadingTest(BaseTest):
old = LibraryDatabase2(self.library_path)
oldvals = {query:set(old.search_getting_ids(query, '')) for query in (
# Date tests
# 'date:9/6/2011',
'date:true', 'date:false', 'pubdate:1/9/2011',
'date:9/6/2011', 'date:true', 'date:false', 'pubdate:1/9/2011',
'#date:true', 'date:<100_daysago', 'date:>9/6/2011',
'#date:>9/1/2011', '#date:=2011',

View File

@ -293,6 +293,8 @@ class ResultCache(SearchQueryParser): # {{{
will almost never be correct.
'''
def relop_eq(db, query, field_count):
if db.tzinfo != query.tzinfo:
db = db.astimezone(tz=query.tzinfo)
if db.year == query.year:
if field_count == 1:
return True
@ -303,6 +305,8 @@ class ResultCache(SearchQueryParser): # {{{
return False
def relop_gt(db, query, field_count):
if db.tzinfo != query.tzinfo:
db = db.astimezone(tz=query.tzinfo)
if db.year > query.year:
return True
if field_count > 1 and db.year == query.year:
@ -312,6 +316,8 @@ class ResultCache(SearchQueryParser): # {{{
return False
def relop_lt(db, query, field_count):
if db.tzinfo != query.tzinfo:
db = db.astimezone(tz=query.tzinfo)
if db.year < query.year:
return True
if field_count > 1 and db.year == query.year:
@ -321,12 +327,18 @@ class ResultCache(SearchQueryParser): # {{{
return False
def relop_ne(db, query, field_count):
if db.tzinfo != query.tzinfo:
db = db.astimezone(tz=query.tzinfo)
return not relop_eq(db, query, field_count)
def relop_ge(db, query, field_count):
if db.tzinfo != query.tzinfo:
db = db.astimezone(tz=query.tzinfo)
return not relop_lt(db, query, field_count)
def relop_le(db, query, field_count):
if db.tzinfo != query.tzinfo:
db = db.astimezone(tz=query.tzinfo)
return not relop_gt(db, query, field_count)
self.date_search_relops = {
@ -417,7 +429,7 @@ class ResultCache(SearchQueryParser): # {{{
continue
v = item[loc]
if isinstance(v, (bytes, str)):
v = parse_date(v)
v = parse_date(v, as_utc=False)
if relop(v, qd, field_count):
matches.add(item[0])
return matches