Sorting it all Out Michael Kaplan's random stuff of dubious value Be sure to read the disclaimer here first!
Back in May of 2004, someone asked the following question in the newsgroups:
I'm programming in ASP.NET. How could I convert my date to UK format ?Thanks in advance.
To which someone else suggested the following code:
DateTime x=DateTime.Now;string s=x.ToString(System.Globalization.CultureInfo.CreateSpecificCulture("uk").DateTimeFormat);
To his credit, this code was compiled and ran; in the end he was convinced he had the solution.
However, there are many problems with this code:
So why did the code run, you may skeptically be thinking? Though probably not if you thought about the title of this post!
Well, it has to with the overlap of ISO 639 (the two letter language codes) and ISO 3166 (the two letter region codes). Or in his case, using the wrong abbreviation in the wrong method and having it find some random region that happens to use the abbreviation (in this case the Ukranian neutral locale was being created).
This is kinda unavoidable since
See the following table for a list of the overlaps. The bad overlaps where you will get wrong results are marked in red.
Code
Language
Region
ar
Arabic
Argentina
az
Azeri
Azerbaijan
be
Belarusian
Belgium
bg
Bulgarian
Bulgaria
ca
Catalan
Canada
de
German
Germany
es
Spanish
Spain
fi
Finnish
Finland
fo
Faroese
Faroe Islands
fr
French
France
hr
Croatian
Croatia
hu
Hungarian
Hungary
id
Indonesian
Indonesia
is
Icelandic
Iceland
it
Italian
Italy
lt
Lithuanian
Lithuania
lv
Latvian
Latvia
mk
Macedonian
FYROM
mn
Mongolian
Mongolia
nl
Dutch
The Netherlands
no
Norwegian
Norway
pa
Punjabi
Panama
pl
Polish
Poland
pt
Portuguese
Portugal
ro
Romanian
Romania
ru
Russian
Russia
sa
Sanskrit
Saudi Arabia
sk
Slovak
Slovakia
sv
Swedish
El Salvador
th
Thai
Thailand
tr
Turkish
Turkey
tt
Tatar
Trinidad and Tobago
uz
Uzbek
Uzbekistan
For the full lists of cultures and regions, see the CultureInfo and RegionInfo help topics.
This very non-bug has been reported internally by people wondering why creating a RegionInfo from a neutral (region-less) culture like "AR" was succeeding, as they were not thinking about how when you pass it to a RegionInfo you are not getting "Arabic" you are getting "Argentina".
Of course, this is just a symptom about a larger problem that affects international testing in general, which I will be talking about in a future post. Think of this post as describing the tip of the iceberg. :-)
1 - In this specific case the method would quite literally return the wrong results even if you passed the right language associated with the UK -- en, because the default culture returned from CreateSpecificCulture would be en-US, not en-GB.
This post sponsored by "o", "ο", and "о" (U+006f, U+03bf, and U+043e; a.k.a. LATIN SMALL LETTER O; GREEK SMALL LETTER OMICRON; CYRILLIC SMALL LETTER O).
I got email from some people who would really like there to be an "alias" mechanism for locales.
And