Пример #1
0
            /// <summary>
            /// Parses a string into a <see cref="TimerStartToken"/>.
            /// </summary>
            /// <param name="str">A string representation of a <see cref="TimerStartToken"/>.</param>
            /// <param name="provider">An <see cref="IFormatProvider"/>.</param>
            /// <returns>The <see cref="TimerStartToken"/> parsed from the string.</returns>
            /// <exception cref="ArgumentNullException">If <paramref name="str"/> or <paramref name="provider"/> is
            /// <c>null</c>.</exception>
            /// <exception cref="FormatException">If <paramref name="str"/> is not a supported representation of a <see
            /// cref="TimerStartToken"/>.</exception>
            protected override TimerStartToken ParseInternal(string str, IFormatProvider provider)
            {
                provider = Resources.ResourceManager.GetEffectiveProvider(provider);

                foreach (string pattern in GetPatterns(provider))
                {
                    try
                    {
                        Match match = Regex.Match(str, pattern, RegexOptions);
                        if (match.Success)
                        {
                            TimeSpanToken timeSpanToken = new TimeSpanToken();

                            // Years
                            foreach (Capture capture in match.Groups["years"].Captures)
                            {
                                timeSpanToken.Years += double.Parse(capture.Value, provider);
                            }

                            // Months
                            foreach (Capture capture in match.Groups["months"].Captures)
                            {
                                timeSpanToken.Months += double.Parse(capture.Value, provider);
                            }

                            // Weeks
                            foreach (Capture capture in match.Groups["weeks"].Captures)
                            {
                                timeSpanToken.Weeks += double.Parse(capture.Value, provider);
                            }

                            // Days
                            foreach (Capture capture in match.Groups["days"].Captures)
                            {
                                timeSpanToken.Days += double.Parse(capture.Value, provider);
                            }

                            // Hours
                            foreach (Capture capture in match.Groups["hours"].Captures)
                            {
                                timeSpanToken.Hours += double.Parse(capture.Value, provider);
                            }

                            // Minutes
                            foreach (Capture capture in match.Groups["minutes"].Captures)
                            {
                                timeSpanToken.Minutes += double.Parse(capture.Value, provider);
                            }

                            // Seconds
                            foreach (Capture capture in match.Groups["seconds"].Captures)
                            {
                                timeSpanToken.Seconds += double.Parse(capture.Value, provider);
                            }

                            return timeSpanToken;
                        }
                    }
                    catch
                    {
                        // Try the next pattern
                        continue;
                    }
                }

                // Could not find a matching pattern
                throw new FormatException();
            }
Пример #2
0
        public void GetEndTimeWith1Point5YearsAnd20150101At000000Returns20160701At000000()
        {
            // Arrange
            DateTime startTime = new DateTime(2015, 1, 1, 0, 0, 0);
            TimeSpanToken timeSpanToken = new TimeSpanToken { Years = 1.5 };

            // Act
            DateTime endTime = timeSpanToken.GetEndTime(startTime);

            // Assert
            Assert.AreEqual(new DateTime(2016, 7, 1, 0, 0, 0), endTime);
        }
Пример #3
0
            /// <summary>
            /// Parses a string into a <see cref="TimerStartToken"/>.
            /// </summary>
            /// <param name="str">A string representation of a <see cref="TimerStartToken"/>.</param>
            /// <param name="provider">An <see cref="IFormatProvider"/>.</param>
            /// <returns>The <see cref="TimerStartToken"/> parsed from the string.</returns>
            /// <exception cref="ArgumentNullException">If <paramref name="str"/> or <paramref name="provider"/> is
            /// <c>null</c>.</exception>
            /// <exception cref="FormatException">If <paramref name="str"/> is not a supported representation of a <see
            /// cref="TimerStartToken"/>.</exception>
            protected override TimerStartToken ParseInternal(string str, IFormatProvider provider)
            {
                provider = Resources.ResourceManager.GetEffectiveProvider(provider);

                foreach (string pattern in GetPatterns(provider))
                {
                    try
                    {
                        Match match = Regex.Match(str, pattern, RegexOptions);
                        if (match.Success)
                        {
                            TimeSpanToken timeSpanToken = new TimeSpanToken();

                            // Years
                            foreach (Capture capture in match.Groups["years"].Captures)
                            {
                                timeSpanToken.Years += double.Parse(capture.Value, provider);
                            }

                            // Months
                            foreach (Capture capture in match.Groups["months"].Captures)
                            {
                                timeSpanToken.Months += double.Parse(capture.Value, provider);
                            }

                            // Weeks
                            foreach (Capture capture in match.Groups["weeks"].Captures)
                            {
                                timeSpanToken.Weeks += double.Parse(capture.Value, provider);
                            }

                            // Days
                            foreach (Capture capture in match.Groups["days"].Captures)
                            {
                                timeSpanToken.Days += double.Parse(capture.Value, provider);
                            }

                            // Hours
                            foreach (Capture capture in match.Groups["hours"].Captures)
                            {
                                timeSpanToken.Hours += double.Parse(capture.Value, provider);
                            }

                            // Minutes
                            foreach (Capture capture in match.Groups["minutes"].Captures)
                            {
                                timeSpanToken.Minutes += double.Parse(capture.Value, provider);
                            }

                            // Seconds
                            foreach (Capture capture in match.Groups["seconds"].Captures)
                            {
                                timeSpanToken.Seconds += double.Parse(capture.Value, provider);
                            }

                            return(timeSpanToken);
                        }
                    }
                    catch
                    {
                        // Try the next pattern
                        continue;
                    }
                }

                // Could not find a matching pattern
                throw new FormatException();
            }
Пример #4
0
        public void GetEndTimeWith5MonthsAnd20150101At000000Returns20150601At000000()
        {
            // Arrange
            DateTime startTime = new DateTime(2015, 1, 1, 0, 0, 0);
            TimeSpanToken timeSpanToken = new TimeSpanToken { Months = 5 };

            // Act
            DateTime endTime = timeSpanToken.GetEndTime(startTime);

            // Assert
            Assert.AreEqual(new DateTime(2015, 6, 1, 0, 0, 0), endTime);
        }