Collapse AsSpan().Slice(...) into .AsSpan(...) (dotnet/corefx#27867)
[platform/upstream/coreclr.git] / src / mscorlib / shared / System / Globalization / HijriCalendar.Win32.cs
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
5 using Microsoft.Win32;
6
7 namespace System.Globalization
8 {
9     public partial class HijriCalendar : Calendar
10     {
11         private int GetHijriDateAdjustment()
12         {
13             if (_hijriAdvance == Int32.MinValue)
14             {
15                 // Never been set before.  Use the system value from registry.
16                 _hijriAdvance = GetAdvanceHijriDate();
17             }
18             return (_hijriAdvance);
19         }
20
21         private const String InternationalRegKey = "Control Panel\\International";
22         private const String HijriAdvanceRegKeyEntry = "AddHijriDate";
23
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.
32         **
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()
41         {
42             int hijriAdvance = 0;
43             Microsoft.Win32.RegistryKey key = null;
44
45             try
46             {
47                 // Open in read-only mode.
48                 key = RegistryKey.GetBaseKey(RegistryKey.HKEY_CURRENT_USER).OpenSubKey(InternationalRegKey, false);
49             }
50             //If this fails for any reason, we'll just return 0.
51             catch (ObjectDisposedException) { return 0; }
52             catch (ArgumentException) { return 0; }
53
54             if (key != null)
55             {
56                 try
57                 {
58                     Object value = key.InternalGetValue(HijriAdvanceRegKeyEntry, null, false, false);
59                     if (value == null)
60                     {
61                         return (0);
62                     }
63                     String str = value.ToString();
64                     if (String.Compare(str, 0, HijriAdvanceRegKeyEntry, 0, HijriAdvanceRegKeyEntry.Length, StringComparison.OrdinalIgnoreCase) == 0)
65                     {
66                         if (str.Length == HijriAdvanceRegKeyEntry.Length)
67                             hijriAdvance = -1;
68                         else
69                         {
70                             try
71                             {
72                                 int advance = Int32.Parse(str.AsSpan(HijriAdvanceRegKeyEntry.Length), provider:CultureInfo.InvariantCulture);
73                                 if ((advance >= MinAdvancedHijri) && (advance <= MaxAdvancedHijri))
74                                 {
75                                     hijriAdvance = advance;
76                                 }
77                             }
78                             // If we got garbage from registry just ignore it.
79                             // hijriAdvance = 0 because of declaraction assignment up above.
80                             catch (ArgumentException) { }
81                             catch (FormatException) { }
82                             catch (OverflowException) { }
83                         }
84                     }
85                 }
86                 finally
87                 {
88                     key.Close();
89                 }
90             }
91             return (hijriAdvance);
92         }
93     }
94 }