Ejemplo n.º 1
0
        public virtual string GetPeriod(DateTime start, DateTime end, TimeSpan duration)
        {
            if (end < start)
            {
                throw new ArgumentOutOfRangeException("end");
            }

            bool startHasTimeOfDay = TimeTool.HasTimeOfDay(start);

            // no duration - schow start date (optionally with the time part)
            if (duration == TimeSpec.MinPeriodDuration)
            {
                return(startHasTimeOfDay ? GetDateTime(start) : GetShortDate(start));
            }

            // within one day: show full start, end time and suration
            if (TimeCompare.IsSameDay(start, end))
            {
                return(GetDateTime(start) + StartEndSeparator + GetLongTime(end) + DurationSeparator + GetDuration(duration));
            }

            // show start date, end date and duration (optionally with the time part)
            bool   endHasTimeOfDay = TimeTool.HasTimeOfDay(end);
            bool   hasTimeOfDays   = startHasTimeOfDay || endHasTimeOfDay;
            string startPart       = hasTimeOfDays ? GetDateTime(start) : GetShortDate(start);
            string endPart         = hasTimeOfDays ? GetDateTime(end) : GetShortDate(end);

            return(startPart + StartEndSeparator + endPart + DurationSeparator + GetDuration(duration));
        }
Ejemplo n.º 2
0
        private int CalcWeeks()
        {
            if (TimeCompare.IsSameDay(Date1, Date2))
            {
                return(0);
            }

            DateTime week1 = TimeTool.GetStartOfWeek(Date1, FirstDayOfWeek);
            DateTime week2 = TimeTool.GetStartOfWeek(Date2, FirstDayOfWeek);

            if (week1.Equals(week2))
            {
                return(0);
            }

            return((int)(week2.Subtract(week1).TotalDays / TimeSpec.DaysPerWeek));
        }
Ejemplo n.º 3
0
        private int CalcMonths()
        {
            if (TimeCompare.IsSameDay(Date1, Date2))
            {
                return(0);
            }

            int compareDay          = Date2.Day;
            int compareDaysPerMonth = Calendar.GetDaysInMonth(Year1, Month1);

            if (compareDay > compareDaysPerMonth)
            {
                compareDay = compareDaysPerMonth;
            }

            DateTime compareDate = new DateTime(Year1, Month1, compareDay,
                                                Date2.Hour, Date2.Minute, Date2.Second, Date2.Millisecond);

            if (Date2 > Date1)
            {
                if (compareDate < Date1)
                {
                    compareDate = compareDate.AddMonths(1);
                }
            }
            else
            {
                if (compareDate > Date1)
                {
                    compareDate = compareDate.AddMonths(-1);
                }
            }
            return
                (((Year2 * TimeSpec.MonthsPerYear) + Month2) -
                 ((Calendar.GetYear(compareDate) * TimeSpec.MonthsPerYear) + Calendar.GetMonth(compareDate)));
        }
Ejemplo n.º 4
0
        public string GetInterval(DateTime start, DateTime end,
                                  IntervalEdge startEdge, IntervalEdge endEdge, TimeSpan duration)
        {
            if (end < start)
            {
                throw new ArgumentOutOfRangeException("end");
            }

            StringBuilder sb = new StringBuilder();

            // interval start
            switch (startEdge)
            {
            case IntervalEdge.Closed:
                sb.Append(IntervalStartClosed);
                break;

            case IntervalEdge.Open:
                sb.Append(UseIsoIntervalNotation ? IntervalStartOpenIso : IntervalStartOpen);
                break;
            }

            bool addDuration       = true;
            bool startHasTimeOfDay = TimeTool.HasTimeOfDay(start);

            // no duration - schow start date (optionally with the time part)
            if (duration == TimeSpec.MinPeriodDuration)
            {
                sb.Append(startHasTimeOfDay ? GetDateTime(start) : GetShortDate(start));
                addDuration = false;
            }
            // within one day: show full start, end time and suration
            else if (TimeCompare.IsSameDay(start, end))
            {
                sb.Append(GetDateTime(start));
                sb.Append(StartEndSeparator);
                sb.Append(GetLongTime(end));
            }
            else
            {
                bool endHasTimeOfDay = TimeTool.HasTimeOfDay(start);
                bool hasTimeOfDays   = startHasTimeOfDay || endHasTimeOfDay;
                if (hasTimeOfDays)
                {
                    sb.Append(GetDateTime(start));
                    sb.Append(StartEndSeparator);
                    sb.Append(GetDateTime(end));
                }
                else
                {
                    sb.Append(GetShortDate(start));
                    sb.Append(StartEndSeparator);
                    sb.Append(GetShortDate(end));
                }
            }

            // interval end
            switch (endEdge)
            {
            case IntervalEdge.Closed:
                sb.Append(IntervalEndClosed);
                break;

            case IntervalEdge.Open:
                sb.Append(UseIsoIntervalNotation ? IntervalEndOpenIso : IntervalEndOpen);
                break;
            }

            // duration
            if (addDuration)
            {
                sb.Append(DurationSeparator);
                sb.Append(GetDuration(duration));
            }

            return(sb.ToString());
        }