summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Globalization/HijriCalendar.Win32.cs
blob: 4ba95c8fa8197448d008994dfd102d85ba9d7a28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Win32;

namespace System.Globalization
{
    public partial class HijriCalendar : Calendar
    {
        private int GetHijriDateAdjustment()
        {
            if (_hijriAdvance == Int32.MinValue)
            {
                // Never been set before.  Use the system value from registry.
                _hijriAdvance = GetAdvanceHijriDate();
            }
            return (_hijriAdvance);
        }

        private const String InternationalRegKey = "Control Panel\\International";
        private const String HijriAdvanceRegKeyEntry = "AddHijriDate";

        /*=================================GetAdvanceHijriDate==========================
        **Action: Gets the AddHijriDate value from the registry.
        **Returns:
        **Arguments:    None.
        **Exceptions:
        **Note:
        **  The HijriCalendar has a user-overidable calculation.  That is, use can set a value from the control
        **  panel, so that the calculation of the Hijri Calendar can move ahead or backwards from -2 to +2 days.
        **
        **  The valid string values in the registry are:
        **      "AddHijriDate-2"  =>  Add -2 days to the current calculated Hijri date.
        **      "AddHijriDate"    =>  Add -1 day to the current calculated Hijri date.
        **      ""              =>  Add 0 day to the current calculated Hijri date.
        **      "AddHijriDate+1"  =>  Add +1 days to the current calculated Hijri date.
        **      "AddHijriDate+2"  =>  Add +2 days to the current calculated Hijri date.
        ============================================================================*/
        private static int GetAdvanceHijriDate()
        {
            int hijriAdvance = 0;
            Microsoft.Win32.RegistryKey key = null;

            using (key = Registry.CurrentUser.OpenSubKey(InternationalRegKey, writable: false))
            {
                if (key == null)
                    return 0;

                Object value = key.GetValue(HijriAdvanceRegKeyEntry);
                if (value == null)
                {
                    return (0);
                }
                String str = value.ToString();
                if (String.Compare(str, 0, HijriAdvanceRegKeyEntry, 0, HijriAdvanceRegKeyEntry.Length, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    if (str.Length == HijriAdvanceRegKeyEntry.Length)
                        hijriAdvance = -1;
                    else
                    {
                        str = str.Substring(HijriAdvanceRegKeyEntry.Length);
                        try
                        {
                            int advance = Int32.Parse(str.ToString(), CultureInfo.InvariantCulture);
                            if ((advance >= MinAdvancedHijri) && (advance <= MaxAdvancedHijri))
                            {
                                hijriAdvance = advance;
                            }
                        }
                        // If we got garbage from registry just ignore it.
                        // hijriAdvance = 0 because of declaraction assignment up above.
                        catch (ArgumentException) { }
                        catch (FormatException) { }
                        catch (OverflowException) { }
                    }
                }
            }

            return (hijriAdvance);
        }
    }
}