Parsing Rss/Opml DateTimes
I have been fighting a bit with how to "correctly" parse DateTimes from RSS & Opml feeds. After much wrangling, I decided to go with the most strict form of parsing, whereby I assume the datetime is in RFC822 or RFC1123 format.
My Opml library uses Serialization, so I use the following technique for Serializing/Deserializing DateTimes:
/// <summary>
/// Indicates when the document was created
/// </summary>
[XmlIgnore()]
public DateTime DateCreated
{
get{ return _dateCreated;}
set{ _dateCreated = value;}
}
/// <summary>
/// Indicates when the document was created
/// In Universal Time.
/// </summary>
[XmlElement("dateCreated")]
public string DateCreatedUT
{
get
{
if (_dateCreated == DateTime.MinValue)
return String.Empty;
else
return _dateCreated.ToUniversalTime().ToString("r");
}
set
{
DateTime tmpDate = DateTime.MinValue;
if (value.Trim() != String.Empty)
{
try
{
// Ty to parse as an RFC1123
tmpDate = DateTime.Parse(value, CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.None);
}
catch
{
}
}
_dateCreated = tmpDate;}
}
I store the DateTime internally in a LocalDate format, but Serialize/Deserialize it as UniversalTime. As a result, I run into two problems. 1) What do I store if DateTime parsing fails? (I chose DateTime.MinValue), and 2) What should I return if the DateTime value is invalid? (I chose String.Empty)
There must be a better way...any suggestions?