views:

234

answers:

6

Hello, I am writing an RSS and Mail reader app in C# (technically MonoTouch).

I have run into the issue of parsing DateTimes. I see a lot of variance in how dates are presented in the wild and have begun writing a function like this:

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;
}

This, well, makes me sick. Two points. (1) It's silly of me to think that I can actually collect a format list that will cover everything out there. (2) It's wrong! Notice that I'm treating an EST date time as UTC (since .NET seems oblivious to time zones).

I am looking for an existing library (source only please) that is known to handle a bunch of these formats.

Also, I would like to keep using UTC DateTimes throughout my code so whatever library is suggested should be able to produce DateTimes.

Is there anything out there like this?

Update It would seem I was unclear in my request. I am looking for a library that knows a lot of these wild format strings. DateTime.Parse/DateTime.TryParse only know a couple formats, and they certainly don't understand timezones (you can see I am already using it). I need something more powerful than DateTime.Parse/DateTime.TryParse.

Update 2 Everyone was dwelling on the fact that I was using Parse instead of TryParse. I have switched the code. But it's still wrong and incomplete.

+2  A: 

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

David Stratton
I call Parse (check the code). It's the same difference in terms of **what is supported**. Each one of the format strings I provided fails DateTime.Parse. I need something with more knowledge.
Frank Krueger
+1  A: 

You could use:

DateTime.TryParse()

instead of

DateTime.Parse()

rather than a try/ catch

hunter
I already do that (see code). But Parse will still fail for the more exotic formats. I can live with the exceptions, what I want is more variety in formats.
Frank Krueger
Really? I'm suggesting you do a TryParse and a TryParseExact rather than your try/ catch.
hunter
@Hunter Parse vs. TryParse. They are the SAME! One throws an exception, the other does not. Same for ParseExact and TryParseExact. They use the same format database. So yes, it helps my point #3, but exceptions are not my true concern. I need a parser that knows a lot of formats.
Frank Krueger
+3  A: 

In addition to TryParse on DateTime, .Net also provides the DateTimeOffset struct which offers access to the UTC offset as another property on the struct, allowing you to store time zone info and datetime info together.

Joel Potter
I'm just trying to avoid creating a format database on my own. Certainly someone must have done this work already? But thanks for the info, if I have to write it on my own, this will be useful.
Frank Krueger
+1 Joel! Stay outta my questions you sumbitch!
hunter
A: 

I have developed a library (NaturalDate) for parsing dates but it might not be what you're looking for as it is targeted at human input forms, not computer generated forms. IIRC it might not even parse the forms you showed above. Also, the website's down for the time being (it was the only thing on that server).

If I get some time (likely, as I might even get paid for it), I'll try and resurrect it as an actively maintained project.

BCS
+1  A: 

I had the same problem with rss feeds that included dates with the name of timezones, not the offset. I thought about creating my own DateTime Parser for this situation, but I beleived that someone probably had already created it. After a few hours of searching I found the following

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

I'm currently using this code from the previous url in a project. Even though the project has not gone to prod, the code seems to work.

BarDev

BarDev
+1  A: 

Frank, to parse 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)

miguel.de.icaza