Reputation: 1521
My program is handling events that appear periodically. I want to hash them into buckets by their arrival time, in other words, truncate the event time by an arbitrary amount of seconds.
There are a lot of date and timestamp related questions, but I didn't find the answer to this. I figured out a way that works for me, and I wanted to share it. So I'll just answer the question below.
Upvotes: 0
Views: 832
Reputation: 1521
Divide the event time by the bucket size, then multiply that (the bucket count) by the bucket size. This just removes remainder. Now an event can use that for the event time.
Here is a script and it's output to demonstrate how simple this is using 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)))
And here is the output for today...
---------------
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
Upvotes: 1