time()
accepts a negative bars_back
argument to retrieve the UNIX time up to the 500th bar in the future. You could iterate until the expected number of bars is found:
int counter = 1
while time("", -counter) < futureTime
counter += 1
log.info("The number of bars is: {0}", counter)