2012-02-23 79 views
1

我是Ruby的新手,我試圖在早上9點到上午11點之間返回電子郵件的數量。使用Ruby IMAP在兩個日期之間搜索

例如。

@received_today = imap.search(["SINCE", @today.strftime("%d-%b-%Y-%H"):"BEFORE", @today.strftime("%d-%b-%Y-%H")]).count.to_s 

我知道這是錯誤的,但這是我最接近的猜測如何做到這一點。任何幫助將不勝感激。

回答

2

也許是這樣的:

require 'date' 
start_time = Net::IMAP.format_datetime(DateTime.strptime("09:00", "%H:%M")) 
end_time = Net::IMAP.format_datetime(DateTime.strptime("11:00", "%H:%M")) 
@received_today = imap.search(["SINCE", start_time, "BEFORE", end_time ]).count 

UPDATE: 嘗試#2 :)

由於IMAP SEARCH命令忽略自從和前條件的部分時間這應該工作:

require 'date' 

today = Net::IMAP.format_date(Date.today) 

start_time = DateTime.strptime("09:00", "%H:%M") 
end_time = DateTime.strptime("11:00", "%H:%M") 

@received_today = imap.search(["ON", today]) # get sequence nums of todays emails 

# fetch the INTERNALDATE-s and count the ones in the timeframe 
count = imap.fetch(@received_today, "INTERNALDATE").count{ |data| 
    time = DateTime.parse(data.attr["INTERNALDATE"]) 
    time.between? start_time, end_time 
} 
+0

謝謝 - 我會檢查並接受它是否有效。我也在考慮只進行一次搜索,使用%H返回當天1小時的所有電子郵件,然後對其進行迭代。 – krapdagn 2012-02-23 20:24:49