也许是这样的:
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