Skip to content

Instantly share code, notes, and snippets.

# start_id - the first id in the range we're trying to search
# end_id - the last id in the range we're trying to search. This _must_ be higher than start_id. If not provided, we use model.last.id
# tolerance - the amount of time (in seconds) we're willing to tolerate to say that an id is "close enough"
def find_closest(model, date, start_id: 0, end_id: nil, tolerance: 60)
end_id ||= model.last.id
diff = nil
pointer = 0
# General algorithm:
# 1. find a number in the middle between start_id and end_id, this is your pointer