我有一个代码:
def set_date_range_filter(self,attribute = None,start_date = None , end_date = None):
if attribute is None:
return
#Make sure set the passing start date and end date
if not start_date or not end_date :
return
if isinstance(start_date, str) :
start_date = datetime.strptime(start_date, "%Y-%m-%d")
if isinstance(start_date, unicode) :
start_date = datetime.strptime(str(start_date), "%Y-%m-%d")
if isinstance(end_date ,str):
end_date = datetime.strptime(end_date, "%Y-%m-%d")
if isinstance(end_date ,unicode):
end_date = datetime.strptime(str(end_date), "%Y-%m-%d")
# Shphnx Range Filter ,start_date and end_date must be integers that define the acceptable attribute values range
start_date = int(time.mktime(start_date.timetuple()))
end_date = int(time.mktime(end_date.timetuple()))
if start_date > end_date :
return
self.sphinx.SetFilterRange(str(attribute),start_date,end_date)
我想更新此代码只接受'start_date'或仅接受'end_date'或两者兼而有之。
就像我想要2014-01-01的所有日期或者我想要2014-01-01之后的所有数据 或者说我想要2014-01-01到2014-09-01的所有数据。我该如何存档?
答案 0 :(得分:1)
而不是
if not start_date or not end_date :
return
替换为
if not start_date:
start_date = '1971-01-01'
if not end_date:
end_date = '2037-01-01'
或类似的。如果缺少其中任何一个,那么只需使用非常早或非常晚的日期(超出数据范围)。上面的示例日期在unix时间戳的范围内选择。
(然后通过strptime将其转换为正确的日期对象)