| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.RegularExpressions; |
| | | 3 | | using ValidateLib.Metadata.Descriptors; |
| | | 4 | | |
| | | 5 | | namespace ValidateLib.TabularData.Datatypes.DateDatatypes |
| | | 6 | | { |
| | | 7 | | public class DateBaseDT : BaseDT, IValue |
| | | 8 | | { |
| | 1 | 9 | | public DateBaseDT() { } |
| | 0 | 10 | | public DateBaseDT(string stringValue) : base(stringValue) |
| | | 11 | | { |
| | 0 | 12 | | } |
| | | 13 | | |
| | 1 | 14 | | public DateBaseDT(string stringValue, FormatDescriptor format) : base(stringValue, format) |
| | | 15 | | { |
| | 1 | 16 | | } |
| | | 17 | | |
| | | 18 | | virtual protected string NormalizeStringValueToZZZ(string stringValue, string? formatPattern) |
| | | 19 | | { |
| | | 20 | | // case when we normalize without pattern provided |
| | 1 | 21 | | if (formatPattern is null) |
| | | 22 | | { |
| | 1 | 23 | | stringValue = NormalizeZMarker(stringValue); |
| | 1 | 24 | | if (stringValue.Length >= 6) |
| | | 25 | | { |
| | 1 | 26 | | string lastSixChars = stringValue.Substring(stringValue.Length - 6); |
| | 1 | 27 | | if (!Regex.IsMatch(lastSixChars, @"(Z|(\+|-)[0-9][0-9]:[0-9][0-9])")) |
| | 1 | 28 | | stringValue = stringValue + "+00:00"; |
| | | 29 | | } |
| | | 30 | | else |
| | 0 | 31 | | stringValue = stringValue + "+00:00"; |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | // case when user provided a pattern format and ends with time zone marker |
| | | 35 | | |
| | | 36 | | else |
| | | 37 | | { |
| | 1 | 38 | | if (formatPattern.EndsWith("XXX")) |
| | | 39 | | { |
| | 1 | 40 | | stringValue = NormalizeZMarker(stringValue); |
| | 1 | 41 | | formatPattern.Replace("XXX", "zzz"); |
| | | 42 | | } |
| | 1 | 43 | | else if (formatPattern.EndsWith("XX")) |
| | | 44 | | { |
| | 1 | 45 | | stringValue = NormalizeZMarker(stringValue); |
| | 1 | 46 | | if (stringValue[stringValue.Length - 3] != ':') |
| | 1 | 47 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | 1 | 48 | | formatPattern.Replace("XX", "zzz"); |
| | | 49 | | } |
| | 1 | 50 | | else if (formatPattern.EndsWith("X")) |
| | | 51 | | { |
| | 1 | 52 | | stringValue = NormalizeZMarker(stringValue); |
| | 1 | 53 | | if (stringValue[stringValue.Length - 3] != ':') |
| | | 54 | | { |
| | 1 | 55 | | if (AreMinutesOmmited(stringValue)) |
| | 1 | 56 | | stringValue = stringValue + ":00"; |
| | 1 | 57 | | if (stringValue[stringValue.Length - 3] != ':') |
| | 0 | 58 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | | 59 | | } |
| | 1 | 60 | | formatPattern.Replace("X", "zzz"); |
| | | 61 | | } |
| | 1 | 62 | | else if (formatPattern.EndsWith("xxx")) |
| | 0 | 63 | | formatPattern.Replace("xxx", "zzz"); |
| | 1 | 64 | | else if (formatPattern.EndsWith("xx")) |
| | | 65 | | { |
| | 0 | 66 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | 0 | 67 | | formatPattern.Replace("xx", "zzz"); |
| | | 68 | | } |
| | 1 | 69 | | else if (formatPattern.EndsWith("x")) |
| | | 70 | | { |
| | 1 | 71 | | if (AreMinutesOmmited(stringValue)) |
| | 1 | 72 | | stringValue = stringValue + ":00"; |
| | | 73 | | else |
| | 0 | 74 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | 1 | 75 | | formatPattern.Replace("x", "zzz"); |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | return stringValue; |
| | | 80 | | |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | protected string NormalizeFormatToZZZ(string formatPattern) |
| | | 84 | | { |
| | 1 | 85 | | if (formatPattern.EndsWith("XXX")) |
| | 1 | 86 | | return formatPattern.Replace("XXX", "zzz"); |
| | 1 | 87 | | else if (formatPattern.EndsWith("XX")) |
| | 1 | 88 | | return formatPattern.Replace("XX", "zzz"); |
| | 1 | 89 | | else if (formatPattern.EndsWith("X")) |
| | 1 | 90 | | return formatPattern.Replace("X", "zzz"); |
| | 1 | 91 | | else if (formatPattern.EndsWith("xxx")) |
| | 0 | 92 | | return formatPattern.Replace("xxx", "zzz"); |
| | 1 | 93 | | else if (formatPattern.EndsWith("xx")) |
| | 0 | 94 | | return formatPattern.Replace("xx", "zzz"); |
| | 1 | 95 | | else if (formatPattern.EndsWith("x")) |
| | 1 | 96 | | return formatPattern.Replace("x", "zzz"); |
| | 1 | 97 | | else return formatPattern; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | protected string NormalizeZMarker(string stringValue) |
| | | 101 | | { |
| | 1 | 102 | | if (stringValue[stringValue.Length - 1] == 'Z') |
| | | 103 | | { |
| | 1 | 104 | | stringValue = stringValue.Substring(0, stringValue.Length - 1) + "+00:00"; |
| | | 105 | | } |
| | | 106 | | |
| | 1 | 107 | | return stringValue; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | protected bool AreMinutesOmmited(string stringValue) |
| | | 111 | | { |
| | 1 | 112 | | if (stringValue[stringValue.Length - 3] == '-' || stringValue[stringValue.Length - 3] == '+') |
| | 1 | 113 | | return true; |
| | 1 | 114 | | return false; |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | protected bool isPatternValid(string pattern) |
| | | 118 | | { |
| | | 119 | | try |
| | | 120 | | { |
| | 1 | 121 | | string dts = DateTime.Now.ToString(pattern, CultureInfo.InvariantCulture); |
| | 1 | 122 | | Console.WriteLine(DateTime.ParseExact(dts, pattern, CultureInfo.InvariantCulture)); |
| | 1 | 123 | | return true; |
| | | 124 | | } |
| | 0 | 125 | | catch (Exception) |
| | | 126 | | { |
| | 0 | 127 | | return false; |
| | | 128 | | } |
| | | 129 | | |
| | 1 | 130 | | } |
| | 1 | 131 | | public override bool IsPatternValid(FormatDescriptor format) => isPatternValid(format.pattern!); |
| | | 132 | | |
| | | 133 | | } |
| | | 134 | | } |