Ejemplo n.º 1
0
 public static DateTime Add(this DateTime date, DurationStruct duration)
 {
     return(date
            .AddYears(duration.Years)
            .AddMonths(duration.Months)
            .AddDays(duration.Days)
            .AddHours(duration.Hours)
            .AddMinutes(duration.Minutes)
            .AddSeconds(duration.Seconds));
 }
Ejemplo n.º 2
0
        private string GetPatternFromDurationStruct(DurationStruct durationStruct)
        {
            string pattern = Constants.TAG_PERIOD;

            if (durationStruct.Seconds >= Constants.SECONDS_PER_MINUTE)
            {
                int seconds = durationStruct.Seconds / Constants.SECONDS_PER_MINUTE;
                durationStruct.Minutes += seconds;
                durationStruct.Seconds -= seconds * Constants.SECONDS_PER_MINUTE;
            }

            if (durationStruct.Minutes >= Constants.MINUTES_PER_HOUR)
            {
                int hours = durationStruct.Minutes / Constants.MINUTES_PER_HOUR;
                durationStruct.Hours   += hours;
                durationStruct.Minutes -= hours * Constants.MINUTES_PER_HOUR;
            }

            if (durationStruct.Hours >= Constants.HOURS_PER_DAY)
            {
                int days = durationStruct.Hours / Constants.HOURS_PER_DAY;
                durationStruct.Days  += days;
                durationStruct.Hours -= days * Constants.HOURS_PER_DAY;
            }

            if (durationStruct.Days >= Constants.DAYS_PER_MONTH)
            {
                int years  = durationStruct.Days / Constants.DAYS_PER_YEAR;
                int days   = durationStruct.Days - (years * Constants.DAYS_PER_YEAR);
                int months = (days / Constants.DAYS_PER_MONTH);

                durationStruct.Years  += years;
                durationStruct.Months += months;
                durationStruct.Days   -= (years * Constants.DAYS_PER_YEAR) + (months * Constants.DAYS_PER_MONTH);
            }

            if (durationStruct.Months >= Constants.MONTHS_PER_YEAR)
            {
                int years  = durationStruct.Months / Constants.MONTHS_PER_YEAR;
                int months = durationStruct.Months - (years * Constants.MONTHS_PER_YEAR);

                durationStruct.Years += years;
                durationStruct.Months = months;
            }

            pattern += (durationStruct.Years > 0 ? durationStruct.Years + Constants.TAG_YEARS : string.Empty);
            pattern += (durationStruct.Months > 0 ? durationStruct.Months + Constants.TAG_MONTHS : string.Empty);
            pattern += (durationStruct.Days > 0 ? durationStruct.Days + Constants.TAG_DAYS : string.Empty);

            var patternTime = string.Empty;

            patternTime += (durationStruct.Hours > 0 ? durationStruct.Hours + Constants.TAG_HOURS : string.Empty);
            patternTime += (durationStruct.Minutes > 0 ? durationStruct.Minutes + Constants.TAG_MINUTES : string.Empty);
            patternTime += (durationStruct.Seconds > 0 ? durationStruct.Seconds + Constants.TAG_SECONDS : string.Empty);

            if (!string.IsNullOrEmpty(patternTime))
            {
                pattern += Constants.TAG_TIME + patternTime;
            }

            return(pattern);
        }
Ejemplo n.º 3
0
        public TimeSpan ToTimeSpan(DurationStruct durationStruct)
        {
            durationStruct.Days = CalculateDays(durationStruct.Years, durationStruct.Months, durationStruct.Days);

            return(new TimeSpan(durationStruct.Days, durationStruct.Hours, durationStruct.Minutes, durationStruct.Seconds));
        }
Ejemplo n.º 4
0
        public DurationStruct ToDurationStruct(string pattern)
        {
            if (pattern.IsNullOrEmpty() ||
                pattern == Constants.TAG_PERIOD ||                 // ISO8601 doesn't allow "P" to indicate a TimeSpan of 0
                pattern.EndsWith(Constants.TAG_TIME))                    // Ensure time pattern contains any data
            {
                throw new Iso8601DurationException(EXCEPTION_PATTERN_NOT_VALID);
            }

            try
            {
                var regex = new Regex(DURATION_EXPRESSION_REGEX);
                var match = regex.Match(pattern);
                if (!match.Success)
                {
                    throw new Iso8601DurationException(EXCEPTION_PATTERN_NOT_VALID);
                }

                var years   = match.Groups["years"];
                var months  = match.Groups["months"];
                var weeks   = match.Groups["weeks"];
                var days    = match.Groups["days"];
                var hours   = match.Groups["hours"];
                var minutes = match.Groups["minutes"];
                var seconds = match.Groups["seconds"];

                var durationStruct = new DurationStruct();
                if (years.Length > 0)
                {
                    durationStruct.Years = int.Parse(years.Value);
                }
                if (months.Length > 0)
                {
                    durationStruct.Months = int.Parse(months.Value);
                }
                // Convert Weeks to Days to avoid precission loss when doing calculations with DateTimes
                if (weeks.Length > 0)
                {
                    durationStruct.Days += int.Parse(weeks.Value) * Constants.DAYS_PER_WEEK;
                }
                if (days.Length > 0)
                {
                    durationStruct.Days += int.Parse(days.Value);
                }
                if (hours.Length > 0)
                {
                    durationStruct.Hours = int.Parse(hours.Value);
                }
                if (minutes.Length > 0)
                {
                    durationStruct.Minutes = int.Parse(minutes.Value);
                }
                if (seconds.Length > 0)
                {
                    durationStruct.Seconds = int.Parse(seconds.Value);
                }

                return(durationStruct);
            }
            catch (Exception ex)
            {
                throw new Iso8601DurationException(EXCEPTION_GENERIC_ERROR, ex);
            }
        }
Ejemplo n.º 5
0
 public string ToString(DurationStruct durationStruct)
 {
     return(GetPatternFromDurationStruct(durationStruct));
 }