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