使用Ruby IMAP在两个日期之间搜索
我是Ruby的新手,我试图在上午9点到11点之间回复电子邮件的数量 比如说使用Ruby IMAP在两个日期之间搜索,ruby,imap,Ruby,Imap,我是Ruby的新手,我试图在上午9点到11点之间回复电子邮件的数量 比如说 @received_today = imap.search(["SINCE", @today.strftime("%d-%b-%Y-%H"):"BEFORE", @today.strftime("%d-%b-%Y-%H")]).count.to_s 我知道这是错误的,但这是我最接近的猜测如何做到这一点。任何帮助都将不胜感激。可能是这样的: require 'date' start_time = Net::IMAP.fo
@received_today = imap.search(["SINCE", @today.strftime("%d-%b-%Y-%H"):"BEFORE", @today.strftime("%d-%b-%Y-%H")]).count.to_s
我知道这是错误的,但这是我最接近的猜测如何做到这一点。任何帮助都将不胜感激。可能是这样的:
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
更新:
试试#2:)
由于imap命令忽略“自”和“之前”条件中的时间部分,因此应该可以:
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小时内的所有电子邮件,然后进行迭代。