CalendarData.Windows.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.CompilerServices;
  8. using System.Collections.Generic;
  9. using Internal.Runtime.CompilerServices;
  10. namespace System.Globalization
  11. {
  12. internal partial class CalendarData
  13. {
  14. private bool LoadCalendarDataFromSystem(string localeName, CalendarId calendarId)
  15. {
  16. Debug.Assert(!GlobalizationMode.Invariant);
  17. bool ret = true;
  18. uint useOverrides = this.bUseUserOverrides ? 0 : CAL_NOUSEROVERRIDE;
  19. //
  20. // Windows doesn't support some calendars right now, so remap those.
  21. //
  22. switch (calendarId)
  23. {
  24. case CalendarId.JAPANESELUNISOLAR: // Data looks like Japanese
  25. calendarId = CalendarId.JAPAN;
  26. break;
  27. case CalendarId.JULIAN: // Data looks like gregorian US
  28. case CalendarId.CHINESELUNISOLAR: // Algorithmic, so actual data is irrelevent
  29. case CalendarId.SAKA: // reserved to match Office but not implemented in our code, so data is irrelevent
  30. case CalendarId.LUNAR_ETO_CHN: // reserved to match Office but not implemented in our code, so data is irrelevent
  31. case CalendarId.LUNAR_ETO_KOR: // reserved to match Office but not implemented in our code, so data is irrelevent
  32. case CalendarId.LUNAR_ETO_ROKUYOU: // reserved to match Office but not implemented in our code, so data is irrelevent
  33. case CalendarId.KOREANLUNISOLAR: // Algorithmic, so actual data is irrelevent
  34. case CalendarId.TAIWANLUNISOLAR: // Algorithmic, so actual data is irrelevent
  35. calendarId = CalendarId.GREGORIAN_US;
  36. break;
  37. }
  38. //
  39. // Special handling for some special calendar due to OS limitation.
  40. // This includes calendar like Taiwan calendar, UmAlQura calendar, etc.
  41. //
  42. CheckSpecialCalendar(ref calendarId, ref localeName);
  43. // Numbers
  44. ret &= CallGetCalendarInfoEx(localeName, calendarId, CAL_ITWODIGITYEARMAX | useOverrides, out this.iTwoDigitYearMax);
  45. // Strings
  46. ret &= CallGetCalendarInfoEx(localeName, calendarId, CAL_SCALNAME, out this.sNativeName);
  47. ret &= CallGetCalendarInfoEx(localeName, calendarId, CAL_SMONTHDAY | useOverrides, out this.sMonthDay);
  48. // String Arrays
  49. // Formats
  50. ret &= CallEnumCalendarInfo(localeName, calendarId, CAL_SSHORTDATE, LOCALE_SSHORTDATE | useOverrides, out this.saShortDates);
  51. ret &= CallEnumCalendarInfo(localeName, calendarId, CAL_SLONGDATE, LOCALE_SLONGDATE | useOverrides, out this.saLongDates);
  52. // Get the YearMonth pattern.
  53. ret &= CallEnumCalendarInfo(localeName, calendarId, CAL_SYEARMONTH, LOCALE_SYEARMONTH, out this.saYearMonths);
  54. // Day & Month Names
  55. // These are all single calType entries, 1 per day, so we have to make 7 or 13 calls to collect all the names
  56. // Day
  57. // Note that we're off-by-one since managed starts on sunday and windows starts on monday
  58. ret &= GetCalendarDayInfo(localeName, calendarId, CAL_SDAYNAME7, out this.saDayNames);
  59. ret &= GetCalendarDayInfo(localeName, calendarId, CAL_SABBREVDAYNAME7, out this.saAbbrevDayNames);
  60. // Month names
  61. ret &= GetCalendarMonthInfo(localeName, calendarId, CAL_SMONTHNAME1, out this.saMonthNames);
  62. ret &= GetCalendarMonthInfo(localeName, calendarId, CAL_SABBREVMONTHNAME1, out this.saAbbrevMonthNames);
  63. //
  64. // The following LCTYPE are not supported in some platforms. If the call fails,
  65. // don't return a failure.
  66. //
  67. GetCalendarDayInfo(localeName, calendarId, CAL_SSHORTESTDAYNAME7, out this.saSuperShortDayNames);
  68. // Gregorian may have genitive month names
  69. if (calendarId == CalendarId.GREGORIAN)
  70. {
  71. GetCalendarMonthInfo(localeName, calendarId, CAL_SMONTHNAME1 | CAL_RETURN_GENITIVE_NAMES, out this.saMonthGenitiveNames);
  72. GetCalendarMonthInfo(localeName, calendarId, CAL_SABBREVMONTHNAME1 | CAL_RETURN_GENITIVE_NAMES, out this.saAbbrevMonthGenitiveNames);
  73. }
  74. // Calendar Parts Names
  75. // This doesn't get always get localized names for gregorian (not available in windows < 7)
  76. // so: eg: coreclr on win < 7 won't get these
  77. CallEnumCalendarInfo(localeName, calendarId, CAL_SERASTRING, 0, out this.saEraNames);
  78. CallEnumCalendarInfo(localeName, calendarId, CAL_SABBREVERASTRING, 0, out this.saAbbrevEraNames);
  79. //
  80. // Calendar Era Info
  81. // Note that calendar era data (offsets, etc) is hard coded for each calendar since this
  82. // data is implementation specific and not dynamic (except perhaps Japanese)
  83. //
  84. // Clean up the escaping of the formats
  85. this.saShortDates = CultureData.ReescapeWin32Strings(this.saShortDates);
  86. this.saLongDates = CultureData.ReescapeWin32Strings(this.saLongDates);
  87. this.saYearMonths = CultureData.ReescapeWin32Strings(this.saYearMonths);
  88. this.sMonthDay = CultureData.ReescapeWin32String(this.sMonthDay);
  89. return ret;
  90. }
  91. // Get native two digit year max
  92. internal static int GetTwoDigitYearMax(CalendarId calendarId)
  93. {
  94. if (GlobalizationMode.Invariant)
  95. {
  96. return Invariant.iTwoDigitYearMax;
  97. }
  98. int twoDigitYearMax = -1;
  99. if (!CallGetCalendarInfoEx(null, calendarId, (uint)CAL_ITWODIGITYEARMAX, out twoDigitYearMax))
  100. {
  101. twoDigitYearMax = -1;
  102. }
  103. return twoDigitYearMax;
  104. }
  105. // Call native side to figure out which calendars are allowed
  106. internal static int GetCalendars(string localeName, bool useUserOverride, CalendarId[] calendars)
  107. {
  108. Debug.Assert(!GlobalizationMode.Invariant);
  109. EnumCalendarsData data = new EnumCalendarsData();
  110. data.userOverride = 0;
  111. data.calendars = new List<int>();
  112. // First call GetLocaleInfo if necessary
  113. if (useUserOverride)
  114. {
  115. // They want user overrides, see if the user calendar matches the input calendar
  116. int userCalendar = CultureData.GetLocaleInfoExInt(localeName, LOCALE_ICALENDARTYPE);
  117. // If we got a default, then use it as the first calendar
  118. if (userCalendar != 0)
  119. {
  120. data.userOverride = userCalendar;
  121. data.calendars.Add(userCalendar);
  122. }
  123. }
  124. unsafe
  125. {
  126. Interop.Kernel32.EnumCalendarInfoExEx(EnumCalendarsCallback, localeName, ENUM_ALL_CALENDARS, null, CAL_ICALINTVALUE, Unsafe.AsPointer(ref data));
  127. }
  128. // Copy to the output array
  129. for (int i = 0; i < Math.Min(calendars.Length, data.calendars.Count); i++)
  130. calendars[i] = (CalendarId)data.calendars[i];
  131. // Now we have a list of data, return the count
  132. return data.calendars.Count;
  133. }
  134. private static bool SystemSupportsTaiwaneseCalendar()
  135. {
  136. Debug.Assert(!GlobalizationMode.Invariant);
  137. string data;
  138. // Taiwanese calendar get listed as one of the optional zh-TW calendars only when having zh-TW UI
  139. return CallGetCalendarInfoEx("zh-TW", CalendarId.TAIWAN, CAL_SCALNAME, out data);
  140. }
  141. // PAL Layer ends here
  142. private const uint CAL_RETURN_NUMBER = 0x20000000;
  143. private const uint CAL_RETURN_GENITIVE_NAMES = 0x10000000;
  144. private const uint CAL_NOUSEROVERRIDE = 0x80000000;
  145. private const uint CAL_SCALNAME = 0x00000002;
  146. private const uint CAL_SMONTHDAY = 0x00000038;
  147. private const uint CAL_SSHORTDATE = 0x00000005;
  148. private const uint CAL_SLONGDATE = 0x00000006;
  149. private const uint CAL_SYEARMONTH = 0x0000002f;
  150. private const uint CAL_SDAYNAME7 = 0x0000000d;
  151. private const uint CAL_SABBREVDAYNAME7 = 0x00000014;
  152. private const uint CAL_SMONTHNAME1 = 0x00000015;
  153. private const uint CAL_SABBREVMONTHNAME1 = 0x00000022;
  154. private const uint CAL_SSHORTESTDAYNAME7 = 0x00000037;
  155. private const uint CAL_SERASTRING = 0x00000004;
  156. private const uint CAL_SABBREVERASTRING = 0x00000039;
  157. private const uint CAL_ICALINTVALUE = 0x00000001;
  158. private const uint CAL_ITWODIGITYEARMAX = 0x00000030;
  159. private const uint ENUM_ALL_CALENDARS = 0xffffffff;
  160. private const uint LOCALE_SSHORTDATE = 0x0000001F;
  161. private const uint LOCALE_SLONGDATE = 0x00000020;
  162. private const uint LOCALE_SYEARMONTH = 0x00001006;
  163. private const uint LOCALE_ICALENDARTYPE = 0x00001009;
  164. ////////////////////////////////////////////////////////////////////////
  165. //
  166. // For calendars like Gregorain US/Taiwan/UmAlQura, they are not available
  167. // in all OS or all localized versions of OS.
  168. // If OS does not support these calendars, we will fallback by using the
  169. // appropriate fallback calendar and locale combination to retrieve data from OS.
  170. //
  171. // Parameters:
  172. // __deref_inout pCalendarInt:
  173. // Pointer to the calendar ID. This will be updated to new fallback calendar ID if needed.
  174. // __in_out pLocaleNameStackBuffer
  175. // Pointer to the StackSString object which holds the locale name to be checked.
  176. // This will be updated to new fallback locale name if needed.
  177. //
  178. ////////////////////////////////////////////////////////////////////////
  179. private static void CheckSpecialCalendar(ref CalendarId calendar, ref string localeName)
  180. {
  181. string data;
  182. // Gregorian-US isn't always available in the OS, however it is the same for all locales
  183. switch (calendar)
  184. {
  185. case CalendarId.GREGORIAN_US:
  186. // See if this works
  187. if (!CallGetCalendarInfoEx(localeName, calendar, CAL_SCALNAME, out data))
  188. {
  189. // Failed, set it to a locale (fa-IR) that's alway has Gregorian US available in the OS
  190. localeName = "fa-IR";
  191. }
  192. // See if that works
  193. if (!CallGetCalendarInfoEx(localeName, calendar, CAL_SCALNAME, out data))
  194. {
  195. // Failed again, just use en-US with the gregorian calendar
  196. localeName = "en-US";
  197. calendar = CalendarId.GREGORIAN;
  198. }
  199. break;
  200. case CalendarId.TAIWAN:
  201. // Taiwan calendar data is not always in all language version of OS due to Geopolical reasons.
  202. // It is only available in zh-TW localized versions of Windows.
  203. // Let's check if OS supports it. If not, fallback to Greogrian localized for Taiwan calendar.
  204. if (!SystemSupportsTaiwaneseCalendar())
  205. {
  206. calendar = CalendarId.GREGORIAN;
  207. }
  208. break;
  209. }
  210. }
  211. private static bool CallGetCalendarInfoEx(string localeName, CalendarId calendar, uint calType, out int data)
  212. {
  213. return (Interop.Kernel32.GetCalendarInfoEx(localeName, (uint)calendar, IntPtr.Zero, calType | CAL_RETURN_NUMBER, IntPtr.Zero, 0, out data) != 0);
  214. }
  215. private static unsafe bool CallGetCalendarInfoEx(string localeName, CalendarId calendar, uint calType, out string data)
  216. {
  217. const int BUFFER_LENGTH = 80;
  218. // The maximum size for values returned from GetCalendarInfoEx is 80 characters.
  219. char* buffer = stackalloc char[BUFFER_LENGTH];
  220. int ret = Interop.Kernel32.GetCalendarInfoEx(localeName, (uint)calendar, IntPtr.Zero, calType, (IntPtr)buffer, BUFFER_LENGTH, IntPtr.Zero);
  221. if (ret > 0)
  222. {
  223. if (buffer[ret - 1] == '\0')
  224. {
  225. ret--; // don't include the null termination in the string
  226. }
  227. data = new string(buffer, 0, ret);
  228. return true;
  229. }
  230. data = "";
  231. return false;
  232. }
  233. // Context for EnumCalendarInfoExEx callback.
  234. private struct EnumData
  235. {
  236. public string userOverride;
  237. public List<string> strings;
  238. }
  239. // EnumCalendarInfoExEx callback itself.
  240. // [NativeCallable(CallingConvention = CallingConvention.StdCall)]
  241. private static unsafe Interop.BOOL EnumCalendarInfoCallback(char* lpCalendarInfoString, uint calendar, IntPtr pReserved, void* lParam)
  242. {
  243. ref EnumData context = ref Unsafe.As<byte, EnumData>(ref *(byte*)lParam);
  244. try
  245. {
  246. string calendarInfo = new string(lpCalendarInfoString);
  247. // If we had a user override, check to make sure this differs
  248. if (context.userOverride != calendarInfo)
  249. context.strings.Add(calendarInfo);
  250. return Interop.BOOL.TRUE;
  251. }
  252. catch (Exception)
  253. {
  254. return Interop.BOOL.FALSE;
  255. }
  256. }
  257. private static unsafe bool CallEnumCalendarInfo(string localeName, CalendarId calendar, uint calType, uint lcType, out string[] data)
  258. {
  259. EnumData context = new EnumData();
  260. context.userOverride = null;
  261. context.strings = new List<string>();
  262. // First call GetLocaleInfo if necessary
  263. if (((lcType != 0) && ((lcType & CAL_NOUSEROVERRIDE) == 0)) &&
  264. // Get user locale, see if it matches localeName.
  265. // Note that they should match exactly, including letter case
  266. GetUserDefaultLocaleName() == localeName)
  267. {
  268. // They want user overrides, see if the user calendar matches the input calendar
  269. CalendarId userCalendar = (CalendarId)CultureData.GetLocaleInfoExInt(localeName, LOCALE_ICALENDARTYPE);
  270. // If the calendars were the same, see if the locales were the same
  271. if (userCalendar == calendar)
  272. {
  273. // They matched, get the user override since locale & calendar match
  274. string res = CultureData.GetLocaleInfoEx(localeName, lcType);
  275. // if it succeeded remember the override for the later callers
  276. if (res != null)
  277. {
  278. // Remember this was the override (so we can look for duplicates later in the enum function)
  279. context.userOverride = res;
  280. // Add to the result strings.
  281. context.strings.Add(res);
  282. }
  283. }
  284. }
  285. unsafe
  286. {
  287. // Now call the enumeration API. Work is done by our callback function
  288. Interop.Kernel32.EnumCalendarInfoExEx(EnumCalendarInfoCallback, localeName, (uint)calendar, null, calType, Unsafe.AsPointer(ref context));
  289. }
  290. // Now we have a list of data, fail if we didn't find anything.
  291. if (context.strings.Count == 0)
  292. {
  293. data = null;
  294. return false;
  295. }
  296. string[] output = context.strings.ToArray();
  297. if (calType == CAL_SABBREVERASTRING || calType == CAL_SERASTRING)
  298. {
  299. // Eras are enumerated backwards. (oldest era name first, but
  300. // Japanese calendar has newest era first in array, and is only
  301. // calendar with multiple eras)
  302. Array.Reverse(output, 0, output.Length);
  303. }
  304. data = output;
  305. return true;
  306. }
  307. ////////////////////////////////////////////////////////////////////////
  308. //
  309. // Get the native day names
  310. //
  311. // NOTE: There's a disparity between .Net & windows day orders, the input day should
  312. // start with Sunday
  313. //
  314. // Parameters:
  315. // OUT pOutputStrings The output string[] value.
  316. //
  317. ////////////////////////////////////////////////////////////////////////
  318. private static bool GetCalendarDayInfo(string localeName, CalendarId calendar, uint calType, out string[] outputStrings)
  319. {
  320. bool result = true;
  321. //
  322. // We'll need a new array of 7 items
  323. //
  324. string[] results = new string[7];
  325. // Get each one of them
  326. for (int i = 0; i < 7; i++, calType++)
  327. {
  328. result &= CallGetCalendarInfoEx(localeName, calendar, calType, out results[i]);
  329. // On the first iteration we need to go from CAL_SDAYNAME7 to CAL_SDAYNAME1, so subtract 7 before the ++ happens
  330. // This is because the framework starts on sunday and windows starts on monday when counting days
  331. if (i == 0)
  332. calType -= 7;
  333. }
  334. outputStrings = results;
  335. return result;
  336. }
  337. ////////////////////////////////////////////////////////////////////////
  338. //
  339. // Get the native month names
  340. //
  341. // Parameters:
  342. // OUT pOutputStrings The output string[] value.
  343. //
  344. ////////////////////////////////////////////////////////////////////////
  345. private static bool GetCalendarMonthInfo(string localeName, CalendarId calendar, uint calType, out string[] outputStrings)
  346. {
  347. //
  348. // We'll need a new array of 13 items
  349. //
  350. string[] results = new string[13];
  351. // Get each one of them
  352. for (int i = 0; i < 13; i++, calType++)
  353. {
  354. if (!CallGetCalendarInfoEx(localeName, calendar, calType, out results[i]))
  355. results[i] = "";
  356. }
  357. outputStrings = results;
  358. return true;
  359. }
  360. //
  361. // struct to help our calendar data enumaration callback
  362. //
  363. private struct EnumCalendarsData
  364. {
  365. public int userOverride; // user override value (if found)
  366. public List<int> calendars; // list of calendars found so far
  367. }
  368. // [NativeCallable(CallingConvention = CallingConvention.StdCall)]
  369. private static unsafe Interop.BOOL EnumCalendarsCallback(char* lpCalendarInfoString, uint calendar, IntPtr reserved, void* lParam)
  370. {
  371. ref EnumCalendarsData context = ref Unsafe.As<byte, EnumCalendarsData>(ref *(byte*)lParam);
  372. try
  373. {
  374. // If we had a user override, check to make sure this differs
  375. if (context.userOverride != calendar)
  376. context.calendars.Add((int)calendar);
  377. return Interop.BOOL.TRUE;
  378. }
  379. catch (Exception)
  380. {
  381. return Interop.BOOL.FALSE;
  382. }
  383. }
  384. private static unsafe string GetUserDefaultLocaleName()
  385. {
  386. Debug.Assert(!GlobalizationMode.Invariant);
  387. const int LOCALE_NAME_MAX_LENGTH = 85;
  388. const uint LOCALE_SNAME = 0x0000005c;
  389. const string LOCALE_NAME_USER_DEFAULT = null;
  390. int result;
  391. char* localeName = stackalloc char[LOCALE_NAME_MAX_LENGTH];
  392. result = CultureData.GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SNAME, localeName, LOCALE_NAME_MAX_LENGTH);
  393. return result <= 0 ? "" : new string(localeName, 0, result - 1); // exclude the null termination
  394. }
  395. }
  396. }