I needed to parse RSS/Atom feeds and I was rather surprised to find that the standard .NET class libraries do not include a convenient way to successfully parse strings like "Tue, 01 Dec 2009 10:22:00 EST". The problem lies in the last part that says EST (or some other abbreviation). Based on my experiments, it is appearing in feed pubDate values every now and then. I don't know what the specs say about the time formats, but the dominant format seems to be like this: "Mon, 19 Mar 2007 23:48:18 -0700", which gets fortunately parsed correctly by DateTime.Parse().
So, I needed a way to parse the "incorrectly formatted" string. Another surprise was that .NET class libraries do not even seem to include those abbreviated timezones either (TimeZoneInfo class comes close though). So I ended up writing this to solve the problem:
// Handles cases like "Tue, 01 Dec 2009 10:22:00 EST" which cannot be parsed by DateTime.TryParse()
private static DateTime ParseSpecialDateTime(string datetimeString)
{
Dictionary<string, int> knownTimeZoneAbbreviations = new Dictionary<string, int>()
{
{"AST", -4},
{"ADT", -3},
{"EST", -5},
{"EDT", -4},
{"CST", -6},
{"CDT", -5},
{"MST", -7},
{"MDT", -6},
{"PST", -8},
{"PDT", -7},
{"AKST", -9},
{"AKDT", -8},
{"HAST", -10},
{"HADT", -9}
};
foreach (String key in knownTimeZoneAbbreviations.Keys)
{
DateTime dt;
if (datetimeString.Contains(key))
{
string withoutAbbr = datetimeString.Replace(key, "");
if (DateTime.TryParse(withoutAbbr, out dt) == true)
{
dt = dt.AddHours(knownTimeZoneAbbreviations[key]);
return dt;
}
else
break;
}
}
return DateTime.Now;
}
If anybody knows a better way to do this, feel free to write a comment or email me at aali.alikoski (at) avanade.com