Is it possible to get a block by timestamp?

by March Cat   Last Updated March 14, 2019 20:27 PM - source

I use Bitcoin Core API and I can retrieve a block by header hash as parameter using the getblock method. I need to get a block by given timestamp (median time).
I found some solution.

difference_in_blocks = round((last_block_mediantime - given_timestamp) / 600sec)
desired_block = last_block_heigth + 1 - difference_in_blocks

600 seconds - average time of a block mining (I assume this value is a constant)

Is there a specific API method or a better, more accurate way to do this?

Related Questions

What is chainwork?

Updated December 29, 2017 03:27 AM

The role of timestamp

Updated January 15, 2018 23:27 PM

What is happening to the none approved transactions?

Updated January 17, 2018 14:27 PM

What is the purpose of Bitcoin timestamps

Updated September 01, 2017 11:27 AM