< Summary

Information
Class: ValidateLib.TabularData.Datatypes.DateDatatypes.DateBaseDT
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Datatypes\DateDatatypes\DateBaseDT.cs
Line coverage
81%
Covered lines: 53
Uncovered lines: 12
Coverable lines: 65
Total lines: 134
Line coverage: 81.5%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Datatypes\DateDatatypes\DateBaseDT.cs

#LineLine coverage
 1using System.Globalization;
 2using System.Text.RegularExpressions;
 3using ValidateLib.Metadata.Descriptors;
 4
 5namespace ValidateLib.TabularData.Datatypes.DateDatatypes
 6{
 7    public class DateBaseDT : BaseDT, IValue
 8    {
 19        public DateBaseDT() { }
 010        public DateBaseDT(string stringValue) : base(stringValue)
 11        {
 012        }
 13
 114        public DateBaseDT(string stringValue, FormatDescriptor format) : base(stringValue, format)
 15        {
 116        }
 17
 18        virtual protected string NormalizeStringValueToZZZ(string stringValue, string? formatPattern)
 19        {
 20            // case when we normalize without pattern provided
 121            if (formatPattern is null)
 22            {
 123                stringValue = NormalizeZMarker(stringValue);
 124                if (stringValue.Length >= 6)
 25                {
 126                    string lastSixChars = stringValue.Substring(stringValue.Length - 6);
 127                    if (!Regex.IsMatch(lastSixChars, @"(Z|(\+|-)[0-9][0-9]:[0-9][0-9])"))
 128                        stringValue = stringValue + "+00:00";
 29                }
 30                else
 031                    stringValue = stringValue + "+00:00";
 32            }
 33
 34            // case when user provided a pattern format and ends with time zone marker
 35
 36            else
 37            {
 138                if (formatPattern.EndsWith("XXX"))
 39                {
 140                    stringValue = NormalizeZMarker(stringValue);
 141                    formatPattern.Replace("XXX", "zzz");
 42                }
 143                else if (formatPattern.EndsWith("XX"))
 44                {
 145                    stringValue = NormalizeZMarker(stringValue);
 146                    if (stringValue[stringValue.Length - 3] != ':')
 147                        stringValue = stringValue.Insert(stringValue.Length - 2, ":");
 148                    formatPattern.Replace("XX", "zzz");
 49                }
 150                else if (formatPattern.EndsWith("X"))
 51                {
 152                    stringValue = NormalizeZMarker(stringValue);
 153                    if (stringValue[stringValue.Length - 3] != ':')
 54                    {
 155                        if (AreMinutesOmmited(stringValue))
 156                            stringValue = stringValue + ":00";
 157                        if (stringValue[stringValue.Length - 3] != ':')
 058                            stringValue = stringValue.Insert(stringValue.Length - 2, ":");
 59                    }
 160                    formatPattern.Replace("X", "zzz");
 61                }
 162                else if (formatPattern.EndsWith("xxx"))
 063                    formatPattern.Replace("xxx", "zzz");
 164                else if (formatPattern.EndsWith("xx"))
 65                {
 066                    stringValue = stringValue.Insert(stringValue.Length - 2, ":");
 067                    formatPattern.Replace("xx", "zzz");
 68                }
 169                else if (formatPattern.EndsWith("x"))
 70                {
 171                    if (AreMinutesOmmited(stringValue))
 172                        stringValue = stringValue + ":00";
 73                    else
 074                        stringValue = stringValue.Insert(stringValue.Length - 2, ":");
 175                    formatPattern.Replace("x", "zzz");
 76                }
 77            }
 78
 179            return stringValue;
 80
 81        }
 82
 83        protected string NormalizeFormatToZZZ(string formatPattern)
 84        {
 185            if (formatPattern.EndsWith("XXX"))
 186                return formatPattern.Replace("XXX", "zzz");
 187            else if (formatPattern.EndsWith("XX"))
 188                return formatPattern.Replace("XX", "zzz");
 189            else if (formatPattern.EndsWith("X"))
 190                return formatPattern.Replace("X", "zzz");
 191            else if (formatPattern.EndsWith("xxx"))
 092                return formatPattern.Replace("xxx", "zzz");
 193            else if (formatPattern.EndsWith("xx"))
 094                return formatPattern.Replace("xx", "zzz");
 195            else if (formatPattern.EndsWith("x"))
 196                return formatPattern.Replace("x", "zzz");
 197            else return formatPattern;
 98        }
 99
 100        protected string NormalizeZMarker(string stringValue)
 101        {
 1102            if (stringValue[stringValue.Length - 1] == 'Z')
 103            {
 1104                stringValue = stringValue.Substring(0, stringValue.Length - 1) + "+00:00";
 105            }
 106
 1107            return stringValue;
 108        }
 109
 110        protected bool AreMinutesOmmited(string stringValue)
 111        {
 1112            if (stringValue[stringValue.Length - 3] == '-' || stringValue[stringValue.Length - 3] == '+')
 1113                return true;
 1114            return false;
 115        }
 116
 117        protected bool isPatternValid(string pattern)
 118        {
 119            try
 120            {
 1121                string dts = DateTime.Now.ToString(pattern, CultureInfo.InvariantCulture);
 1122                Console.WriteLine(DateTime.ParseExact(dts, pattern, CultureInfo.InvariantCulture));
 1123                return true;
 124            }
 0125            catch (Exception)
 126            {
 0127                return false;
 128            }
 129
 1130        }
 1131        public override bool IsPatternValid(FormatDescriptor format) => isPatternValid(format.pattern!);
 132
 133    }
 134}