Ejemplo n.º 1
0
        public override bool Equals(object obj)
        {
            Iso8601Duration other = obj as Iso8601Duration;

            if (null == other)
            {
                return(false);
            }
            long otherTicks = other.ToTimeSpan().Ticks;
            long ticks      = ToTimeSpan().Ticks;

            return(otherTicks == ticks);
        }
Ejemplo n.º 2
0
        public static TimeSpan XmlSchemaDurationToTimeSpan(string duration)
        {
            // return SoapDuration.Parse(duration);
            //
            // This would be the .NET built-in helper
            // but I examined the code using Reflector and
            // I don't like it as much as mine.
            // For what it's worth, the SoapDuration Parse method
            // converts years to 360 days,
            // months to 30 days, and ignores weeks

            Iso8601Duration iso8601Duration = Iso8601Duration.Parse(duration);

            return(iso8601Duration.ToTimeSpan());
        }
Ejemplo n.º 3
0
 public static TimeSpan ToTimeSpan(Iso8601Duration duration)
 {
     return duration.ToTimeSpan();
 }
Ejemplo n.º 4
0
        public static Iso8601Duration Parse(string duration)
        {
            MatchCollection matches = RegexDuration.Matches(duration);
            if (!(null != matches && matches.Count > 0))
            {
                return null;
            }

            Debug.Assert(matches.Count == 1);

            Match match = matches[0];
            if (!match.Success)
            {
                return null;
            }

            GroupCollection groups = match.Groups;
            if (!(null != groups && groups.Count > 0))
            {
                return null;
            }

            int signum = 1;
            int years = 0;
            int months = 0;
            int weeks = 0;
            int days = 0;
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            int fraction = 0;

            Group signGroup = groups["sign"];
            string signValue = signGroup.Value;
            if (!string.IsNullOrEmpty(signValue))
            {
                if (signValue.Equals("-"))
                {
                    signum = -1;
                }
            }

            Group yearsGroup = groups["years"];
            string yearsValue = yearsGroup.Value;
            if (!string.IsNullOrEmpty(yearsValue))
            {
                if (!int.TryParse(yearsValue, out years))
                {
                    throw new ArgumentException("Invalid years value in duration", "duration");
                }
            }

            Group monthsGroup = groups["months"];
            string monthsValue = monthsGroup.Value;
            if (!string.IsNullOrEmpty(monthsValue))
            {
                if (!int.TryParse(monthsValue, out months))
                {
                    throw new ArgumentException("Invalid months value in duration", "duration");
                }
            }

            Group weeksGroup = groups["weeks"];
            string weeksValue = weeksGroup.Value;
            if (!string.IsNullOrEmpty(weeksValue))
            {
                if (!int.TryParse(weeksValue, out weeks))
                {
                    throw new ArgumentException("Invalid weeks value in duration", "duration");
                }
            }

            Group daysGroup = groups["days"];
            string daysValue = daysGroup.Value;
            if (!string.IsNullOrEmpty(daysValue))
            {
                if (!int.TryParse(daysValue, out days))
                {
                    throw new ArgumentException("Invalid days value in duration", "duration");
                }
            }

            Group hoursGroup = groups["hours"];
            string hoursValue = hoursGroup.Value;
            if (!string.IsNullOrEmpty(hoursValue))
            {
                if (!int.TryParse(hoursValue, out hours))
                {
                    throw new ArgumentException("Invalid hours value in duration", "duration");
                }
            }

            Group minutesGroup = groups["minutes"];
            string minutesValue = minutesGroup.Value;
            if (!string.IsNullOrEmpty(minutesValue))
            {
                if (!int.TryParse(minutesValue, out minutes))
                {
                    throw new ArgumentException("Invalid minutes value in duration", "duration");
                }
            }

            Group secondsGroup = groups["seconds"];
            string secondsValue = secondsGroup.Value;
            if (!string.IsNullOrEmpty(secondsValue))
            {
                if (!int.TryParse(secondsValue, out seconds))
                {
                    throw new ArgumentException("Invalid seconds value in duration", "duration");
                }
            }

            Group fractionGroup = groups["fraction"];
            string fractionValue = fractionGroup.Value;
            if (!string.IsNullOrEmpty(fractionValue))
            {
                if (!int.TryParse(fractionValue, out fraction))
                {
                    throw new ArgumentException("Invalid seconds fraction value in duration", "duration");
                }
            }

            Iso8601Duration iso8601Duration = new Iso8601Duration();
            iso8601Duration.Signum = signum;
            iso8601Duration.Years = years;
            iso8601Duration.Months = months;
            iso8601Duration.Weeks = weeks;
            iso8601Duration.Days = days;
            iso8601Duration.Hours = hours;
            iso8601Duration.Minutes = minutes;
            iso8601Duration.Seconds = seconds;
            iso8601Duration.FractionOfSecond = decimal.Parse("0." + fraction.ToString());

            return iso8601Duration;
        }
Ejemplo n.º 5
0
 public static TimeSpan ToTimeSpan(Iso8601Duration duration)
 {
     return(duration.ToTimeSpan());
 }
Ejemplo n.º 6
0
        public static Iso8601Duration Parse(string duration)
        {
            MatchCollection matches = RegexDuration.Matches(duration);

            if (!(null != matches && matches.Count > 0))
            {
                return(null);
            }

            Debug.Assert(matches.Count == 1);

            Match match = matches[0];

            if (!match.Success)
            {
                return(null);
            }

            GroupCollection groups = match.Groups;

            if (!(null != groups && groups.Count > 0))
            {
                return(null);
            }

            int signum   = 1;
            int years    = 0;
            int months   = 0;
            int weeks    = 0;
            int days     = 0;
            int hours    = 0;
            int minutes  = 0;
            int seconds  = 0;
            int fraction = 0;

            Group  signGroup = groups["sign"];
            string signValue = signGroup.Value;

            if (!string.IsNullOrEmpty(signValue))
            {
                if (signValue.Equals("-"))
                {
                    signum = -1;
                }
            }

            Group  yearsGroup = groups["years"];
            string yearsValue = yearsGroup.Value;

            if (!string.IsNullOrEmpty(yearsValue))
            {
                if (!int.TryParse(yearsValue, out years))
                {
                    throw new ArgumentException("Invalid years value in duration", "duration");
                }
            }

            Group  monthsGroup = groups["months"];
            string monthsValue = monthsGroup.Value;

            if (!string.IsNullOrEmpty(monthsValue))
            {
                if (!int.TryParse(monthsValue, out months))
                {
                    throw new ArgumentException("Invalid months value in duration", "duration");
                }
            }

            Group  weeksGroup = groups["weeks"];
            string weeksValue = weeksGroup.Value;

            if (!string.IsNullOrEmpty(weeksValue))
            {
                if (!int.TryParse(weeksValue, out weeks))
                {
                    throw new ArgumentException("Invalid weeks value in duration", "duration");
                }
            }

            Group  daysGroup = groups["days"];
            string daysValue = daysGroup.Value;

            if (!string.IsNullOrEmpty(daysValue))
            {
                if (!int.TryParse(daysValue, out days))
                {
                    throw new ArgumentException("Invalid days value in duration", "duration");
                }
            }

            Group  hoursGroup = groups["hours"];
            string hoursValue = hoursGroup.Value;

            if (!string.IsNullOrEmpty(hoursValue))
            {
                if (!int.TryParse(hoursValue, out hours))
                {
                    throw new ArgumentException("Invalid hours value in duration", "duration");
                }
            }

            Group  minutesGroup = groups["minutes"];
            string minutesValue = minutesGroup.Value;

            if (!string.IsNullOrEmpty(minutesValue))
            {
                if (!int.TryParse(minutesValue, out minutes))
                {
                    throw new ArgumentException("Invalid minutes value in duration", "duration");
                }
            }

            Group  secondsGroup = groups["seconds"];
            string secondsValue = secondsGroup.Value;

            if (!string.IsNullOrEmpty(secondsValue))
            {
                if (!int.TryParse(secondsValue, out seconds))
                {
                    throw new ArgumentException("Invalid seconds value in duration", "duration");
                }
            }

            Group  fractionGroup = groups["fraction"];
            string fractionValue = fractionGroup.Value;

            if (!string.IsNullOrEmpty(fractionValue))
            {
                if (!int.TryParse(fractionValue, out fraction))
                {
                    throw new ArgumentException("Invalid seconds fraction value in duration", "duration");
                }
            }

            Iso8601Duration iso8601Duration = new Iso8601Duration();

            iso8601Duration.Signum           = signum;
            iso8601Duration.Years            = years;
            iso8601Duration.Months           = months;
            iso8601Duration.Weeks            = weeks;
            iso8601Duration.Days             = days;
            iso8601Duration.Hours            = hours;
            iso8601Duration.Minutes          = minutes;
            iso8601Duration.Seconds          = seconds;
            iso8601Duration.FractionOfSecond = decimal.Parse("0." + fraction.ToString());

            return(iso8601Duration);
        }