Thursday, November 20, 2008

Today's Timestamp is Tomorrow's Date

I have been working on a RSS feed for Q2OH. The Feeds plugin makes it as easy as I can imagine. I have run into a quirk or two with data formats that have nothing at all to do with the plugin. It seems my Dates are Timestamps. Grails does such a good job of things just working that I never noticed before. Trying to format a Timestamp is not exactly the same as formatting a Date. Also, when an API needs a Date, it may choke on a Timestamp. This makes sense and once understood is not too huge a stumbling block. However I noticed one thing that I found interesting and works in my favor. Incrementing a Timestamp seems to turn it into a Date.

Since Q2OH is the Quote of Tomorrow, in the feed entry I add a day to the field that holds the day the quote was added to the feed. In the Domain Class Quote, qotd is a Date. Here is the entire code necessary to create the feed:

class FeedsController {

def rss = {
render(feedType:"rss", feedVersion:"2.0") {
title = "Q2OH >> Quote of Tomorrow"
link = "http://host:port/app/rss/feed"
description = "The Quote of Tomorrow"

Quote.findAllByQotdIsNotNull().each() { quote ->
entry("Quote for ${quote.qotd + 1}") {
link = "http://host:port/app/quote/display/${quote.id}"
author = quote.author
publishedDate = (java.util.Date) quote.qotd
quote.content // return the content
}
}
}
}
}

This results in an entry title of : Quote for Fri Nov 21 10:22:50 EST 2008

Originally the code was:
    entry("Quote for ${quote.qotd}") {

That had an entry title of: Quote for 2008-11-20 10:22:50.0

So if you ask me, tomorrow looks better than today.

No comments:

Post a Comment