开发者

Robust DateTime parser library for .NET

开发者 https://www.devze.com 2022-12-25 20:32 出处:网络
I am writing an RSS and Mail reader a开发者_运维知识库pp 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

I am writing an RSS and Mail reader a开发者_运维知识库pp 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.


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.

  • MSDN article


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


You could use:

DateTime.TryParse()

instead of

DateTime.Parse()

rather than a try/ catch


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


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消