title: Comparing two Python 3 datetime objects returns "can't compare offset-naive and offset-aware datetimes: TypeError"
body: <p>I am trying to compare the time of an AWS EC2 instance object that is of type datetime with another datetime being represented as datetime.datetime.now. The line of code in question looks like, </p>
  
  <pre><code>if launchTime &lt; datetime.datetime.now()-datetime.timedelta(seconds=20):
  </code></pre>
  
  <p>Where launchTime is of type datetime. However when I run it I get the error </p>
  
  <pre><code>can't compare offset-naive and offset-aware datetimes: TypeError
  </code></pre>
  
  <p>And I'm unsure of how to convert launchTime in such a way where I can successfully compare it. </p>
  
  <p>Edited fixed code below -----------------------------------------</p>
  
  <pre><code>if launchTime.replace(tzinfo=None) &lt; datetime.datetime.now()-datetime.timedelta(minutes=4):
  </code></pre>
  
  <p>Full code as well in case any future people find it of value. It's Python 3 to stop EC2 instances that have been running for an "x" amount of time. In this case if an instance is running for five minutes. Terminate it. The lambda itself is set up with Cloudwatch to run every 4 minutes as well.</p>
  
  <pre><code>import boto3
  import time
  import datetime
  
  #for returning data about our newly created instance later on in fuction
  client = boto3.client('ec2')
  
  def lambda_handler(event, context):
  
  response = client.describe_instances()
  #for each instance currently running/terminated/stopped
  for r in response['Reservations']:
      for i in r['Instances']:
          #if its running then we want to see if its been running for more then 3 hours. If it has then we stop it. 
          if i["State"]["Name"] == "running":
              launchTime = i["LaunchTime"]
  
              #can change minutes=4 to anything
              if launchTime.replace(tzinfo=None) &lt; datetime.datetime.now()-datetime.timedelta(minutes=4):
                  response = client.stop_instances(
                      InstanceIds=[
                          i["InstanceId"]
                      ]
                  )
  </code></pre>