public TimeContext Apply(TextBox textBox, TimeFormatSpecifier[] formatSpecifiers, out int start, out int length)
        {
            // No value or no text selected then prevent increment
            if (string.IsNullOrWhiteSpace(textBox.SelectedText))
            {
                start  = -1;
                length = -1;
                return(null);
            }

            string text = textBox.SelectedText.TrimEnd();

            if (!Regex.IsMatch(text, "^[0-9]*$"))
            {
                start  = -1;
                length = -1;
                return(null);
            }

            start  = textBox.SelectionStart;
            length = text.Length;

            int numberOfPreviousNumbers = 0;

            for (int i = 0; i < textBox.Text.Length; i++)
            {
                if (i == start) // Found start index
                {
                    break;
                }

                if (char.IsDigit(textBox.Text[i]))
                {
                    continue;
                }

                numberOfPreviousNumbers++; // Must have encountered as seperator. Inc
            }

            TimeFormatSpecifier formatSpecifier = formatSpecifiers
                                                  .FirstOrDefault(x => x.Index == numberOfPreviousNumbers);
            TimeContext dateTimeContext = new TimeContext(formatSpecifier);

            return(dateTimeContext);
        }
        public IEnumerable <TimeFormatSpecifier> CalculateTimeFormatSpecifiers(string formatString)
        {
            // The user has specified a custom string
            if (string.IsNullOrWhiteSpace(formatString))
            {
                return(null);
            }

            if (!ValidStringFormat(formatString))
            {
                return(null);
            }

            /*
             * Validated a valid DateTime Format from the user.
             * Calculate the positions of each data type.
             */
            ITimeFormatSpecifierFactory factory = new TimeFormatSpecifierFactory();

            int index = 0;

            string[] specifiers = formatString.Split(FormatStringSeperators.Seperators);
            IList <TimeFormatSpecifier> formatSpecifiers = new List <TimeFormatSpecifier>();

            foreach (string specifier in specifiers)
            {
                string s = specifier;
                if (!(s.Equals("HH") || s.Equals("mm") || s.Equals("ss")))
                {
                    continue;
                }

                TimeFormatSpecifier formatSpecifier = factory.CreateFormatSpecifier(specifier, index);
                formatSpecifiers.Add(formatSpecifier);
                index++;
            }

            return(formatSpecifiers);
        }
Esempio n. 3
0
 public TimeContext(TimeFormatSpecifier timeFormatSpecifier)
 {
     _formatSpecifier = timeFormatSpecifier;
 }