我是Ruby的新手,我正在尝试在上午9点到11点之间返回电子邮件的数量。
例如。
@received_today = imap.search(["SINCE", @today.strftime("%d-%b-%Y-%H"):"BEFORE", @today.strftime("%d-%b-%Y-%H")]).count.to_s
我知道这是错的,但这是我对如何做到这一点的最接近的猜测。任何帮助都将受到高度赞赏。
答案 0 :(得分: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
更新: 尝试#2 :)
由于imap SEARCH命令忽略了SINCE和BEFORE条件中的时间部分,所以这应该有效:
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
}