2016-07-13 47 views
0

我的程序正在處理定期出現的事件。我想通過它們的到達時間將它們散列到桶中,換句話說,將事件時間截斷任意數量的秒。基於時間的python哈希事件分爲桶

有很多日期和時間戳相關的問題,但我沒有找到答案。我想出了一種適合我的方式,我想分享它。所以我只回答下面的問題。

回答

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