2014-10-06 102 views
0

我有一個代碼:獅身人面像搜索如何搜索上面的某個日期

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) 

我要更新這個代碼只接受「起始日期」或只「END_DATE」或兩者兼而有之。

喜歡我想要的所有日期從2014-01-01或我想要的所有數據2014-01-01 之後或說我想要從2014-01-01到2014-09-01的所有數據。我如何存檔這?

回答

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變成適當的日期對象)