# 每天进步一点点：bitshares中如何获取指定时间点的区块编号？

in python-bitshares •  10 months ago

(图源 ：pixabay)

20多天过去了，今天我决定抽出时间来实现一下当初的设想，首先我们简化一下问题，读取时间段内的区块，我们只需知道时间点1对应的区块编号以及时间点2对应的区块编号即可，那么这个这个文件可以简化为：如何获取指定时间点的区块编号？

# 原理

• 每个区块都有时间戳(timestamp)
• 两个相邻区块之间的时间间隔为3秒

``````db_base = Block(base_num).time()
seconds = dp - dp_base
blocks_diff = int(seconds / 3)
block_num = base_num + blocks_diff
``````

# 代码

``````def find_block_by_time(data_point, base):

print("\tBase:", base)

dp = Block(base).time()
delta = data_point - dp
diff_seconds = delta.total_seconds()

if abs(diff_seconds) > 3:
diff_blocks =  int(diff_seconds / 3)
base += diff_blocks
block_num = find_block_by_time(data_point, base)
return block_num
else:
return base
``````

# 测试

#### 例子一：指定邻近区块为基准

``````dp = datetime.strptime('2017-12-01T00:08:00', '%Y-%m-%dT%H:%M:%S')
block_num = find_block_by_time(dp, base=22250000)
print(block_num)
print(Block(block_num).time())
``````

### 例子二：指定创世块为基准

``````dp = datetime.strptime('2017-12-01T00:08:00', '%Y-%m-%dT%H:%M:%S')
block_num = find_block_by_time(dp, base=1)
print(block_num)
print(Block(block_num).time())
``````

# 用途

(图源 ：pixabay)

Sort Order:

Interesting postings and certainly many who like.
Hopefully the next post is more interesting and useful.

Nice post thanks

Nice post about Bitshares @oflyhigh. Thanks for sharing

i couldn't understand what you say.i have no idea about your language.if it will be English so that i will understand it properly. @oflyhigh