| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | | 3 | | using ValidateLib.Metadata.Descriptors; |
| | | 4 | | |
| | | 5 | | namespace ValidateLib.TabularData.Datatypes.DateDatatypes |
| | | 6 | | { |
| | | 7 | | public class DateTimeStampDT : DateTimeDT |
| | | 8 | | { |
| | 1 | 9 | | public static readonly string dateTimeStampRegex = @".*(Z|(\+|-)[0-9][0-9]:[0-9][0-9])"; |
| | 1 | 10 | | public DateTimeStampDT() { } |
| | 1 | 11 | | public DateTimeStampDT(string stringValue, FormatDescriptor format) : base(stringValue, format) |
| | | 12 | | { |
| | 1 | 13 | | if (format.pattern is not null) |
| | | 14 | | { |
| | 1 | 15 | | if (!isPatternValid(format.pattern)) |
| | 1 | 16 | | ErrorFactory.ThrowInvalidFormatError(format.pattern); |
| | | 17 | | } |
| | | 18 | | |
| | 1 | 19 | | CheckLexicalSpace(this.stringValue); |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | public static void CheckLexicalSpace(string stringValue) |
| | | 23 | | { |
| | 1 | 24 | | if (!Regex.IsMatch(stringValue, dateTimeStampRegex)) |
| | 0 | 25 | | ErrorFactory.ThrowDatatypeValidationError(); |
| | 1 | 26 | | } |
| | | 27 | | |
| | | 28 | | new protected bool isPatternValid(string pattern) |
| | | 29 | | { |
| | 1 | 30 | | if ( |
| | 1 | 31 | | !pattern.EndsWith("X") && |
| | 1 | 32 | | !pattern.EndsWith("x") |
| | 1 | 33 | | ) |
| | 1 | 34 | | return false; |
| | 1 | 35 | | return true; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | override protected string NormalizeStringValueToZZZ(string stringValue, string? formatPattern) |
| | | 39 | | { |
| | | 40 | | // case when we normalize without pattern provided |
| | 1 | 41 | | if (formatPattern is null) |
| | | 42 | | { |
| | 1 | 43 | | stringValue = NormalizeZMarker(stringValue); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // case when user provided a pattern format and ends with time zone marker |
| | | 47 | | |
| | | 48 | | else |
| | | 49 | | { |
| | 1 | 50 | | if (formatPattern.EndsWith("XXX")) |
| | | 51 | | { |
| | 1 | 52 | | stringValue = NormalizeZMarker(stringValue); |
| | 1 | 53 | | formatPattern.Replace("XXX", "zzz"); |
| | | 54 | | } |
| | 1 | 55 | | else if (formatPattern.EndsWith("XX")) |
| | | 56 | | { |
| | 0 | 57 | | stringValue = NormalizeZMarker(stringValue); |
| | 0 | 58 | | if (stringValue[stringValue.Length - 3] != ':') |
| | 0 | 59 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | 0 | 60 | | formatPattern.Replace("XX", "zzz"); |
| | | 61 | | } |
| | 1 | 62 | | else if (formatPattern.EndsWith("X")) |
| | | 63 | | { |
| | 1 | 64 | | stringValue = NormalizeZMarker(stringValue); |
| | 1 | 65 | | if (stringValue[stringValue.Length - 3] != ':') |
| | | 66 | | { |
| | 1 | 67 | | if (AreMinutesOmmited(stringValue)) |
| | 0 | 68 | | stringValue = stringValue + ":00"; |
| | | 69 | | else |
| | 1 | 70 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | | 71 | | } |
| | 1 | 72 | | formatPattern.Replace("X", "zzz"); |
| | | 73 | | } |
| | 1 | 74 | | else if (formatPattern.EndsWith("xxx")) |
| | 0 | 75 | | formatPattern.Replace("xxx", "zzz"); |
| | 1 | 76 | | else if (formatPattern.EndsWith("xx")) |
| | | 77 | | { |
| | 0 | 78 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | 0 | 79 | | formatPattern.Replace("xx", "zzz"); |
| | | 80 | | } |
| | 1 | 81 | | else if (formatPattern.EndsWith("x")) |
| | | 82 | | { |
| | 0 | 83 | | if (AreMinutesOmmited(stringValue)) |
| | 0 | 84 | | stringValue = stringValue + ":00"; |
| | | 85 | | else |
| | 0 | 86 | | stringValue = stringValue.Insert(stringValue.Length - 2, ":"); |
| | 0 | 87 | | formatPattern.Replace("x", "zzz"); |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | |
| | 1 | 91 | | return stringValue; |
| | | 92 | | |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | } |