| | 1 | | using System.Text.RegularExpressions; |
| | 2 | | using System.Xml; |
| | 3 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 4 | | using ValidateLib.Metadata.Descriptors; |
| | 5 | |
|
| | 6 | | namespace ValidateLib.TabularData.Datatypes.DateDatatypes |
| | 7 | | { |
| | 8 | | public abstract class GBaseDT : BaseDT |
| | 9 | | { |
| 1 | 10 | | public DateTime Value { get; set; } |
| 1 | 11 | | public GBaseDT() { } |
| 1 | 12 | | protected GBaseDT(string stringValue) : base(stringValue) |
| | 13 | | { |
| 1 | 14 | | CheckLexicalSpace(stringValue); |
| | 15 | | try |
| | 16 | | { |
| 1 | 17 | | Value = XmlConvert.ToDateTime(stringValue, XmlDateTimeSerializationMode.RoundtripKind); |
| 1 | 18 | | } |
| | 19 | |
|
| 1 | 20 | | catch (Exception) |
| | 21 | | { |
| 1 | 22 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 0 | 23 | | } |
| | 24 | |
|
| 1 | 25 | | } |
| 0 | 26 | | protected GBaseDT(string stringValue, FormatDescriptor format) : base(stringValue, format) |
| | 27 | | { |
| 0 | 28 | | VerifyFormatPatternIsValidRegex(format.pattern!); |
| 0 | 29 | | ValidateFormat(stringValue, format.pattern!); |
| 0 | 30 | | CheckLexicalSpace(stringValue); |
| | 31 | | try |
| | 32 | | { |
| 0 | 33 | | Value = XmlConvert.ToDateTime(stringValue, XmlDateTimeSerializationMode.RoundtripKind); |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | catch (Exception) |
| | 37 | | { |
| 0 | 38 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 0 | 39 | | } |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | void ValidateFormat(string stringValue, string formatPattern) |
| | 43 | | { |
| 0 | 44 | | if (!Regex.IsMatch(stringValue, formatPattern)) |
| 0 | 45 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public virtual void CheckLexicalSpace(string stringValue) |
| | 49 | | { |
| | 50 | |
|
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | override public int CompareTo(BaseDT? other) |
| | 54 | | { |
| 0 | 55 | | if (other is null) |
| 0 | 56 | | return 1; |
| 0 | 57 | | if (other is not GBaseDT) |
| 0 | 58 | | throw new ArgumentException(); |
| 0 | 59 | | var typedOther = (GBaseDT)other; |
| 0 | 60 | | return Value.CompareTo(typedOther.Value); |
| | 61 | |
|
| | 62 | | } |
| | 63 | | public override bool IsPatternValid(FormatDescriptor format) |
| | 64 | | { |
| | 65 | | try |
| | 66 | | { |
| 0 | 67 | | Regex regex = new Regex(format.pattern!); |
| 0 | 68 | | return true; |
| | 69 | | } |
| 0 | 70 | | catch (Exception) |
| | 71 | | { |
| 0 | 72 | | return false; |
| | 73 | | } |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | } |
| | 77 | | } |