I am writing an RSS and mail reader application in C # (technically MonoTouch).
I ran into a problem parsing DateTime s. I see a lot of differences in how dates are represented in the wild and started writing this function:
static string[] DateTimeFormats = new string[] { "ddd, d MMM yyyy H:mm:ss \"GMT+00:00\"", "d MMM yyyy H:mm:ss \"EST\"", "yyyy-MM-dd\"T\"HH:mm:ss\"Z\"", "ddd MMM d HH:mm:ss \"+0000\" yyyy", }; public static DateTime ParseTime(string timeStr) { var r = DateTime.MinValue; var styles = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces; if (DateTime.TryParse(timeStr, CultureInfo.InvariantCulture, styles, out r)) { return r; } else { if (DateTime.TryParseExact(timeStr, DateTimeFormats, CultureInfo.InvariantCulture, styles, out r)) { return r;
That, well, makes me sick. Two points. (1) Itβs stupid for me to think that I can put together a list of formats that will cover everything. (2) This is wrong! Note that I treat EST timestamps as UTC (since .NET does not seem to pay attention to time zones).
I am looking for an existing library (source only) that is known to handle a bunch of these formats.
Also, I would like to use UTC DateTimes throughout my code, so whenever a library is offered, you can create DateTimes.
Is there anything similar?
Update . In my opinion, I was unclear in my request. I am looking for a library that knows a lot of these wild lines. DateTime.Parse / DateTime.TryParse know only a couple of formats, and they, of course, do not understand time intervals (you can see that I already use it). I need something more powerful than DateTime.Parse / DateTime.TryParse.
Update 2 . Everyone said that I used Parse instead of TryParse. I switched the code. But this is still wrong and incomplete.
Frank krueger
source share