| | 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.DurationDatatypes |
| | 7 | | { |
| | 8 | |
|
| | 9 | | public class DurationDT : BaseDT, IValue |
| | 10 | | { |
| 1 | 11 | | public TimeSpan Value { get; set; } |
| 1 | 12 | | public DurationDT() { } |
| 1 | 13 | | public DurationDT(string stringValue) : base(stringValue) |
| | 14 | | { |
| 1 | 15 | | ValidateValue(stringValue); |
| | 16 | | try |
| | 17 | | { |
| 1 | 18 | | Value = XmlConvert.ToTimeSpan(stringValue); |
| 1 | 19 | | } |
| 1 | 20 | | catch |
| | 21 | | { |
| 1 | 22 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 0 | 23 | | } |
| 1 | 24 | | } |
| | 25 | |
|
| 1 | 26 | | public DurationDT(string stringValue, FormatDescriptor format) : base(stringValue) |
| | 27 | | { |
| 1 | 28 | | ValidateValue(stringValue); |
| 1 | 29 | | ValidateFormat(format.pattern!); |
| | 30 | |
|
| 1 | 31 | | if (!Regex.IsMatch(stringValue, format.pattern!)) |
| 1 | 32 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| | 33 | | try |
| | 34 | | { |
| 1 | 35 | | Value = XmlConvert.ToTimeSpan(stringValue); |
| | 36 | |
|
| 1 | 37 | | } |
| 0 | 38 | | catch |
| | 39 | | { |
| 0 | 40 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 0 | 41 | | } |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | void ValidateValue(string stringValue) |
| | 45 | | { |
| 1 | 46 | | string pattern = @"-?P((([0-9]+Y([0-9]+M)?([0-9]+D)?)|([0-9]+M)([0-9]+D)?|([0-9]+D))(T((([0-9]+H)([0-9]+M)?( |
| | 47 | |
|
| 1 | 48 | | if (!Regex.IsMatch(stringValue, pattern)) |
| 1 | 49 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 1 | 50 | | } |
| | 51 | |
|
| | 52 | | void ValidateFormat(string stringValue) |
| | 53 | | { |
| | 54 | | try |
| | 55 | | { |
| 1 | 56 | | Regex regex = new Regex(stringValue); |
| 1 | 57 | | } |
| 0 | 58 | | catch (Exception) |
| | 59 | | { |
| 0 | 60 | | ErrorFactory.ThrowInvalidFormatError(stringValue); |
| 0 | 61 | | } |
| 1 | 62 | | } |
| | 63 | | override public int CompareTo(BaseDT? other) |
| | 64 | | { |
| 0 | 65 | | if (other is null) |
| 0 | 66 | | return 1; |
| 0 | 67 | | if (other is not DurationDT) |
| 0 | 68 | | throw new ArgumentException(); |
| 0 | 69 | | var typedOther = (DurationDT)other; |
| 0 | 70 | | return Value.CompareTo(typedOther.Value); |
| | 71 | |
|
| | 72 | | } |
| | 73 | |
|
| | 74 | | public override bool IsPatternValid(FormatDescriptor format) |
| | 75 | | { |
| | 76 | | try |
| | 77 | | { |
| 1 | 78 | | Regex regex = new Regex(format.pattern!); |
| 1 | 79 | | return true; |
| | 80 | | } |
| 0 | 81 | | catch (Exception) |
| | 82 | | { |
| 0 | 83 | | return false; |
| | 84 | | } |
| 1 | 85 | | } |
| | 86 | | } |
| | 87 | | } |