Compare a datetime with a datetime range
<p>I am fairly new to coding with Python and have so far managed to google my way out of most issues thanks to the great resources available on this site.</p>

<p>I am writing a program which takes multiple .csv files, strips out the data from each of the initial files into different types of log files and writes these different types of logs into their own .csv.</p>

<p>Now I have stripped the files back I have the need to roll through file A and for each row take the datetime and search file B for the same datetime and copy the relevant data into a new column alongside the initial data. This is nice and easy with a if A == B for loop HOWEVER... each of these logs are written by different computers who's real time clock drifts over time.
So what I actually want is to say take the time from file A and search for a corresponding time in file B +/- 30 seconds which is where I am stuck and have been gong around in circles for the last 3/4 hours!</p>

<p>Currently when I run the below code extract I get the following:</p>

<p>---> 35                 if (timecode - margin) &lt;= datetime.datetime.date(ssptime) &lt;= (timecode + margin):</p>

<p>TypeError: can't compare datetime.datetime to datetime.date </p>

<p>Thanks in advance!</p>

<pre><code>    import matplotlib.pyplot as plt  # external function need from inside Canopy
import os # external functions
import re # external functions
import matplotlib.patches as mpatches
import csv
import pandas as pd
import datetime


addrbase = "23"
csvnum = [1,2,3,4,5] # CSV number
csvnum2 = [1,2,3,4,5]
senstyp = ['BSL'] #Sensor Type
Beacons = 5





outfile = open('C:\Users\xxx\Canopy\2303_AVG2.csv', 'w') #File to write to
outcsv = csv.writer(outfile, lineterminator='\n')

with open('C:\Users\xxx\Canopy\2303_AVG.csv', 'r') as f: #File read vairable f
    csvread = csv.reader(f, delimiter=',') #Stores the data from file location f in csvread using the delimiter of','
    for row in csvread: #sets up a for loop using the data in csvread
        timecode = datetime.datetime.strptime(row[1],'%Y/%m/%d  %H:%M:%S')#BSL time to datetime.datetime
        margin = datetime.timedelta(seconds = 30)

        with open('C:\Users\xxx\Canopy\2301_SSP_AVG.csv', 'r') as f: #File read vairable f
            csvreadssp = csv.reader(f, delimiter=',')
            for line in csvreadssp:
                ssptime = datetime.datetime.strptime(row[2],'%Y/%m/%d  %H:%M:%S')#
                print ssptime
                if (timecode - margin) &lt;= datetime.datetime.date(ssptime) &lt;= (timecode + margin):  
                    relssp = line[6]
                    print "Time: " + str(timecode) + " SSP: " + str(relssp)
        #try:
                    row.append(relssp) #Calculates the one way travel time of the range and adds a new column with the data
                    outcsv.writerow(row) # Writes file
        #except ValueError: #handles errors from header files
        #    row.append(0) #handles errors from header files
outfile.flush()        
outfile.close()    
print "done"  
</code></pre>

<p>You can't compare a <code>datetime</code> representing a specific point in time to a <code>date</code>, which represents a whole day. What time of day should the date represent?</p>

<p><code>ssptime</code> is already a <code>datetime</code> (because that's what <code>strptime</code> returns) - why are you calling <code>date</code> on it? This should work:</p>

<pre><code>if (timecode - margin) &lt;= ssptime &lt;= (timecode + margin):  
</code></pre>

<p>Since your times are all down to second precision, you could also do this:</p>

<pre><code>if abs((ssptime - timecode).total_seconds()) &lt; margin:
</code></pre>

<p>I'm not sure which is clearer - I'd probably lean towards the second.</p>

python csv datetime