1

I am getting some junk data returned from a ToString() call on a DateTime object in C# and I'm afraid I'm stumped after poking around with it for a while.

The function is supposed to format dates to be compliant with RFC 822 (as required by the RSS spec) and looks like:

public static string FormatPubDate(DateTime pubDate) 
{
    string _rfc822Format = "ddd, dd MMM yyyy HH:mm:ss";
    string _tmp = pubDate.ToUniversalTime().ToString(_rfc822Format);

    return pubDate.ToString(_tmp + " UT");
}

Which should be what I want, from what I can read of the DateTime ToString() docs.

However, for some dates it's generating junk:

 Console.WriteLine(FormatPubDate(new DateTime(2008, 12, 16, 13, 44, 33)));
 Console.WriteLine(FormatPubDate(new DateTime(2008, 12, 17, 13, 44, 33)));
 Console.WriteLine(FormatPubDate(new DateTime(2009, 3, 18, 4, 17, 20)));
 Console.WriteLine(FormatPubDate(new DateTime(2009, 4, 30, 10, 44, 33)));

Yields:

Tue, 16 Dec 2008 19:44:33 UT
We17, 17 Dec 2008 19:44:33 UT
We18, 18 3ar 2009 09:17:20 UT
T10u, 30 Apr 2009 15:44:33 UT

Any ideas why it's returning We18 instead of Wed and 3ar instead of Mar?

Dana
  • 32,083
  • 17
  • 62
  • 73
  • If you take out the ToUniversalTime() call does it work? That could be altering the culture and format strings are culture specific. – John Sheehan Apr 30 '09 at 16:08
  • @John -- Nope. I tried that, as well as running my program on a couple of different machines. – Dana Apr 30 '09 at 16:09

3 Answers3

12

You're problem is the last

return pubDate.ToString(_tmp + " UT");

You're doing a second ToString() on the DateTime with the formatted value, as the formatter...

Try changing it to

string _rfc822Format = "ddd, dd MMM yyyy HH:mm:ss";
string _tmp = pubDate.ToUniversalTime().ToString(_rfc822Format);

return _tmp + " UT";
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
  • 1
    Aww, man! I guess this my morning to display my doofusness on the Internet :P – Dana Apr 30 '09 at 16:14
  • haha... that's pretty much 9am-10am for me... every morning ;) – Eoin Campbell Apr 30 '09 at 16:16
  • Lol. Why didn't I spot that one? You can add the UT in the format also, so that you don't need the extra concatentation: "ddd, dd MMM yyyy HH:mm:ss' UT'". – Guffa Apr 30 '09 at 16:16
3

Can you use this instead?:

String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • Good point. The other posters are correct in identifying the extra ToString() call in your original posted code... – Mitch Wheat Apr 30 '09 at 16:14
  • Yeah, your formatting is perfect for what I need, and Eoin pointed out my dumb mistake. – Dana Apr 30 '09 at 16:15
3

You are currently calling ToString on the date, passing in your RFC format, then you are calling ToString on the date again, apssing in your already converted date + "UT" as the format, I'm suprised you get anything good coming out!

Try this:

public static string FormatPubDate(DateTime pubDate) 
{
    string _rfc822Format = "ddd, dd MMM yyyy HH:mm:ss";
    return pubDate.ToUniversalTime().ToString(_rfc822Format) + " UT";

}
cjk
  • 45,739
  • 9
  • 81
  • 112