[corlib] Assume UTC if no $TZ set. Fixes #30360
[mono.git] / mcs / class / corlib / System.Globalization / CultureInfo.cs
1 //
2 // System.Globalization.CultureInfo.cs
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Dick Porter (dick@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
8 //
9 // (C) 2001, 2002, 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Collections.Generic;
34 using System.Threading;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics.Contracts;
38
39 namespace System.Globalization
40 {
41         [System.Runtime.InteropServices.ComVisible (true)]
42         [Serializable]
43         [StructLayout (LayoutKind.Sequential)]
44         public class CultureInfo : ICloneable, IFormatProvider
45         {
46                 static volatile CultureInfo invariant_culture_info = new CultureInfo (InvariantCultureId, false, true);
47                 static object shared_table_lock = new object ();
48                 static CultureInfo default_current_culture;
49
50 #pragma warning disable 169, 649
51                 bool m_isReadOnly;
52                 int  cultureID;
53                 [NonSerialized]
54                 int parent_lcid;
55                 [NonSerialized]
56                 int datetime_index;
57                 [NonSerialized]
58                 int number_index;
59                 [NonSerialized]
60                 int default_calendar_type;
61                 bool m_useUserOverride;
62                 [NonSerialized]
63                 internal volatile NumberFormatInfo numInfo;
64                 internal volatile DateTimeFormatInfo dateTimeInfo;
65                 volatile TextInfo textInfo;
66                 internal string m_name;
67                 
68                 [NonSerialized]
69                 private string englishname;
70                 [NonSerialized]
71                 private string nativename;
72                 [NonSerialized]
73                 private string iso3lang;
74                 [NonSerialized]
75                 private string iso2lang;
76                 [NonSerialized]
77                 private string win3lang;
78                 [NonSerialized]
79                 private string territory;
80                 [NonSerialized]
81                 string[] native_calendar_names;
82
83                 volatile CompareInfo compareInfo;
84                 [NonSerialized]
85                 private unsafe readonly void *textinfo_data;
86
87                 [StructLayout (LayoutKind.Sequential)]
88                 struct Data {
89                         public int ansi;
90                         public int ebcdic;
91                         public int mac;
92                         public int oem;
93                         public bool right_to_left;
94                         public byte list_sep;
95                 }
96
97                 int m_dataItem;         // MS.NET serializes this.
98 #pragma warning restore 169, 649
99
100                 Calendar calendar;
101
102                 [NonSerialized]
103                 CultureInfo parent_culture;
104
105                 // Deserialized instances will set this to false
106                 [NonSerialized]
107                 bool constructed;
108
109                 [NonSerialized]
110                 // Used by Thread.set_CurrentCulture
111                 internal byte[] cached_serialized_form;
112
113                 [NonSerialized]internal CultureData m_cultureData;
114                 [NonSerialized]internal bool m_isInherited;
115                 
116                 internal const int InvariantCultureId = 0x7F;
117                 const int CalendarTypeBits = 8;
118
119                 const string MSG_READONLY = "This instance is read only";
120                 
121                 public static CultureInfo InvariantCulture {
122                         get {
123                                 return invariant_culture_info;
124                         }
125                 }
126
127                 public static CultureInfo CurrentCulture {
128                         get {
129                                 return Thread.CurrentThread.CurrentCulture;
130                         }
131                 }
132
133                 public static CultureInfo CurrentUICulture { 
134                         get {
135                                 return Thread.CurrentThread.CurrentUICulture;
136                         }
137                 }
138
139                 internal static CultureInfo ConstructCurrentCulture ()
140                 {
141                         if (default_current_culture != null)
142                                 return default_current_culture;
143
144                         var locale_name = get_current_locale_name ();
145                         CultureInfo ci = null;
146                         try {
147                                 ci = CreateSpecificCulture (locale_name);
148                         } catch {
149                         }
150
151                         if (ci == null) {
152                                 ci = InvariantCulture;
153                         } else {
154                                 ci.m_isReadOnly = true;
155                                 ci.m_useUserOverride = true;
156                         }
157
158                         default_current_culture = ci;
159                         return ci;
160                 }
161
162                 internal static CultureInfo ConstructCurrentUICulture ()
163                 {
164                         return ConstructCurrentCulture ();
165                 }
166
167                 // it is used for RegionInfo.
168                 internal string Territory {
169                         get { return territory; }
170                 }
171
172 #if !NET_2_1
173                 // FIXME: It is implemented, but would be hell slow.
174                 [ComVisible (false)]
175                 public CultureTypes CultureTypes {
176                         get {
177                                 CultureTypes ret = (CultureTypes) 0;
178                                 foreach (CultureTypes v in Enum.GetValues (typeof (CultureTypes)))
179                                         if (Array.IndexOf (GetCultures (v), this) >= 0)
180                                                 ret |= v;
181                                 return ret;
182                         }
183                 }
184
185                 [ComVisible (false)]
186                 public CultureInfo GetConsoleFallbackUICulture ()
187                 {
188                         // as documented in MSDN ...
189                         switch (Name) {
190                         case "ar": case "ar-BH": case "ar-EG": case "ar-IQ":
191                         case "ar-JO": case "ar-KW": case "ar-LB": case "ar-LY":
192                         case "ar-QA": case "ar-SA": case "ar-SY": case "ar-AE":
193                         case "ar-YE":
194                         case "dv": case "dv-MV":
195                         case "fa": case "fa-IR":
196                         case "gu": case "gu-IN":
197                         case "he": case "he-IL":
198                         case "hi": case "hi-IN":
199                         case "kn": case "kn-IN":
200                         case "kok": case "kok-IN":
201                         case "mr": case "mr-IN":
202                         case "pa": case "pa-IN":
203                         case "sa": case "sa-IN":
204                         case "syr": case "syr-SY":
205                         case "ta": case "ta-IN":
206                         case "te": case "te-IN":
207                         case "th": case "th-TH":
208                         case "ur": case "ur-PK":
209                         case "vi": case "vi-VN":
210                                 return GetCultureInfo ("en");
211                         case "ar-DZ": case "ar-MA": case "ar-TN":
212                                 return GetCultureInfo ("fr");
213                         }
214                         return (CultureTypes & CultureTypes.WindowsOnlyCultures) != 0 ? CultureInfo.InvariantCulture : this;
215                 }
216
217                 [ComVisible (false)]
218                 public string IetfLanguageTag {
219                         // There could be more consistent way to implement
220                         // it, but right now it works just fine with this...
221                         get {
222                                 switch (Name) {
223                                 case "zh-CHS":
224                                         return "zh-Hans";
225                                 case "zh-CHT":
226                                         return "zh-Hant";
227                                 default:
228                                         return Name;
229                                 }
230                         }
231                 }
232
233                 // For specific cultures it basically returns LCID.
234                 // For neutral cultures it is mapped to the default(?) specific
235                 // culture, where the LCID of the specific culture seems to be
236                 // n + 1024 by default. zh-CHS is the only exception which is 
237                 // mapped to 2052, not 1028 (zh-CHT is mapped to 1028 instead).
238                 // There are very few exceptions, here I simply list them here.
239                 // It is Windows-specific property anyways, so no worthy of
240                 // trying to do some complex things with locale-builder.
241                 [ComVisible (false)]
242                 public virtual int KeyboardLayoutId {
243                         get {
244                                 switch (LCID) {
245                                 case 4: // zh-CHS (neutral)
246                                         return 2052;
247                                 case 1034: // es-ES Spanish 2
248                                         return 3082;
249                                 case 31748: // zh-CHT (neutral)
250                                         return 1028;
251                                 case 31770: // sr (neutral)
252                                         return 2074;
253                                 default:
254                                         return LCID < 1024 ? LCID + 1024 : LCID;
255                                 }
256                         }
257                 }
258 #endif
259
260                 public virtual int LCID {
261                         get {
262                                 return cultureID;
263                         }
264                 }
265
266                 public virtual string Name {
267                         get {
268                                 return(m_name);
269                         }
270                 }
271
272                 public virtual string NativeName {
273                         get {
274                                 if (!constructed) Construct ();
275                                 return nativename;
276                         }
277                 }
278
279                 internal string NativeCalendarName {
280                         get {
281                                 if (!constructed) Construct ();
282                                 return native_calendar_names[(default_calendar_type >> CalendarTypeBits) - 1];
283                         }
284                 }
285                 
286                 public virtual Calendar Calendar {
287                         get {
288                                 if (calendar == null) {
289                                         if (!constructed) Construct ();
290                                         calendar = CreateCalendar (default_calendar_type);
291                                 }
292
293                                 return calendar;
294                         }
295                 }
296
297                 [MonoLimitation ("Optional calendars are not supported only default calendar is returned")]
298                 public virtual Calendar[] OptionalCalendars {
299                         get {
300                                 return new[] { Calendar };
301                         }
302                 }
303
304                 public virtual CultureInfo Parent
305                 {
306                         get {
307                                 if (parent_culture == null) {
308                                         if (!constructed)
309                                                 Construct ();
310                                         if (parent_lcid == cultureID) {
311                                                 //
312                                                 // Parent lcid is same but culture info is not for legacy zh culture
313                                                 //
314                                                 if (parent_lcid == 0x7C04 && EnglishName [EnglishName.Length - 1] == 'y')
315                                                         return parent_culture = new CultureInfo ("zh-Hant");
316                                                 else if (parent_lcid == 0x0004 && EnglishName [EnglishName.Length -1] == 'y')
317                                                         return parent_culture = new CultureInfo ("zh-Hans");
318                                                 return null;
319                                         }
320                                         
321                                         if (parent_lcid == InvariantCultureId)
322                                                 parent_culture = InvariantCulture;
323                                         else if (cultureID == InvariantCultureId)
324                                                 parent_culture = this;
325                                         else if (cultureID == 0x0404) {
326                                                 // zh-tw has parent id 0x7C04 which is in this case zh-cht and not zh-hant
327                                                 parent_culture = new CultureInfo ("zh-CHT");
328                                         } else
329                                                 parent_culture = new CultureInfo (parent_lcid);
330                                 }
331                                 return parent_culture;
332                         }
333                 }
334
335                 public virtual TextInfo TextInfo
336                 {
337                         get {
338                                 if (textInfo == null) {
339                                         if (!constructed) Construct ();
340                                         lock (this) {
341                                                 if(textInfo == null) {
342                                                         textInfo = CreateTextInfo (m_isReadOnly);
343                                                 }
344                                         }
345                                 }
346                                 
347                                 return(textInfo);
348                         }
349                 }
350
351                 public virtual string ThreeLetterISOLanguageName {
352                         get {
353                                 if (!constructed) Construct ();
354                                 return iso3lang;
355                         }
356                 }
357
358                 public virtual string ThreeLetterWindowsLanguageName
359                 {
360                         get {
361                                 if (!constructed) Construct ();
362                                 return(win3lang);
363                         }
364                 }
365
366                 public virtual string TwoLetterISOLanguageName {
367                         get {
368                                 if (!constructed) Construct ();
369                                 return(iso2lang);
370                         }
371                 }
372
373                 public bool UseUserOverride
374                 {
375                         get {
376                                 return m_useUserOverride;
377                         }
378                 }
379
380                 public void ClearCachedData()
381                 {
382                         lock (shared_table_lock) {
383                                 shared_by_number = null;
384                                 shared_by_name = null;
385                         }
386
387                         //
388                         // ClearCachedData method does not refresh the information in
389                         // the Thread.CurrentCulture property for existing threads
390                         //
391                         default_current_culture = null;
392
393                         RegionInfo.ClearCachedData ();
394                         TimeZone.ClearCachedData ();
395                         TimeZoneInfo.ClearCachedData ();
396                 }
397
398                 public virtual object Clone()
399                 {
400                         if (!constructed) Construct ();
401                         CultureInfo ci=(CultureInfo)MemberwiseClone ();
402                         ci.m_isReadOnly=false;
403                         ci.cached_serialized_form=null;
404                         if (!IsNeutralCulture) {
405                                 ci.NumberFormat = (NumberFormatInfo)NumberFormat.Clone ();
406                                 ci.DateTimeFormat = (DateTimeFormatInfo)DateTimeFormat.Clone ();
407                         }
408                         return(ci);
409                 }
410
411                 public override bool Equals (object value)
412                 {
413                         CultureInfo b = value as CultureInfo;
414                         
415                         if (b != null)
416                                 return b.cultureID == cultureID;
417                         return false;
418                 }
419
420                 public static CultureInfo[] GetCultures(CultureTypes types)
421                 {
422                         bool neutral=((types & CultureTypes.NeutralCultures)!=0);
423                         bool specific=((types & CultureTypes.SpecificCultures)!=0);
424                         bool installed=((types & CultureTypes.InstalledWin32Cultures)!=0);  // TODO
425
426                         CultureInfo [] infos = internal_get_cultures (neutral, specific, installed);
427                         // The runtime returns a NULL in the first position of the array when
428                         // 'neutral' is true. We fill it in with a clone of InvariantCulture
429                         // since it must not be read-only
430                         int i = 0;
431                         if (neutral && infos.Length > 0 && infos [0] == null) {
432                                 infos [i++] = (CultureInfo) InvariantCulture.Clone ();
433                         }
434
435                         for (; i < infos.Length; ++i) {
436                                 var ci = infos [i];
437                                 var ti = ci.GetTextInfoData ();
438                                 infos [i].m_cultureData = CultureData.GetCultureData (ci.m_name, false, ci.datetime_index, ci.CalendarType, ci.number_index, ci.iso2lang,
439                                         ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
440                         }
441
442                         return infos;
443                 }
444
445                 unsafe Data GetTextInfoData ()
446                 {
447                         return *(Data*) textinfo_data;
448                 }
449
450                 public override int GetHashCode ()
451                 {
452                         return cultureID.GetHashCode ();
453                 }
454
455                 public static CultureInfo ReadOnly(CultureInfo ci)
456                 {
457                         if(ci==null) {
458                                 throw new ArgumentNullException("ci");
459                         }
460
461                         if(ci.m_isReadOnly) {
462                                 return(ci);
463                         } else {
464                                 CultureInfo new_ci=(CultureInfo)ci.Clone ();
465                                 new_ci.m_isReadOnly=true;
466                                 if (new_ci.numInfo != null)
467                                         new_ci.numInfo = NumberFormatInfo.ReadOnly (new_ci.numInfo);
468                                 if (new_ci.dateTimeInfo != null)
469                                         new_ci.dateTimeInfo = DateTimeFormatInfo.ReadOnly (new_ci.dateTimeInfo);
470                                 if (new_ci.textInfo != null)
471                                         new_ci.textInfo = TextInfo.ReadOnly (new_ci.textInfo);
472                                 return(new_ci);
473                         }
474                 }
475
476                 public override string ToString()
477                 {
478                         return(m_name);
479                 }
480                 
481                 public virtual CompareInfo CompareInfo
482                 {
483                         get {
484                                 if(compareInfo==null) {
485                                         if (!constructed)
486                                                 Construct ();
487
488                                         lock (this) {
489                                                 if(compareInfo==null) {
490                                                         compareInfo=new CompareInfo (this);
491                                                 }
492                                         }
493                                 }
494                                 
495                                 return(compareInfo);
496                         }
497                 }
498
499                 public virtual bool IsNeutralCulture {
500                         get {
501                                 if (cultureID == InvariantCultureId)
502                                         return false;
503
504                                 if (!constructed) Construct ();
505                                 return territory == null;
506                         }
507                 }
508
509                 internal void CheckNeutral ()
510                 {
511                 }
512
513                 public virtual NumberFormatInfo NumberFormat {
514                         get {
515                                 if (numInfo == null) {
516                                         NumberFormatInfo temp = new NumberFormatInfo(this.m_cultureData);
517                                         temp.isReadOnly = m_isReadOnly;
518                                         numInfo = temp;
519                                 }
520
521                                 return numInfo;
522                         }
523
524                         set {
525                                 if (!constructed) Construct ();
526                                 if (m_isReadOnly) throw new InvalidOperationException(MSG_READONLY);
527
528                                 if (value == null)
529                                         throw new ArgumentNullException ("NumberFormat");
530                                 
531                                 numInfo = value;
532                         }
533                 }
534
535                 public virtual DateTimeFormatInfo DateTimeFormat {
536                         get {
537                                 if (dateTimeInfo != null)
538                                         return dateTimeInfo;
539
540                                 if (!constructed) Construct ();
541                                 CheckNeutral ();
542
543                                 var temp = new DateTimeFormatInfo (m_cultureData, Calendar);
544                                 temp.m_isReadOnly = m_isReadOnly;
545                                 System.Threading.Thread.MemoryBarrier();
546                                 dateTimeInfo = temp;
547                                 return dateTimeInfo;
548                         }
549
550                         set {
551                                 if (!constructed) Construct ();
552                                 if (m_isReadOnly) throw new InvalidOperationException(MSG_READONLY);
553
554                                 if (value == null)
555                                         throw new ArgumentNullException ("DateTimeFormat");
556                                 
557                                 dateTimeInfo = value;
558                         }
559                 }
560
561                 public virtual string DisplayName {
562                         get {
563                                 // Mono is not localized and will always return english name regardless of OS locale
564                                 return EnglishName;
565                         }
566                 }
567
568                 public virtual string EnglishName {
569                         get {
570                                 if (!constructed) Construct ();
571                                 return englishname;
572                         }
573                 }
574
575                 public static CultureInfo InstalledUICulture {
576                         get {
577                                 return ConstructCurrentCulture ();
578                         }
579                 }
580
581                 public bool IsReadOnly {
582                         get {
583                                 return m_isReadOnly;
584                         }
585                 }
586                 
587
588                 // 
589                 // IFormatProvider implementation
590                 //
591                 public virtual object GetFormat( Type formatType )
592                 {
593                         object format = null;
594
595                         if ( formatType == typeof(NumberFormatInfo) )
596                                 format = NumberFormat;
597                         else if ( formatType == typeof(DateTimeFormatInfo) )
598                                 format = DateTimeFormat;
599                         
600                         return format;
601                 }
602                 
603                 void Construct ()
604                 {
605                         construct_internal_locale_from_lcid (cultureID);
606                         constructed = true;
607                 }
608
609                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
610                 private extern bool construct_internal_locale_from_lcid (int lcid);
611
612                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
613                 private extern bool construct_internal_locale_from_name (string name);
614
615                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
616                 private extern static string get_current_locale_name ();
617
618                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
619                 private extern static CultureInfo [] internal_get_cultures (bool neutral, bool specific, bool installed);
620
621                 private void ConstructInvariant (bool read_only)
622                 {
623                         cultureID = InvariantCultureId;
624
625                         /* NumberFormatInfo defaults to the invariant data */
626                         numInfo=NumberFormatInfo.InvariantInfo;
627
628                         if (!read_only) {
629                                 numInfo = (NumberFormatInfo) numInfo.Clone ();
630                         }
631
632                         textInfo = CreateTextInfo (read_only);
633
634                         m_name=String.Empty;
635                         englishname=
636                         nativename="Invariant Language (Invariant Country)";
637                         iso3lang="IVL";
638                         iso2lang="iv";
639                         win3lang="IVL";
640                         default_calendar_type = 1 << CalendarTypeBits | (int) GregorianCalendarTypes.Localized;
641                 }
642
643                 private unsafe TextInfo CreateTextInfo (bool readOnly)
644                 {
645                         TextInfo tempTextInfo = new TextInfo (this.m_cultureData);
646                         tempTextInfo.SetReadOnlyState (readOnly);
647                         return tempTextInfo;
648                 }
649
650                 public CultureInfo (int culture) : this (culture, true) {}
651
652                 public CultureInfo (int culture, bool useUserOverride) :
653                         this (culture, useUserOverride, false) {}
654
655                 private CultureInfo (int culture, bool useUserOverride, bool read_only)
656                 {
657                         if (culture < 0)
658                                 throw new ArgumentOutOfRangeException ("culture", "Positive "
659                                         + "number required.");
660
661                         constructed = true;
662                         m_isReadOnly = read_only;
663                         m_useUserOverride = useUserOverride;
664
665                         if (culture == InvariantCultureId) {
666                                 /* Short circuit the invariant culture */
667                                 m_cultureData = CultureData.Invariant;
668                                 ConstructInvariant (read_only);
669                                 return;
670                         }
671
672                         if (!construct_internal_locale_from_lcid (culture)) {
673                                 //
674                                 // Be careful not to cause recursive CultureInfo initialization
675                                 //
676                                 var msg = string.Format (InvariantCulture, "Culture ID {0} (0x{1}) is not a supported culture.", culture.ToString (InvariantCulture), culture.ToString ("X4", InvariantCulture));
677                                 throw new CultureNotFoundException ("culture", msg);
678                         }
679
680                         var ti = GetTextInfoData ();
681                         m_cultureData = CultureData.GetCultureData (m_name, m_useUserOverride, datetime_index, CalendarType, number_index, iso2lang,
682                                 ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
683                 }
684
685                 public CultureInfo (string name) : this (name, true) {}
686
687                 public CultureInfo (string name, bool useUserOverride) :
688                         this (name, useUserOverride, false) {}
689
690                 private CultureInfo (string name, bool useUserOverride, bool read_only)
691                 {
692                         if (name == null)
693                                 throw new ArgumentNullException ("name");
694
695                         constructed = true;
696                         m_isReadOnly = read_only;
697                         m_useUserOverride = useUserOverride;
698                         m_isInherited = GetType() != typeof(System.Globalization.CultureInfo);
699
700                         if (name.Length == 0) {
701                                 /* Short circuit the invariant culture */
702                                 m_cultureData = CultureData.Invariant;
703                                 ConstructInvariant (read_only);
704                                 return;
705                         }
706
707                         if (!construct_internal_locale_from_name (name.ToLowerInvariant ())) {
708                                 throw CreateNotFoundException (name);
709                         }
710
711                         var ti = GetTextInfoData ();
712                         m_cultureData = CultureData.GetCultureData (m_name, useUserOverride, datetime_index, CalendarType, number_index, iso2lang,
713                                 ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
714                 }
715
716                 // This is used when creating by specific name and creating by
717                 // current locale so we can initialize the object without
718                 // doing any member initialization
719                 private CultureInfo () { constructed = true; }
720                 static Dictionary<int, CultureInfo> shared_by_number;
721                 static Dictionary<string, CultureInfo> shared_by_name;
722                 
723                 static void insert_into_shared_tables (CultureInfo c)
724                 {
725                         if (shared_by_number == null){
726                                 shared_by_number = new Dictionary<int, CultureInfo> ();
727                                 shared_by_name = new Dictionary<string, CultureInfo> ();
728                         }
729                         shared_by_number [c.cultureID] = c;
730                         shared_by_name [c.m_name] = c;
731                 }
732                 
733                 public static CultureInfo GetCultureInfo (int culture)
734                 {
735                         CultureInfo c;
736                         
737                         lock (shared_table_lock){
738                                 if (shared_by_number != null) {
739                                         if (shared_by_number.TryGetValue (culture, out c))
740                                                 return c;
741                                 }
742
743                                 c = new CultureInfo (culture, false, true);
744                                 insert_into_shared_tables (c);
745                                 return c;
746                         }
747                 }
748
749                 public static CultureInfo GetCultureInfo (string name)
750                 {
751                         if (name == null)
752                                 throw new ArgumentNullException ("name");
753
754                         CultureInfo c;
755                         lock (shared_table_lock){
756                                 if (shared_by_name != null){
757                                         if (shared_by_name.TryGetValue (name, out c))
758                                                 return c;
759                                 }
760                                 c = new CultureInfo (name, false, true);
761                                 insert_into_shared_tables (c);
762                                 return c;
763                         }
764                 }
765
766                 [MonoTODO ("Currently it ignores the altName parameter")]
767                 public static CultureInfo GetCultureInfo (string name, string altName) {
768                         if (name == null)
769                                 throw new ArgumentNullException ("null");
770                         if (altName == null)
771                                 throw new ArgumentNullException ("null");
772
773                         return GetCultureInfo (name);
774                 }
775
776                 public static CultureInfo GetCultureInfoByIetfLanguageTag (string name)
777                 {
778                         // There could be more consistent way to implement
779                         // it, but right now it works just fine with this...
780                         switch (name) {
781                         case "zh-Hans":
782                                 return GetCultureInfo ("zh-CHS");
783                         case "zh-Hant":
784                                 return GetCultureInfo ("zh-CHT");
785                         default:
786                                 return GetCultureInfo (name);
787                         }
788                 }
789
790                 // used in runtime (icall.c) to construct CultureInfo for
791                 // AssemblyName of assemblies
792                 internal static CultureInfo CreateCulture (string name, bool reference)
793                 {
794                         bool read_only;
795                         bool use_user_override;
796
797                         bool invariant = name.Length == 0;
798                         if (reference) {
799                                 use_user_override = invariant ? false : true;
800                                 read_only = false;
801                         } else {
802                                 read_only = false;
803                                 use_user_override = invariant ? false : true;
804                         }
805
806                         return new CultureInfo (name, use_user_override, read_only);
807                 }
808
809                 public static CultureInfo CreateSpecificCulture (string name)
810                 {
811                         if (name == null)
812                                 throw new ArgumentNullException ("name");
813
814                         if (name.Length == 0)
815                                 return InvariantCulture;
816
817                         var src_name = name;
818                         name = name.ToLowerInvariant ();
819                         CultureInfo ci = new CultureInfo ();
820
821                         if (!ci.construct_internal_locale_from_name (name)) {
822                                 int idx = name.IndexOf ('-');
823                                 if (idx < 1 || !ci.construct_internal_locale_from_name (name.Substring (0, idx)))
824                                         throw CreateNotFoundException (src_name);
825                         }
826
827                         if (ci.IsNeutralCulture)
828                                 ci = CreateSpecificCultureFromNeutral (ci.Name);
829
830                         var ti = ci.GetTextInfoData ();
831
832                         ci.m_cultureData = CultureData.GetCultureData (ci.m_name, false, ci.datetime_index, ci.CalendarType, ci.number_index, ci.iso2lang,
833                                 ti.ansi, ti.oem, ti.mac, ti.ebcdic, ti.right_to_left, ((char)ti.list_sep).ToString ());
834                         return ci;
835                 }
836
837                 //
838                 // Creates specific culture from neutral culture. Used by CreateSpecificCulture
839                 // only but using separate method we can delay switch underlying Dictionary
840                 // initialization
841                 //
842                 static CultureInfo CreateSpecificCultureFromNeutral (string name)
843                 {
844                         int id;
845
846                         //
847                         // For neutral cultures find predefined default specific culture
848                         //
849                         // Use managed switch because we need this for only some cultures
850                         // and the method is not used frequently
851                         //
852                         // TODO: We could optimize for cultures with single specific culture 
853                         //
854                         switch (name.ToLowerInvariant ()) {
855                         case "af": id = 1078; break;
856                         case "am": id = 1118; break;
857                         case "ar": id = 1025; break;
858                         case "arn": id = 1146; break;
859                         case "as": id = 1101; break;
860                         case "az": id = 1068; break;
861                         case "az-cyrl": id = 2092; break;
862                         case "az-latn": id = 1068; break;
863                         case "ba": id = 1133; break;
864                         case "be": id = 1059; break;
865                         case "bg": id = 1026; break;
866                         case "bn": id = 1093; break;
867                         case "bo": id = 1105; break;
868                         case "br": id = 1150; break;
869                         case "bs": id = 5146; break;
870                         case "bs-cyrl": id = 8218; break;
871                         case "bs-latn": id = 5146; break;
872                         case "ca": id = 1027; break;
873                         case "co": id = 1155; break;
874                         case "cs": id = 1029; break;
875                         case "cy": id = 1106; break;
876                         case "da": id = 1030; break;
877                         case "de": id = 1031; break;
878                         case "dsb": id = 2094; break;
879                         case "dv": id = 1125; break;
880                         case "el": id = 1032; break;
881                         case "en": id = 1033; break;
882                         case "es": id = 3082; break;
883                         case "et": id = 1061; break;
884                         case "eu": id = 1069; break;
885                         case "fa": id = 1065; break;
886                         case "fi": id = 1035; break;
887                         case "fil": id = 1124; break;
888                         case "fo": id = 1080; break;
889                         case "fr": id = 1036; break;
890                         case "fy": id = 1122; break;
891                         case "ga": id = 2108; break;
892                         case "gd": id = 1169; break;
893                         case "gl": id = 1110; break;
894                         case "gsw": id = 1156; break;
895                         case "gu": id = 1095; break;
896                         case "ha": id = 1128; break;
897                         case "ha-latn": id = 1128; break;
898                         case "he": id = 1037; break;
899                         case "hi": id = 1081; break;
900                         case "hr": id = 1050; break;
901                         case "hsb": id = 1070; break;
902                         case "hu": id = 1038; break;
903                         case "hy": id = 1067; break;
904                         case "id": id = 1057; break;
905                         case "ig": id = 1136; break;
906                         case "ii": id = 1144; break;
907                         case "is": id = 1039; break;
908                         case "it": id = 1040; break;
909                         case "iu": id = 2141; break;
910                         case "iu-cans": id = 1117; break;
911                         case "iu-latn": id = 2141; break;
912                         case "ja": id = 1041; break;
913                         case "ka": id = 1079; break;
914                         case "kk": id = 1087; break;
915                         case "kl": id = 1135; break;
916                         case "km": id = 1107; break;
917                         case "kn": id = 1099; break;
918                         case "ko": id = 1042; break;
919                         case "kok": id = 1111; break;
920                         case "ky": id = 1088; break;
921                         case "lb": id = 1134; break;
922                         case "lo": id = 1108; break;
923                         case "lt": id = 1063; break;
924                         case "lv": id = 1062; break;
925                         case "mi": id = 1153; break;
926                         case "mk": id = 1071; break;
927                         case "ml": id = 1100; break;
928                         case "mn": id = 1104; break;
929                         case "mn-cyrl": id = 1104; break;
930                         case "mn-mong": id = 2128; break;
931                         case "moh": id = 1148; break;
932                         case "mr": id = 1102; break;
933                         case "ms": id = 1086; break;
934                         case "mt": id = 1082; break;
935                         case "nb": id = 1044; break;
936                         case "ne": id = 1121; break;
937                         case "nl": id = 1043; break;
938                         case "nn": id = 2068; break;
939                         case "no": id = 1044; break;
940                         case "nso": id = 1132; break;
941                         case "oc": id = 1154; break;
942                         case "or": id = 1096; break;
943                         case "pa": id = 1094; break;
944                         case "pl": id = 1045; break;
945                         case "prs": id = 1164; break;
946                         case "ps": id = 1123; break;
947                         case "pt": id = 1046; break;
948                         case "qut": id = 1158; break;
949                         case "quz": id = 1131; break;
950                         case "rm": id = 1047; break;
951                         case "ro": id = 1048; break;
952                         case "ru": id = 1049; break;
953                         case "rw": id = 1159; break;
954                         case "sa": id = 1103; break;
955                         case "sah": id = 1157; break;
956                         case "se": id = 1083; break;
957                         case "si": id = 1115; break;
958                         case "sk": id = 1051; break;
959                         case "sl": id = 1060; break;
960                         case "sma": id = 7227; break;
961                         case "smj": id = 5179; break;
962                         case "smn": id = 9275; break;
963                         case "sms": id = 8251; break;
964                         case "sq": id = 1052; break;
965                         case "sr": id = 9242; break;
966                         case "sr-cyrl": id = 10266; break;
967                         case "sr-latn": id = 9242; break;
968                         case "sv": id = 1053; break;
969                         case "sw": id = 1089; break;
970                         case "syr": id = 1114; break;
971                         case "ta": id = 1097; break;
972                         case "te": id = 1098; break;
973                         case "tg": id = 1064; break;
974                         case "tg-cyrl": id = 1064; break;
975                         case "th": id = 1054; break;
976                         case "tk": id = 1090; break;
977                         case "tn": id = 1074; break;
978                         case "tr": id = 1055; break;
979                         case "tt": id = 1092; break;
980                         case "tzm": id = 2143; break;
981                         case "tzm-latn": id = 2143; break;
982                         case "ug": id = 1152; break;
983                         case "uk": id = 1058; break;
984                         case "ur": id = 1056; break;
985                         case "uz": id = 1091; break;
986                         case "uz-cyrl": id = 2115; break;
987                         case "uz-latn": id = 1091; break;
988                         case "vi": id = 1066; break;
989                         case "wo": id = 1160; break;
990                         case "xh": id = 1076; break;
991                         case "yo": id = 1130; break;
992                         case "zh": id = 2052; break;
993                         case "zh-chs":
994                         case "zh-hans":
995                                 id = 2052; break;
996                         case "zh-cht":
997                         case "zh-hant":
998                                 id = 3076; break;
999                         case "zu": id = 1077; break;
1000                         default:
1001                                 throw new NotImplementedException ("Mapping for neutral culture " + name);
1002                         }
1003
1004                         return new CultureInfo (id);
1005                 }
1006
1007                 internal int CalendarType {
1008                         get {
1009                                 switch (default_calendar_type >> CalendarTypeBits) {
1010                                 case 1:
1011                                         return Calendar.CAL_GREGORIAN;
1012                                 case 2:
1013                                         return Calendar.CAL_THAI;
1014                                 case 3:
1015                                         return Calendar.CAL_UMALQURA;
1016                                 case 4:
1017                                         return Calendar.CAL_HIJRI;
1018                                 default:
1019                                         throw new NotImplementedException ("CalendarType");
1020                                 }
1021                         }
1022                 }
1023
1024                 static Calendar CreateCalendar (int calendarType)
1025                 {
1026                         string name = null;
1027                         switch (calendarType >> CalendarTypeBits) {
1028                         case 1:
1029                                 GregorianCalendarTypes greg_type;
1030                                 greg_type = (GregorianCalendarTypes) (calendarType & 0xFF);
1031                                 return new GregorianCalendar (greg_type);
1032                         case 2:
1033                                 name = "System.Globalization.ThaiBuddhistCalendar";
1034                                 break;
1035                         case 3:
1036                                 name = "System.Globalization.UmAlQuraCalendar";
1037                                 break;
1038                         case 4:
1039                                 name = "System.Globalization.HijriCalendar";
1040                                 break;
1041                         default:
1042                                 throw new NotImplementedException ("Unknown calendar type: " + calendarType);
1043                         }
1044
1045                         Type type = Type.GetType (name, false);
1046                         if (type == null)
1047                                 return new GregorianCalendar (GregorianCalendarTypes.Localized); // return invariant calendar if not found
1048                         return (Calendar) Activator.CreateInstance (type);
1049                 }
1050
1051                 static Exception CreateNotFoundException (string name)
1052                 {
1053                         return new CultureNotFoundException ("name", "Culture name " + name + " is not supported.");
1054                 }
1055                 
1056                 public static CultureInfo DefaultThreadCurrentCulture {
1057                         get {
1058                                 return Thread.default_culture;
1059                         }
1060                         set {
1061                                 Thread.default_culture = value;
1062                         }
1063                 }
1064                 
1065                 public static CultureInfo DefaultThreadCurrentUICulture {
1066                         get {
1067                                 return Thread.default_ui_culture;
1068                         }
1069                         set {
1070                                 Thread.default_ui_culture = value;
1071                         }
1072                 }
1073
1074                 internal string SortName {
1075                         get {
1076                                 return m_name;
1077                         }
1078                 }
1079
1080 #region reference sources
1081                 // TODO:
1082                 internal static readonly bool IsTaiwanSku;
1083
1084         //
1085         // CheckDomainSafetyObject throw if the object is customized object which cannot be attached to 
1086         // other object (like CultureInfo or DateTimeFormatInfo).
1087         //
1088
1089         internal static void CheckDomainSafetyObject(Object obj, Object container)
1090         {
1091             if (obj.GetType().Assembly != typeof(System.Globalization.CultureInfo).Assembly) {
1092                 
1093                 throw new InvalidOperationException(
1094                             String.Format(
1095                                 CultureInfo.CurrentCulture, 
1096                                 Environment.GetResourceString("InvalidOperation_SubclassedObject"), 
1097                                 obj.GetType(),
1098                                 container.GetType()));
1099             }
1100             Contract.EndContractBlock();
1101         }
1102
1103         // For resource lookup, we consider a culture the invariant culture by name equality.
1104         // We perform this check frequently during resource lookup, so adding a property for
1105         // improved readability.
1106         internal bool HasInvariantCultureName
1107         {
1108             get { return Name == CultureInfo.InvariantCulture.Name; }
1109         }
1110
1111         internal static bool VerifyCultureName(String cultureName, bool throwException)
1112         {
1113             // This function is used by ResourceManager.GetResourceFileName(). 
1114             // ResourceManager searches for resource using CultureInfo.Name,
1115             // so we should check against CultureInfo.Name.
1116
1117             for (int i=0; i<cultureName.Length; i++) {
1118                 char c = cultureName[i];
1119                 // 
1120
1121                 if (Char.IsLetterOrDigit(c) || c=='-' || c=='_') {
1122                     continue;
1123                 }
1124                 if (throwException) {
1125                     throw new ArgumentException(Environment.GetResourceString("Argument_InvalidResourceCultureName", cultureName));
1126                 }
1127                 return false;
1128             }
1129             return true;
1130         }
1131
1132 #endregion
1133         }
1134 }