python哈希事件基于时间进入桶

时间:2016-07-13 00:51:21

标签: python hash timestamp

我的程序正在处理定期出现的事件。我希望通过它们的到达时间将它们哈希到桶中,换句话说,将事件时间截断任意秒数。

有很多日期和时间戳相关的问题,但我没有找到答案。我想出了一种适合我的方式,我想分享它。所以我只想回答下面的问题。

1 个答案:

答案 0 :(得分:0)

将事件时间除以桶大小,然后将该(桶数)乘以桶大小。这只是删除剩余部分。现在,事件可以将其用于事件时间。

这是一个脚本,它的输出是为了演示使用python是多么简单:

from datetime import datetime
import time

TS='%m/%d/%Y %H:%M:%S'
def mkdate(d):
    return time.strftime(TS, time.gmtime(d))

def mkhash(time_bucket_size, current_time):
    blocks_in_epoch = int(current_time / time_bucket_size)
    return blocks_in_epoch * time_bucket_size

if __name__ == "__main__":
    epoch = int(time.time())

    time_bucket_size = (60 * 60 * 6) # n hrs block size
    event_time_adjustment_modifier = (60 * 60) # test every hour
    test_events_into_past=-10
    test_events_into_future=10

    print("---------------\ntime_bucket_size=%s, epoch=%s, date=%s\n---------------"%(time_bucket_size, epoch, mkdate(epoch)))
    # print up the hashed times for current to n hrs ahead and n hrs behind
    for event_time_adjustment in range(test_events_into_past, test_events_into_future):
        test_epoch = epoch + (event_time_adjustment * event_time_adjustment_modifier)
        time_block_hash = mkhash(time_bucket_size=time_bucket_size, current_time=test_epoch)
        print("test_date=%s, event_time_adjustment=%s, time_block=%s"%(mkdate(test_epoch), event_time_adjustment, mkdate(time_block_hash)))

这是今天的输出......

---------------
time_bucket_size=21600, epoch=1468370760, date=07/13/2016 00:46:00
---------------
test_date=07/12/2016 14:46:00, event_time_adjustment=-10, time_block=07/12/2016 12:00:00
test_date=07/12/2016 15:46:00, event_time_adjustment=-9, time_block=07/12/2016 12:00:00
test_date=07/12/2016 16:46:00, event_time_adjustment=-8, time_block=07/12/2016 12:00:00
test_date=07/12/2016 17:46:00, event_time_adjustment=-7, time_block=07/12/2016 12:00:00
test_date=07/12/2016 18:46:00, event_time_adjustment=-6, time_block=07/12/2016 18:00:00
test_date=07/12/2016 19:46:00, event_time_adjustment=-5, time_block=07/12/2016 18:00:00
test_date=07/12/2016 20:46:00, event_time_adjustment=-4, time_block=07/12/2016 18:00:00
test_date=07/12/2016 21:46:00, event_time_adjustment=-3, time_block=07/12/2016 18:00:00
test_date=07/12/2016 22:46:00, event_time_adjustment=-2, time_block=07/12/2016 18:00:00
test_date=07/12/2016 23:46:00, event_time_adjustment=-1, time_block=07/12/2016 18:00:00
test_date=07/13/2016 00:46:00, event_time_adjustment=0, time_block=07/13/2016 00:00:00
test_date=07/13/2016 01:46:00, event_time_adjustment=1, time_block=07/13/2016 00:00:00
test_date=07/13/2016 02:46:00, event_time_adjustment=2, time_block=07/13/2016 00:00:00
test_date=07/13/2016 03:46:00, event_time_adjustment=3, time_block=07/13/2016 00:00:00
test_date=07/13/2016 04:46:00, event_time_adjustment=4, time_block=07/13/2016 00:00:00
test_date=07/13/2016 05:46:00, event_time_adjustment=5, time_block=07/13/2016 00:00:00
test_date=07/13/2016 06:46:00, event_time_adjustment=6, time_block=07/13/2016 06:00:00
test_date=07/13/2016 07:46:00, event_time_adjustment=7, time_block=07/13/2016 06:00:00
test_date=07/13/2016 08:46:00, event_time_adjustment=8, time_block=07/13/2016 06:00:00
test_date=07/13/2016 09:46:00, event_time_adjustment=9, time_block=07/13/2016 06:00:00