1

I have written a simple code to check if the time difference between 2 date timestamps is more than 7 days, which comes to 604,800 seconds.

If the time in seconds is more than 604,800 then it should print "Relax you have time!!!"

Please find my code below:

import time, datetime, sys, os 
start_time = time.time() 
from datetime import datetime, timedelta, date 
from dateutil.parser import *

datetime1="2018-07-13 03:30:00"  
datetime2="2018-07-20 04:30:00" 
datetime2=datetime.strptime(datetime2, "%Y-%m-%d %H:%M:%S").date() # this is to convert it into a datetime object
datetime1=datetime.strptime(datetime1, "%Y-%m-%d %H:%M:%S").date() # this is to convert it into a datetime object

difference1 =(datetime2-datetime1).total_seconds() 
print("the difference in seconds is "+str(difference1)) 
if difference1 > 604800: #if the difference is more than 7 days, relax , else start preparing
  print("Relax you have time!!!") 
else:
  print("You need to start preparing!!!!!")

Problem:

The code somehow calculates the time in seconds to be more than 604800 only if I change the "datetime2" to "2018-07-21" which means that it is calculating the difference in rounded-off days and not seconds and then simply converting the rounded-off days into seconds, giving the incorrect answer.

For example, in the above code, since "datetime2" is in reality away from "datetime1" by more than 604,800 seconds(to be precise it is 608,400 seconds away), the output should be "Relax you have time!!!", but we get a different output.

What have I done to solve this?

Till now I have looked at similar questions:

How do I check the difference, in seconds, between two dates? (did not work for me as I got TypeError: an integer is required (got type datetime.date))

and Time difference in seconds (as a floating point) (this caters to only very tiny time differences and does not capture a scenario when user enters timestamps himself)

and How to calculate the time interval between two time strings (this is what I have done in my code, but it does not work as expected)

Can you please suggest the problem in my code?

UPDATE: Thanks to @Tim Peters for pointing out that .date() discards the hours,mins and seconds. I only needed to discard .date() for it to work correctly.

Stan
  • 227
  • 5
  • 13
  • 3
    Note: `.date()` truncates the time component from datetimes and creates a datetime.date object. – Paul Jul 13 '18 at 02:35
  • 3
    Expanding on @Paul's comment. by using `.date()` you'e throwing away the hours, minutes, and seconds. The resulting `datetime.date` objects are exactly 7 days apart. – Tim Peters Jul 13 '18 at 02:38
  • @TimPeters: Thank you, I did not know that it discarded the hours, mins and seconds. is there something similar to .date() which does not discard the hours,mins and seconds? – Stan Jul 13 '18 at 02:40
  • 4
    Yes! Simply delete the instances of `.date()`. You _had_ full-blown `datetime.datetime` objects before you went out of your way to damage them ;-) – Tim Peters Jul 13 '18 at 02:41
  • 1
    @TimPeters: Wow that was easy! I completely missed that! Thanks!!!!! – Stan Jul 13 '18 at 02:44

1 Answers1

0

In this case the issue is that you create two datetime.datetime objects with strptime and immediately truncate them to datetime.date objects, which don't have the time components (hours, minutes, seconds, microseconds, tzinfo), so you get two calendar dates which are exactly 7 days apart.

You can fix your original code like this:

from datetime import datetime, timedelta

datetime1 = "2018-07-13 03:30:00"
datetime2 = "2018-07-20 04:30:00"

# The following creates two datetime.datetime objects
datetime2 = datetime.strptime(datetime2, "%Y-%m-%d %H:%M:%S")
datetime1 = datetime.strptime(datetime1, "%Y-%m-%d %H:%M:%S")

difference1 =(datetime2-datetime1).total_seconds() 
print("the difference in seconds is "+str(difference1)) 

# if the difference is more than 7 days, relax , else start preparing
if difference1 > 604800:
  print("Relax you have time!!!") 
else:
  print("You need to start preparing!!!!!")

But one additional thing to note is that datetime.timedelta objects can be directly compared, so you do not need to calculate the number of seconds, so you can change that part to avoid the "number of seconds" calculation (and your intention is clearer):

difference1 = datetime2 - datetime1

# if the difference is more than 7 days, relax , else start preparing
if difference1 > timedelta(days=7):

I imagine that in reality you are not constructing the datetime.datetime objects from string literals as you have in this example, but in case you are, I would also note that you can directly construct those literals as well, so with that refactoring in place, here's how I would have written your example:

from datetime import datetime, timedelta

# The following creates two datetime.datetime objects
datetime1 = datetime(2018, 7, 13, 3, 30)
datetime2 = datetime(2018, 7, 20, 4, 30)

difference1 = datetime2 - datetime1

# if the difference is more than 7 days, relax , else start preparing
if difference1 > timedelta(days=7):
  print("Relax you have time!!!") 
else:
  print("You need to start preparing!!!!!")
Paul
  • 10,381
  • 13
  • 48
  • 86