How to parse this date format

A

Adam Clauss

I am trying to parse a date/time string available to me in the format
specified by RFC 2822. I am specifically running into a problem with
the timezone specification. The RFC specifies that the timezone is
specified as a 4 digit offset from UTC, along with a leading + or -.

For example:
-0700

The format string works out roughly to:
ddd, dd MMM yyyy HH:mm:ss zzz

However, the closest usage of the "z" format specifer can get me is
"zzz" => "-07:00" - I believe that the lack of colon in the source data
is throwing it off and causing the ParseExact call to fail. I do not
see an obvious way to create a format string around a 4 digit timezone
specification without a colon.

A specific example that I hit was:
Mon, 22 Mar 2010 03:52:22 -0700

That fails to parse. If I manually in the debugger change it to:
Mon, 22 Mar 2010 03:52:22 -07:00

By inserting the colon, then it works fine.

Trying to parse the string myself to insert a colon feels like a
"hack." Anyone see an easy way out here that I'm missing?

Thanks,

-Adam
 
A

Arne Vajhøj

I am trying to parse a date/time string available to me in the format
specified by RFC 2822. I am specifically running into a problem with the
timezone specification. The RFC specifies that the timezone is specified
as a 4 digit offset from UTC, along with a leading + or -.

For example:
-0700

The format string works out roughly to:
ddd, dd MMM yyyy HH:mm:ss zzz

However, the closest usage of the "z" format specifer can get me is
"zzz" => "-07:00" - I believe that the lack of colon in the source data
is throwing it off and causing the ParseExact call to fail. I do not see
an obvious way to create a format string around a 4 digit timezone
specification without a colon.

A specific example that I hit was:
Mon, 22 Mar 2010 03:52:22 -0700

That fails to parse. If I manually in the debugger change it to:
Mon, 22 Mar 2010 03:52:22 -07:00

By inserting the colon, then it works fine.

Trying to parse the string myself to insert a colon feels like a "hack."
Anyone see an easy way out here that I'm missing?

using System;
using System.Globalization;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
string s = "Mon, 22 Mar 2010 03:52:22 -0700";
DateTime dt;
if(DateTime.TryParseExact(s, "ddd, dd MMM yyyy HH:mm:ss
zzz", new CultureInfo("en-US"),DateTimeStyles.AllowLeadingWhite |
DateTimeStyles.AllowTrailingWhite, out dt))
{
Console.WriteLine(dt);
}
else
{
Console.WriteLine("No luck");
}
Console.ReadKey();
}
}
}

seems to output a correct result here (.NET 3.5 on XP SP3).

Arne
 
A

Adam Clauss

seems to output a correct result here (.NET 3.5 on XP SP3).

Arne

Hrm... as it does for me (your small test app, still breaks in the
actual app). Which means something else is going on somewhere to break
it. Sigh.

But, that at least means it is in fact possible to parse it, I just need
to figure out what is confusing it.

Thanks,
Adam
 
A

Arne Vajhøj

Hrm... as it does for me (your small test app, still breaks in the
actual app). Which means something else is going on somewhere to break
it. Sigh.

But, that at least means it is in fact possible to parse it, I just need
to figure out what is confusing it.

What about culture?

Both day names and month names are cultural specific.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top