A robust DateTime parser library for .NET.

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; // BUGGY! Ignores time zone!! } } Console.WriteLine ("BAAAAAAAAAAAAD"); return DateTime.MinValue; } 

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.

+7
datetime parsing
source share
6 answers

In addition to TryParse in DateTime.Net also provides a DateTimeOffset structure that offers access to the UTC offset as another property in the structure, allowing you to save time zone information and time and time information.

+3
source share

Have you tried using DateTime.TryParse ? This function already handles most formats.

+2
source share

You can use:

 DateTime.TryParse() 

instead

 DateTime.Parse() 

not try / catch

+1
source share

I had the same problem with rss feeds that included dates with a timezone name rather than an offset. I was thinking of creating my own DateTime Parser for this situation, but I figured someone probably already created it. After hours of searching, I found the following

C #: Date Sync with TimeZone
http://hashfactor.wordpress.com/2009/02/02/c-parsing-datetime-with-timezone/

I am currently using this code from a previous url in a project. Despite the fact that the project did not go to development, this code seems to work.

Bardev

+1
source share

Frank, to parse the time zones you want to use zz or zzz.

Future versions of MonoTouch will also support the new β€œK” modifier (which is an alias for zzz)

+1
source share

I developed a library (NaturalDate) for parsing dates, but this may not be what you are looking for, as it is for custom input forms, not computer-generated forms. IIRC may not even have made out the forms shown above. In addition, the website is not yet available (this was the only thing that was on this server).

If I get some time (most likely, I’m even paid for it), I will try to resurrect it as an actively supported project.

0
source share

All Articles