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