Reputation: 171
I am new to Ruby, I'm trying to return the number of emails between 9am and 11am.
For example.
@received_today = imap.search(["SINCE", @today.strftime("%d-%b-%Y-%H"):"BEFORE", @today.strftime("%d-%b-%Y-%H")]).count.to_s
I know this is wrong, but it's my closest guess on how to do this. Any help would be highly appreciated.
Upvotes: 1
Views: 1992
Reputation: 212
Maybe something like this:
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: Try #2 :)
Since imap SEARCH command ignores the time part in SINCE and BEFORE conditions this should work:
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
}
Upvotes: 2