views:

340

answers:

1

Hello,

I am trying to parse dates in RFC1123 format (Thu, 21 Jan 2010 17:47:00 EST).

Here is what I tried but none worked:

DateTime Date = DateTime.Parse(dt);
DateTime Date = DateTime.ParseExact(dt, "r", null);

Could you please help me out with this?

Thanks, Ruby :)

+2  A: 

Have you tried something like:

string dateString, format;  
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

dateString = "Thu, 21 Jan 2010 17:47:00 EST";
format = "ddd, dd MMM yyyy hh:mm:ss EST";

result = DateTime.ParseExact(dateString, format, provider);
Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());

I haven't tested it yet (will in a few moments)... but I believe that will do it for you.

Edit: It seems that the problem is that RFC1123 states that the timezone should always be GMT... which is why r or R did not work as a format for you. The problem is the EST. The pattern above accounts for EST, but it is static so if you have any other timezone you might be in trouble. The best solution would be to go with the RFC1123 standard and go to GMT and it should solve your problem. If you can't, let me know I might have a solution.

Edit 2: This is not a complete solution but what it does it isolates the timezone and still allows you to parse it. The code doesn't know the timezone that it is being presented with but you can throw any timezone abbreviation at it and it will parse the time. If you want to convert to GMT and then use r or R you can take the result of the regex match, put it against a lookup table (to see what the time offset it for that timezone abbreviation), then convert the time to GMT and parse from there. That would be a good solution but a little more work. Here's the code:

string dateString, format, pattern, tz;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
pattern = @"[a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ (?<timezone>[a-zA-Z]+)";
dateString = "Thu, 21 Jan 2010 17:47:00 EST";

Regex findTz = new Regex(pattern, RegexOptions.Compiled);

tz = findTz.Match(dateString).Result("${timezone}");

format = "ddd, dd MMM yyyy HH:mm:ss " + tz;

try
{
    result = DateTime.ParseExact(dateString, format, provider);
    Console.WriteLine("Timezone format is: {0}", format);
    Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
}
catch (FormatException)
{
    Console.WriteLine("{0} is not in the correct format.", dateString);
}

    Console.ReadLine();

Here is a list of UTC offsets for you if you would like to turn this into a timezone converter:

Timezone Abbreviations with UTC offsets

Tim C
It makes sense. But the problem is that I have no control over the format of the date as it comes from an external source. Any ways to convert non-GMT timezone to GMT based and then applying "r" or "R" ??
Ruby
I wish I had a better answer then what I am about to tell you but... abbreviations are not recognized., I have a solution but it's not fun or elegant. I'll post it and cross my fingers that someone else can help you better than me! I am so sorry.
Tim C
Its not perfect but much better! :) I appreciate your help, I really do :)
Ruby
If you get a chance and if this has been helpful, please feel free to click on the little check mark to the upper left side of this post to accept it. It will turn green. Thanks!
Tim C