| | 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 | | // Does not support 24:00:00... |
| | 9 | | public class TimeDT : DateBaseDT |
| | 10 | | { |
| 1 | 11 | | public TimeDT() { } |
| 1 | 12 | | public DateTime Value { get; set; } |
| 1 | 13 | | public TimeDT(string stringValue) : base(stringValue, new FormatDescriptor()) |
| | 14 | | { |
| 1 | 15 | | CheckLexicalSpace(stringValue); |
| 1 | 16 | | Value = XmlConvert.ToDateTime(stringValue, XmlDateTimeSerializationMode.RoundtripKind); |
| | 17 | |
|
| 1 | 18 | | } |
| | 19 | |
|
| 1 | 20 | | public TimeDT(string stringValue, FormatDescriptor format) : base(stringValue, format) |
| | 21 | | { |
| 1 | 22 | | this.format = format; |
| 1 | 23 | | stringValue = NormalizeStringValueToZZZ(stringValue, format.pattern!); |
| 1 | 24 | | var modifiedPattern = format.pattern!.Replace('S', 'f'); |
| 1 | 25 | | modifiedPattern = NormalizeFormatToZZZ(modifiedPattern); |
| | 26 | |
|
| 1 | 27 | | if (!isPatternValid(format.pattern)) |
| 0 | 28 | | ErrorFactory.ThrowInvalidFormatError(modifiedPattern); |
| | 29 | |
|
| 1 | 30 | | Value = XmlConvert.ToDateTime(stringValue, modifiedPattern); |
| | 31 | |
|
| | 32 | |
|
| 1 | 33 | | } |
| | 34 | |
|
| | 35 | | void CheckLexicalSpace(string stringValue) |
| | 36 | | { |
| 1 | 37 | | string pattern = @"((([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?)|(24:00:00(\.0+)?)|(Z|(\+|-)((0[0-9 |
| 1 | 38 | | if (!Regex.IsMatch(stringValue, pattern)) |
| 1 | 39 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 1 | 40 | | } |
| | 41 | |
|
| | 42 | | override public int CompareTo(BaseDT? other) |
| | 43 | | { |
| 0 | 44 | | if (other is null) |
| 0 | 45 | | return 1; |
| 0 | 46 | | if (other is not TimeDT) |
| 0 | 47 | | throw new ArgumentException(); |
| 0 | 48 | | var typedOther = (TimeDT)other; |
| 0 | 49 | | return Value.CompareTo(typedOther.Value); |
| | 50 | |
|
| | 51 | | } |
| | 52 | | } |
| | 53 | | } |