Cook Computing

ParseExact

October 31, 2004 Written by Charles Cook

Many thanks to Mario Orlandi of Brainstorm who pointed out a bug in the parsing of dateTime.iso8601 values by XML-RPC.NET. The faulty code was:


retVal = DateTime.ParseExact(s, "yyyyMMddTHH:mm:ss", null);
The literals should be surrounded by single quotes:
retVal = DateTime.ParseExact(s, "yyyyMMdd'T'HH':'mm':'ss", null);

Before making the fix I added a new unit test to check dateTime parsing, using the list of locales supported by .NET on the MSDN page for the CultureInfo class. The original code threw an exception for it-IT (Italian - Italy) and fa-FO (Faroese - Faroe Islands) which limited the damage, maybe more so in the latter case, but unfortunately the fixed code still fails for the following locales:

  • ar-SA (Arabic - Saudi Arabia)
  • div-MV (Dhivehi - Maldives)
  • th-TH (Thai - Thailand)

For these locales, no exception is thrown but the DateTime value returned is incorrect. Is the problem in my understanding of format strings or in implementation of ParseExact? I need to do some more research.

By the way, I had a shock when scrolling through the list of locales on the MSDN page mentioned above. I caught sight of eu and for a moment had visions of a EU wide locale which we would all be forced to use at some point by yet another of the endless stream of directives from the EU Commission. Coming shortly after the shameful signing by Tony Blair of the "European Consititution" before holding a referendum on the issue, my anti-EU sensibilities must have been in a rather heightened state of awareness but I need not have worried in this case. It turns out that eu is the code for Basque.