1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2019 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.Globalization;
23 using System.Windows.Forms;
24 using GKCore;
25 
26 namespace GKUI.Components
27 {
28     /// <summary>
29     ///
30     /// </summary>
31     public class GKDateBox : MaskedTextBox
32     {
33         private readonly string fRegionalDatePattern;
34 
35 
36         public string RegionalDatePattern
37         {
38             get { return fRegionalDatePattern; }
39         }
40 
41         public string NormalizeDate
42         {
43             get { return GKUtils.GetNormalizeDate(Text, fRegionalDatePattern); }
44             set { Text = GKUtils.GetRegionalDate(value, fRegionalDatePattern); }
45         }
46 
47 
GKDateBox()48         public GKDateBox()
49         {
50             Culture = new CultureInfo("");
51             TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
52 
53             fRegionalDatePattern = GetShortDatePattern();
54             Mask = GetMask(fRegionalDatePattern);
55         }
56 
GetMask(string regionalDatePattern)57         private static string GetMask(string regionalDatePattern)
58         {
59             // "00/00/0000"
60             string result = regionalDatePattern.Replace('d', '0').Replace('m', '0').Replace('y', '0');
61             return result;
62         }
63 
GetShortDatePattern()64         private static string GetShortDatePattern()
65         {
66             var culture = CultureInfo.CurrentCulture; // work
67             //var culture = new CultureInfo("en-US"); // debug
68             //var culture = new CultureInfo("hu-HU"); // debug
69 
70             var dtf = culture.DateTimeFormat;
71             var dateSeparators = dtf.DateSeparator.ToCharArray();
72 
73             // may contain a period, a dash, and a slash
74             var result = dtf.ShortDatePattern.ToLowerInvariant();
75 
76             // normalize
77             string[] parts = result.Split(dateSeparators, StringSplitOptions.RemoveEmptyEntries);
78             for (int i = 0; i < parts.Length; i++) {
79                 string part = parts[i];
80                 char firstChar = part[0];
81                 switch (firstChar) {
82                     case 'd':
83                     case 'm':
84                         if (part.Length < 2) {
85                             part = part.PadRight(2, firstChar);
86                         }
87                         break;
88 
89                     case 'y':
90                         if (part.Length < 4) {
91                             part = part.PadRight(4, firstChar);
92                         }
93                         break;
94                 }
95                 parts[i] = part;
96             }
97             result = string.Join("/", parts);
98 
99             return result;
100         }
101     }
102 }
103