1

I am creating simple RSS reader. Storing date of last read newest entry in newest_entry_datetime and then when reading again channel I am comparing entry time to newest_entry_datetime with < symbol as I read that Python is smart enough to recognize and compare datetime.

It works on the same day when time part is changing but on the next day newest date is implemented as old.

import datetime
import locale

#locale.setlocale(locale.LC_ALL, 'English_United States.1252')

newest_entry_datetime = 'Thu, 21 Dec 2017 16:02:03 CET'
entry_published = 'Fri, 22 Dec 2017 08:19:15 CET'

#dt_newest = datetime.datetime.strptime (newest_entry_datetime, "%a, %d %b %Y %H:%M:%S %Z" )

if (entry_published <= newest_entry_datetime):
    print('Entry date is older')
else:
    print('Entry date is NEW')

With such code I am getting result: "Entry date is older" which is wrong.

Second idea was to convert datestamps to datetime but I am getting:

ValueError: time data 'Thu, 21 Dec 2017 16:02:03 CET' does not match format '%a, %d %b %Y %H:%M:%S %Z'

even if I will change locale to US.

No clue how to do that correctly. Could you please help?

tobias_k
  • 81,265
  • 12
  • 120
  • 179

2 Answers2

1

If you compare you "dates" before converting to datetime - you compare strings. First you need convert to datetime (use for it right format if current is not support you string datetime style), and after that you can compare two datetime objects. You can't convert to datetime to this format because of 'CET', for timezones you can you custom desicion (like this).

1

Thanks to Anton vBR answer that CET is not recognized I just removed this part of string as feed will always have the same timezone.

Final code that works and gives proper result is here.

import datetime
import locale

locale.setlocale(locale.LC_ALL, 'English_United States.1252')

newest_entry_datetime = 'Thu, 21 Dec 2017 16:02:03 CET'
entry_published = 'Fri, 22 Dec 2017 08:19:15 CET'

newest_entry_datetime = newest_entry_datetime.rsplit(" ", maxsplit=1)[0]
entry_published = entry_published.rsplit(" ", maxsplit=1)[0]
dt_newest = datetime.datetime.strptime (newest_entry_datetime, "%a, %d %b %Y %H:%M:%S" )
st_entry = datetime.datetime.strptime (entry_published, "%a, %d %b %Y %H:%M:%S" )

if (st_entry <= dt_newest):
    print('Entry date is older')
else:
    print('Entry date is NEW')

Result is: 'Entry date is NEW' as it was expected.