Jorge Curioso posted on his blog some comments around dealing with dates in RSS and the .Net framework. I figure I'd put some more detail up here on how I handle dates in JoeBlogger.
- I store both GMT and local time when creating an entry. I should probably keep a "last updated" time too but I haven't done that yet. From these two you can either output absolute time (for RSS) or you can output local time so that everyone can know your state of mind when you made that post. It makes a difference if you are posting at 5 am in the morning vs. 10 am.
- I used C# to do all of my date processing. Here is the code for my properites for these things:
// The PubDate here is in universal time -- UTC [XmlIgnore] public DateTime PubDate; [XmlElement(ElementName="PubDate")] public string PubDateString { get { return PubDate.ToString("r"); } set { PubDate = DateTime.ParseExact(value,"r", null); } } [XmlIgnore] public DateTime LocalPubDate; [XmlElement(ElementName="LocalPubDate")] public string LocalPubDateString { get { return LocalPubDate.ToString("ddd, dd MMM yyyy HH':'mm':'ss"); } set { try { LocalPubDate = DateTime.ParseExact(value,"ddd, dd MMM yyyy HH':'mm':'ss", null); } catch (FormatException) { LocalPubDate = DateTime.MinValue; } } }
- Finially, I do all of the massaging from these times into my display times in the stylesheet itself. Here is an example of a source XML document for my main page and here is my XSLT template for transforming that. If you can assume the .Net implementation of XSLT, you can use some MS specific extensions like this:
<msxsl:script implements-prefix='user' language='CSharp'> <![CDATA[ public string FormatTime(string input) { DateTime dt = DateTime.ParseExact(input, "ddd, dd MMM yyyy HH':'mm':'ss", null); return dt.ToString("h':'mm':'ss tt"); } public string FormatArchiveLink(string input) { DateTime dt = DateTime.ParseExact(input, "ddd, dd MMM yyyy HH':'mm':'ss", null); return String.Format(@"/Archive/{0:D4}/{1:D2}/{2:D2}.html", dt.Date.Year, dt.Date.Month, dt.Date.Day); } ]]> </msxsl:script>
And then use that in the output like this:<xsl:value-of select="user:FormatTime(LocalPubDate)"/>
It might be a good thing if we someone came up with a namespaced extension for RSS to speicfy local time along with GMT. Having "First published" and "Last changed" times might be interesting also. Perhaps I'll write something up at some point.