I'll be brief to start, then give details at the end. Consider the following code:
CultureInfo cultureToTest = new CultureInfo("hu-HU");
Thread.CurrentThread.CurrentCulture = cultureToTest;
DateTime testDateTime = new DateTime(2014,12,13,23,24,25);
String testString = testDateTime.ToString(CultureInfo.InvariantCulture);
DateTime actualDateTime = DateTime.Parse(testString);
The question is whether there is any possible value of cultureToTest
which would either cause the DateTime.Parse
call to throw an exception or to return the wrong value?
Context:
This is for a set of unit tests. There is a body of code which calls DateTime.Parse
without specifying the culture. My concern was that when this code is passed a date in the Invariant or en-US cultures, that the code could fail in some cultures. My proposed solution was to change that code to use
DateTime.Parse(string, CultureInfo.InvariantCulture)
in these cases.
In order to unit test the change, I need to call the new code with a culture that would have made the original DateTime.Parse(string)
fail, then to show that the changed code will succeed.
The problem is that I haven't yet found a culture that will do this for me. I'm going to try to manufacture one, but thought I'd ask the more general question first.
Using this code:
The following cultures throw an exception when parsing that date: