Exemplo n.º 1
0
        public TimeSpan ToTimeSpan(string pattern)
        {
            try
            {
                _isTagTimeFound = false;
                pattern         = pattern.ToUpper().Trim();

                if (!pattern.StartsWith(Constants.TAG_PERIOD))
                {
                    throw new Iso8601DurationException(EXCEPTION_PATTERN_SHOULD_START_BY_P);
                }

                pattern = pattern.Substring(1);

                if (pattern.Length == 0)
                {
                    return(new TimeSpan(0));
                }

                _isMonthBeforeTime = pattern.IsMonthBeforeTime();

                if (pattern.Length > 0 &&
                    pattern.StartsWith(Constants.TAG_TIME))
                {
                    _isTagTimeFound = true;
                    pattern         = pattern.Substring(1);
                }

                var durationStruct = new DurationStruct();

                if (!_isTagTimeFound)
                {
                    durationStruct.Years = GetValue(Constants.TAG_YEARS, ref pattern);
                    if (_isMonthBeforeTime)
                    {
                        durationStruct.Months = GetValue(Constants.TAG_MONTHS, ref pattern);
                    }
                    durationStruct.Days = GetValue(Constants.TAG_DAYS, ref pattern);
                }

                durationStruct.Hours   = GetValue(Constants.TAG_HOURS, ref pattern);
                durationStruct.Minutes = GetValue(Constants.TAG_MINUTES, ref pattern);
                durationStruct.Seconds = GetValue(Constants.TAG_SECONDS, ref pattern);

                return(ToTimeSpan(durationStruct));
            }
            catch (Exception ex)
            {
                throw new Iso8601DurationException(EXCEPTION_GENERIC_ERROR, ex);
            }
        }
Exemplo 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 minutes = durationStruct.Minutes / Constants.MINUTES_PER_HOUR;
                durationStruct.Hours   += minutes;
                durationStruct.Minutes -= minutes * 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.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.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);
        }
Exemplo 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));
        }
Exemplo n.º 4
0
 public string ToString(DurationStruct durationStruct)
 {
     return(NormalizeDuration(GetPatternFromDurationStruct(durationStruct)));
 }