1

I have a matrix like this in my code with unix timestamps:

event_sequences = [ 
  [1368136883, 1368137089], #The first event is never empty
  [1368214777, 1368214966],
  [],
    .... There are the perfect number of days (empty arrays) in the gaps between the existing events.
  [],
  [1368747495, 1368747833],
  [1368830501, 1368831869]
]

for each event in event_sequences I know its start and its end (start = event_sequences[n][0] and end = event_sequences[n][1]). As you can see there are some events that are empty and the approach I have to take is to set the start and the end of these empty events as 00:00 and 23:59 of the day after the last event recorded. Like

[ 
 [start, end],
 [start, end],
 [the day after the last event at 00:00, the day after the last event at 23:59]
]

The start and end of the empty events also need to be unix timestamps. How can I do that in python?

Joabe da Luz
  • 1,030
  • 2
  • 18
  • 32
  • related: [How to convert tomorrows (at specific time) date to a timestamp](http://stackoverflow.com/q/30822699/4279) – jfs Apr 12 '16 at 00:16

1 Answers1

1

The following should do the trick. A few questions/things to be aware of though:

  • This will fail if the first element in event_sequences is an empty list
  • Should there be gaps in days in event_sequences? For example, you'll see in the output below that there's a gap in your sequence between 5/12/2013 and 5/16/2013, even after the blank entries are filled.
  • In the case here where you have two consecutive blank entries, I assumed you wanted the second blank entry to be the day after the first blank entry

Code:

from datetime import datetime, timedelta
import time

event_sequences = [
  [1368136883, 1368137089],
  [1368214777, 1368214966],
  [],
  [],
  [1368747495, 1368747833],
  [1368830501, 1368831869]
]

#take the last_day recorded date as a datetime object from the event_sequences and return a 2-element list
#with the unix timestamps for 00:00 and 23:59
def getNextDay(last_day):
    next_day = last_day + timedelta(days=1)
    next_day_start = next_day.replace(hour=0,minute=0,second=0)
    next_day_end = next_day.replace(hour=23,minute=59,second=0)
    return [int(next_day_start.strftime("%s")), int(next_day_end.strftime("%s"))]

def fillEmptyDates(event_list):
    new_event_list = []
    #note: this will fail if the first element in the list of event_sequences is blank
    last_day = int(time.time())
    for x in event_sequences:
        if len(x) == 0:
            next_day = getNextDay(last_day)
            last_day = datetime.utcfromtimestamp(next_day[1])
        else:
            next_day = x
        last_day = datetime.utcfromtimestamp(next_day[1])
        new_event_list.append(next_day)
    return new_event_list  

new_event_sequence = fillEmptyDates(event_sequences)

print new_event_sequence

#[[1368136883, 1368137089], [1368214777, 1368214966], [1368230400, 1368316740], [1368316800, 1368403140], [1368747495, 1368747833], [1368830501, 1368831869]]

for event in new_event_sequence:
    print str(datetime.utcfromtimestamp(event[0]))+ ' and '+str(datetime.utcfromtimestamp(event[1]))

#2013-05-09 22:01:23 and 2013-05-09 22:04:49
#2013-05-10 19:39:37 and 2013-05-10 19:42:46
#2013-05-11 00:00:00 and 2013-05-11 23:59:00
#2013-05-12 00:00:00 and 2013-05-12 23:59:00
#2013-05-16 23:38:15 and 2013-05-16 23:43:53
#2013-05-17 22:41:41 and 2013-05-17 23:04:29
Max
  • 458
  • 3
  • 10
  • Yes I forgot to specify: 1 The fist element will never be empty, 2 there's gaps between the event_sequences itens and the must be filled as I said, 3 - 2 blank entries: the second blank entry gets the last entry, even if it was blank. – Joabe da Luz Apr 28 '16 at 03:07
  • I am using you solution @Max, but I found a problem yet. I will edit the post to show you. – Joabe da Luz Apr 28 '16 at 03:26
  • Your solutions is right, I just was using ctime to verify the sequences, but my local time interfered on the function an it was giving me the wrong time string. I tested it online and it's working properly. (http:// goo .gl/hz2KUS) – Joabe da Luz Apr 28 '16 at 05:12