Lance's Whiteboard

Random scribbling about C#, ASP.NET, Sql Reporting, etc.

News

BlogMailr Enabled




Sponsored Ad
Sponsored Ad

Blogs I Read

Developer Sites

Googling, etc...

MSDN Resources

Tutorials & Reference

Date Time Best Practices & RSS

I quickly recognized many of my pet peaves with DateTime problems from the recent MSDN article, "Coding Best Practices Using DateTime in the .NET Framework".

The single biggest offender in the category of "inaccurate DateTime usage" is RSS feeds!

Today, several popular & commonly used RSS class libraries introduce inaccurate dates due their impenetrable API's, and the way they are consumed by developers. The main cause is that the RSS specification requires dates to be expressed as an RFC 822 date/time.

ex.

Mon, 11 Mar 2004 10:42:02 CST

This is just a different string-expression for a date/time that includes the Timezone. This isnt a problem in itself but can become a problem because most RSS library API's use a standard DateTime type, but use parameter names such as "pubDate" which doesnt explicitly tell the developer that RSS date-times must be timezone-specific. So, after the RSS feed is created, and output as XML, it takes a keen eye to notice that the DateTime says it is in GMT, but in fact is incorrect to the tune of 6 hours (this .Text weblog engine is a perfect example). This happens because the .NET Framework doesnt provide a built-in converter for RFC822, so RSS libraries often use a format-string that forces the timezone to GMT or UT:

private const string DateTimeFormatString = "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'";
instead of:
private const string DateTimeFormatString = "ddd, dd MMM yyyy HH':'mm':'ss zzz";

The RSS Library creators will simply say "Its the developer's fault for not converting to GMT", others will say "Its the .NET Framework's fault for not supporting RFC822". Both are true, but it is equally the fault of the RSS API implementation for not leading the eager developer to the correct implementation. As a result, most developers will start by creating a new instance of an RSS feed and begin adding new RSSItems based upon their local data-source without regard to timezone. Frequently this local data source is based upon local time, not GMT or UT (Universal Time) therefore you end up with skewed datetimes.

So, I would just like to add a Best Practice of:

Always make your DateTime variable-names explicitly state the DateTime type expected. For example, use "pubUTDate" or "pubGMTDate" instead of just "pubDate" for your parameter or property name. This will make your API's behavior much more transparent when dealing with DateTime values. Alternately, for RSS feeds, you should consider names such as Rfc822Date. Perhaps someone could do the work of creating a new Rfc822Date formatter, converter, and type to simplify this further.

Comments

Scott Mitchell said:

>Perhaps someone could do the work of creating a new Rfc822Date formatter, converter, and type to simplify this further

RFC 1123 extends the RFC 822 datetime syntax, no? (See: http://www.freesoft.org/CIE/RFC/1123/99.htm)

The .NET Framework contains a date/time formatter for RFC 1123. This is what I use to format the date/time for the RSS dates.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationdatetimeformatinfoclassrfc1123patterntopic.asp
# March 1, 2004 4:19 PM

Lance said:

Excellent.

I recall looking at RFC 1123 a while back, but hadnt seen the connection to RFC 822. That definitely looks like the preferred implementation.

Thanks!
# March 1, 2004 4:26 PM

TrackBack said:

You've been taken out! Thanks for the posting.
# March 1, 2004 11:50 PM

Chris said:

Hi,

Maybe a dumb question, but how to parse this kind of date time (string) values?

To write i use dt.ToString("r"), it works... but i can't parse real rfc822 date time's like

Tue, 09 Mar 2004 08:59:02 -0800
Tue, 09 Mar 2004 08:59:02 EST

with the build in .NET DateTime struct.

Greetz

Chris
# March 9, 2004 4:03 PM

Lance said:

I'm still working on a good reply to your question, but frankly, I havent tried parsing an RFC822 datetime string yet...will let you know soon (hopefully)
# March 19, 2004 9:37 AM

TrackBack said:

# March 19, 2004 10:47 AM

Richard Tallent said:

I've been using DateTime.ToUniversalTime.ToString("r"). Looks good here, why should I use a custom formatter?
# March 20, 2004 3:02 PM

Lance said:

Yes, I commented on this approach above. It was merely an oversight of mine that RFC1123 covered the RFC822 requirements for RSS.

Therefore, the "r" format string works fine.

However, I am still playing around with the idea of some DateTime helpers and variable naming best-practices to help avoid the problem of an RSS API-consumer passing-in a UniversalTime value and it getting converted again into UT, and therefore ending up with an invalid DateTime.

The main point of this post was to draw attention to common problems when people DO use different ways of formatting their RSS feed dates, AND to draw attention to the issues with inadvertantly creating inaccurate Date/Times due to these methods.
# March 20, 2004 5:44 PM

TrackBack said:

# May 27, 2004 10:59 AM

TrackBack said:

# May 27, 2004 11:29 AM

TrackBack said:

# September 1, 2004 4:49 PM

.Net Discoveries said:

Prologue Not long ago, I discovered how cool RSS can be. I was having trouble with my Peerflix account,
# August 31, 2006 12:32 PM