# DateTime conditional for RSS parser

**URL:** <https://www.ruby-forum.com/t/datetime-conditional-for-rss-parser/148007>\
**Category:** Rails\
**Created:** [September 8, 2008, 9:03am UTC](https://www.ruby-forum.com/t/datetime-conditional-for-rss-parser/148007 "2008-09-08T09:03:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [September 8, 2008, 9:03am UTC](https://www.ruby-forum.com/t/datetime-conditional-for-rss-parser/148007/1 "2008-09-08T09:03:28Z")

</div>

I’ve been struggling with an adjustment to a rss parsesr (a freelancer  
built the feature, I’m a newbie working on my own app). The parser needs  
a conditional which means it will only parse new posts in a feed if they  
have a date\_published which is later than the time at which the Source  
(the applications model naming convention for a feed URL) was created  
at, i.e. date\_published later than source.created\_at. [1] is the  
original script, [2] is my failed attempt (but maybe it gives you an  
idea of what I’ve been trying to do);

[1]

if feed  
feed.items.each do |item|  
item\_date =  
DateTime.parse(item.date\_published.to\_s).strftime("%Y-%m-%d %H:%M:%S")

```
if NewsItem.find(:first, :conditions => {:published_at => item_date,

```

:source\_id =\> source.id})  
RAILS\_DEFAULT\_LOGGER.info(“RssReader: Already scraped  
#{item.url}”)  
else  
news\_item = NewsItem.new(  
:title =\> item.title,  
:url =\> item.url,  
:excerpt =\> item.content…

[2]

if feed  
feed.items.each do |item|  
item\_date =  
DateTime.parse(item.date\_published.to\_s).strftime("%Y-%m-%d %H:%M:%S")

```
# the new conditional
if item_date < source.created_at
  RAILS_DEFAULT_LOGGER.info("RssReader: No new NewsItems since the

```

Source was created")  
elsif NewsItem.find(:first, :conditions =\> {:published\_at =\>  
item\_date, :source\_id =\> source.id})  
RAILS\_DEFAULT\_LOGGER.info(“RssReader: Already scraped  
#{item.url}”)  
else  
news\_item = NewsItem.new(  
:title =\> item.title,  
:url =\> item.url,  
:excerpt =\> item.content…

Any ideas? I’m presuming the above doesn’t work because it’s a  
comparison of two strings. However, a friend helped me to try many  
different types of DateTime.parse and Time.parse on the  
source.created\_at in the conditional, to get a fair comparison of  
DateTime formats, but none of them worked. We also followed some trial &  
error by grabbing the output of different DateTime.parse methods in the  
log, but we couldn’t find one that works (to\_i didn’t seem to do it).

Is there a Rails ‘later\_than’ help or a guide to successful DateTime  
comparisons out there? Or does anyone know the correct approach for  
this?
