1

I am trying to compare a date I extracted from a csv file to the current date, to check if the difference is bigger than seven days. Here is my code:

with open (path) as csvfile:
    readcsv = csv.reader(csvfile, delimiter = ',')
    for row in readcsv:
        iso_ts = str(row[3])
        datum = (datetime.datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z'))

    current_time = (datetime.datetime.strptime(datetime.datetime.now(),'%Y-%m-%dT%H:%M:%S%z'))

Without even comparing these I get the following error

File "./netapp.py", line 32, in <module>
    current_time = (datetime.datetime.strptime(datetime.datetime.now(),'%Y-%m-%dT%H:%M:%S%z'))
TypeError: must be str, not datetime.datetime

I would like to check, if the data coming from the csv is older than 7 days to the current date and then do something. I know this is some problem with the format of either one of these dates, but i can not seem to figure out what it is exactly. I would be very grateful for an explanation about what I am missing.

wpercy
  • 9,636
  • 4
  • 33
  • 45

2 Answers2

2

datetime.strptime() takes a date_string and a format string and converts the datestring argument to a datetime object. I don't think you need to convert anything, you should be able to compare datum to datetime.datetime.now() using a timedelta object.

Beware, .now() returns a naive datetime object that represents local time. Local time may be ambiguous -- do not use it for comparison. I see %z (utc offset) in the format string; .strptime() returns a timezone-aware datetime object on Python 3. You can't compare naive and aware datetime objects. Use .now(timezone.utc) to create a timezone-aware current time that can be used for comparison. See Find if 24 hrs have passed between datetimes - Python

Community
  • 1
  • 1
wpercy
  • 9,636
  • 4
  • 33
  • 45
  • 1
    @scriptkiddie: beware, `.now()` returns a naive datetime object that represents local time. Local time may be ambiguous -- do not use it for comparison. I see `%z` (utc offset) in the format string; [`.strptime()` returns a timezone-aware datetime object on Python 3](http://stackoverflow.com/q/12281975/4279). You can't compare naive and aware datetime objects. Use `.now(timezone.utc)` to create a timezone-aware current time that can be used for comparison. See [Find if 24 hrs have passed between datetimes - Python](http://stackoverflow.com/q/26313520/4279) – jfs Mar 02 '16 at 20:49
  • @J.F.Sebastian do you mind if I throw that in my answer as an edit? I think it's a great gotcha. – wpercy Mar 02 '16 at 21:00
  • @wilbur : it is a minor point (the period is 7 days) but it wouldn't hurt to mention it. – jfs Mar 02 '16 at 21:07
0

Your format string is wrong. Try this: current_time = datetime.datetime.today().strftime("%Y-%b-%dT%H:%M:%S")

  • I didn't know you could call `strftime()` directly on a `datetime` object - very cool – wpercy Mar 01 '16 at 14:48
  • 1
    Generating a string is the wrong way to find out whether more than a week has passed since the given timestamp. You should do the opposite: parse the timestamp into a date/time object and find the difference. – jfs Mar 02 '16 at 21:06