[ Update : formatting specifiers are not the same as format strings; the format specifier is part of a custom format string, where the format string is βstockβ and does not provide customization. My problem is with qualifiers, not formats]
I am trying to do DateTime roundtrip conversions with a format string that uses the zzz format specifier, which, as I know, is bound to local time. So, if I try to get around a trip with a UTC date, it throws a DateTimeInvalidLocalFormat exception, which should be with this text:
A UTC DateTime is converted to text in a format that is correct only for local times. This can happen when DateTime.ToString is called using the format specifier "z", which will include the local time zone offset at the output. In this case, either use the format specifier "Z", which indicates the time UTC , or use the format string "o", which is the recommended way to save the DateTime in the text. This can also happen when passing a DateTime to serialize an XmlConvert or DataSet. If you are using XmlConvert.ToString, go to XmlDateTimeSerializationMode.RoundtripKind to serialize correctly. If you are using a DataSet, set the DateTimeMode in the DataColumn object to DataSetDateTime.Utc.
Based on this assumption, all I have to do to get my code to work is to replace "zzz" with "ZZZ" so that I can stand in UTC. The problem is that βZβ is not found anywhere in the documentation, and any combination of the βZβ format that I try, that is, βZβ, βZZβ, βZZZβ, always just converts the DateTime instance with those processed by Z as literals.
Someone forgot to implement "Z" without telling the author about the exception, or am I missing how to replace the current local time offset with "+0000" without hacking?
Code example:
// This is the format with 'zzzzz' representing local time offset const string format = "ddd MMM dd HH:mm:ss zzzzz yyyy"; // create a UTC time const string expected = "Fri Dec 19 17:24:18 +0000 2008"; var time = new DateTime(2008, 12, 19, 17, 24, 18, 0, DateTimeKind.Utc); // If you're using a debugger this will rightfully throw an exception // with .NET 3.5 SP1 because 'z' is for local time only; however, the exception // asks me to use the 'Z' specifier for UTC times, but it doesn't exist, so it // just spits out 'Z' as a literal. var actual = time.ToString(format, CultureInfo.InvariantCulture); Assert.AreEqual(expected, actual);