HijriCalendar.Win32.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 Internal.Win32;
  5. namespace System.Globalization
  6. {
  7. public partial class HijriCalendar : Calendar
  8. {
  9. private int GetHijriDateAdjustment()
  10. {
  11. if (_hijriAdvance == int.MinValue)
  12. {
  13. // Never been set before. Use the system value from registry.
  14. _hijriAdvance = GetAdvanceHijriDate();
  15. }
  16. return (_hijriAdvance);
  17. }
  18. private const string InternationalRegKey = "Control Panel\\International";
  19. private const string HijriAdvanceRegKeyEntry = "AddHijriDate";
  20. /*=================================GetAdvanceHijriDate==========================
  21. **Action: Gets the AddHijriDate value from the registry.
  22. **Returns:
  23. **Arguments: None.
  24. **Exceptions:
  25. **Note:
  26. ** The HijriCalendar has a user-overidable calculation. That is, use can set a value from the control
  27. ** panel, so that the calculation of the Hijri Calendar can move ahead or backwards from -2 to +2 days.
  28. **
  29. ** The valid string values in the registry are:
  30. ** "AddHijriDate-2" => Add -2 days to the current calculated Hijri date.
  31. ** "AddHijriDate" => Add -1 day to the current calculated Hijri date.
  32. ** "" => Add 0 day to the current calculated Hijri date.
  33. ** "AddHijriDate+1" => Add +1 days to the current calculated Hijri date.
  34. ** "AddHijriDate+2" => Add +2 days to the current calculated Hijri date.
  35. ============================================================================*/
  36. private static int GetAdvanceHijriDate()
  37. {
  38. using (RegistryKey? key = Registry.CurrentUser.OpenSubKey(InternationalRegKey))
  39. {
  40. // Abort if we didn't find anything
  41. if (key == null)
  42. {
  43. return 0;
  44. }
  45. object? value = key.GetValue(HijriAdvanceRegKeyEntry);
  46. if (value == null)
  47. {
  48. return 0;
  49. }
  50. int hijriAdvance = 0;
  51. string? str = value.ToString();
  52. if (string.Compare(str, 0, HijriAdvanceRegKeyEntry, 0, HijriAdvanceRegKeyEntry.Length, StringComparison.OrdinalIgnoreCase) == 0)
  53. {
  54. if (str!.Length == HijriAdvanceRegKeyEntry.Length)
  55. hijriAdvance = -1;
  56. else
  57. {
  58. try
  59. {
  60. int advance = int.Parse(str.AsSpan(HijriAdvanceRegKeyEntry.Length), provider:CultureInfo.InvariantCulture);
  61. if ((advance >= MinAdvancedHijri) && (advance <= MaxAdvancedHijri))
  62. {
  63. hijriAdvance = advance;
  64. }
  65. }
  66. // If we got garbage from registry just ignore it.
  67. // hijriAdvance = 0 because of declaraction assignment up above.
  68. catch (ArgumentException) { }
  69. catch (FormatException) { }
  70. catch (OverflowException) { }
  71. }
  72. }
  73. return hijriAdvance;
  74. }
  75. }
  76. }
  77. }