1 /*
2  *  "GEDKeeper", the personal genealogical database editor.
3  *  Copyright (C) 2009-2021 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 BSLib.Calendar;
23 using GDModel.Providers.GEDCOM;
24 using GKCore;
25 using GKCore.Types;
26 
27 namespace GDModel
28 {
29     public class GDMDateValue : GDMCustomDate
30     {
31         private GDMCustomDate fValue;
32 
33         public GDMCustomDate Value
34         {
35             get { return fValue; }
36         }
37 
38 
GDMDateValue()39         public GDMDateValue()
40         {
41             fValue = null;
42         }
43 
Dispose(bool disposing)44         protected override void Dispose(bool disposing)
45         {
46             if (disposing) {
47                 if (fValue != null) fValue.Dispose();
48             }
49             base.Dispose(disposing);
50         }
51 
TrimExcess()52         internal override void TrimExcess()
53         {
54             base.TrimExcess();
55 
56             if (fValue != null) {
57                 fValue.TrimExcess();
58             }
59         }
60 
GetStringValue()61         protected override string GetStringValue()
62         {
63             return ((fValue == null) ? "" : fValue.StringValue);
64         }
65 
GetDateTime()66         public override DateTime GetDateTime()
67         {
68             DateTime result = ((fValue == null) ? new DateTime(0) : fValue.GetDateTime());
69             return result;
70         }
71 
SetDateTime(DateTime value)72         public override void SetDateTime(DateTime value)
73         {
74             if (fValue != null) {
75                 fValue.SetDateTime(value);
76             } else {
77                 fValue = new GDMDate();
78                 fValue.Date = value;
79             }
80         }
81 
Clear()82         public override void Clear()
83         {
84             base.Clear();
85 
86             if (fValue != null) fValue.Clear();
87         }
88 
IsEmpty()89         public override bool IsEmpty()
90         {
91             return base.IsEmpty() && (fValue == null || fValue.IsEmpty());
92         }
93 
ParseString(string strValue)94         public override string ParseString(string strValue)
95         {
96             try {
97                 if (fValue != null) {
98                     fValue.Dispose();
99                     fValue = null;
100                 }
101 
102                 return string.IsNullOrEmpty(strValue) ? string.Empty : GEDCOMUtils.ParseDateValue(null, this, strValue);
103             } catch (Exception ex) {
104                 Logger.WriteError("GDMDateValue.ParseString(\"" + strValue + "\")", ex);
105                 return strValue;
106             }
107         }
108 
109         /// <summary>
110         /// Internal helper method for parser
111         /// </summary>
SetRawData(GDMCustomDate value)112         internal void SetRawData(GDMCustomDate value)
113         {
114             fValue = value;
115         }
116 
117         /// <summary>
118         /// This function compares dates only by chronological year.
119         /// Month and day are not taken into account, the year is compared with the calendar.
120         /// </summary>
121         /// <param name="tag"></param>
122         /// <param name="matchParams"></param>
123         /// <returns></returns>
IsMatch(GDMTag tag, MatchParams matchParams)124         public override float IsMatch(GDMTag tag, MatchParams matchParams)
125         {
126             if (tag == null) return 0.0f;
127             GDMDateValue date = (GDMDateValue)tag;
128 
129             if (IsEmpty() || date.IsEmpty()) return 0.0f;
130 
131             int absVal1 = this.GetChronologicalYear();
132             int absVal2 = date.GetChronologicalYear();
133 
134             float match = 0.0f;
135             float matches = 0.0f;
136 
137             if (absVal1 != 0 && absVal2 != 0) {
138                 matches += 1.0f;
139                 if (Math.Abs(absVal1 - absVal2) <= matchParams.YearsInaccuracy) match += 100.0f;
140             }
141 
142             return (match / matches);
143         }
144 
GetUDN()145         public override UDN GetUDN()
146         {
147             return (fValue == null) ? UDN.CreateEmpty() : fValue.GetUDN();
148         }
149 
150         /// <summary>
151         /// In the historical chronology of the year 0 does not exist.
152         /// Therefore, the digit 0 in the year value can be used as a sign of lack or error.
153         /// ChronologicalYear - introduced for the purposes of uniform chronology years in the Gregorian calendar.
154         /// Is estimated from -4714 BC to 3268 AD.
155         /// </summary>
156         /// <returns>chronological year</returns>
GetChronologicalYear()157         public override int GetChronologicalYear()
158         {
159             return (fValue == null) ? 0 : fValue.GetChronologicalYear();
160         }
161 
GetDisplayStringExt(DateFormat format, bool sign, bool showCalendar)162         public override string GetDisplayStringExt(DateFormat format, bool sign, bool showCalendar)
163         {
164             string result = (fValue == null) ? string.Empty : fValue.GetDisplayStringExt(format, sign, showCalendar);
165             return result;
166         }
167     }
168 }
169