Ejemplo n.º 1
0
        public static Time Add(Time x, DesignatedDuration y)
        {
            if (y.Years != null)
            {
                throw new InvalidOperationException("The Years component of the DesignatedDuration cannot be added to a Time.");
            }

            if (y.Months != null)
            {
                throw new InvalidOperationException("The Months component of the DesignatedDuration cannot be added to a Time.");
            }

            if (y.Days != null)
            {
                throw new InvalidOperationException("The Days component of the DesignatedDuration cannot be added to a Time.");
            }

            var second = x.Second;
            var minute = x.Minute;
            var hour = x.Hour;
            var precision = x.Precision;

            if (y.Seconds != null)
            {
                second += y.Seconds.Value;

                if (hour != (int)hour)
                {
                    minute += (hour - (int)hour) * 60;
                }

                if (minute != (int)minute)
                {
                    second += (minute - (int)minute) * 60;
                }

                while (second > 59)
                {
                    second -= 60;
                    minute++;
                }

                if (precision < TimePrecision.Second)
                {
                    precision = TimePrecision.Second;
                }
            }

            if (y.Minutes != null)
            {
                precision = TimePrecision.Minute;
                minute += y.Minutes.Value;

                if (hour != (int)hour)
                {
                    minute += (hour - (int)hour) * 60;
                }

                while (minute > 59)
                {
                    minute -= 60;
                    hour++;
                }

                if (precision < TimePrecision.Minute)
                {
                    precision = TimePrecision.Minute;
                }
            }

            if (y.Hours != null)
            {
                hour += y.Hours.Value;
            }

            switch (precision)
            {
                case TimePrecision.Hour:
                    return new Time(hour);

                case TimePrecision.Minute:
                    return new Time((int)hour, minute);

                case TimePrecision.Second:
                    return new Time((int)hour, (int)minute, second);

                default:
                    return null;
            }
        }
Ejemplo n.º 2
0
        public static CalendarDate Add(CalendarDate x, DesignatedDuration y)
        {
            if (y.Hours != null)
            {
                throw new InvalidOperationException("The Hours component of a DesignatedDuration cannot be added to a CalendarDate.");
            }

            if (y.Minutes != null)
            {
                throw new InvalidOperationException("The Minutes component of a DesignatedDuration cannot be added to a CalendarDate.");
            }

            if (y.Seconds != null)
            {
                throw new InvalidOperationException("The Seconds component of a DesignatedDuration cannot be added to a CalendarDate.");
            }

            double day = x.Day;
            double month = x.Month;
            double year = x.Year;
            long century = x.Century;
            var precision = x.Precision;

            if (y.Years != null)
            {
                century += (long)y.Years.Value / 100;
                year += y.Years.Value;
            }

            if (y.Months != null)
            {
                month += (int)y.Months.Value;

                if (year != (int)year)
                {
                    month += (year - (int)year) * 60;
                }

                if (precision < CalendarDatePrecision.Month)
                {
                    precision = CalendarDatePrecision.Month;
                }
            }
            else if (precision == CalendarDatePrecision.Year && year != (int)year)
            {
                throw new InvalidOperationException("The Year value of a CalendarDate cannot be fractional.");
            }

            if (y.Days != null)
            {
                day += y.Days.Value;

                if (month != (int)month)
                {
                    day += (month - (int)month) * 60;
                }

                if (y.Days != (int)y.Days)
                {
                    throw new InvalidOperationException("The Day value of a CalendarDate cannot be fractional.");
                }

                if (precision < CalendarDatePrecision.Day)
                {
                    precision = CalendarDatePrecision.Day;
                }
            }
            else if (precision == CalendarDatePrecision.Month && month != (int)month)
            {
                throw new InvalidOperationException("The Month value of a CalendarDate cannot be fractional.");
            }

            while ((int)month > 12)
            {
                month -= 12;
                year++;
            }

            int daysInMonth = DaysInMonth((long)year, (int)month);

            while (day > daysInMonth)
            {
                day -= daysInMonth;
                month++;

                daysInMonth = DaysInMonth((long)year, (int)month);
            }

            switch (precision)
            {
                case CalendarDatePrecision.Century:
                    return CalendarDate.FromCentury(century);

                case CalendarDatePrecision.Year:
                    return new CalendarDate((int)year);

                case CalendarDatePrecision.Month:
                    return new CalendarDate((long)year, (int)month);

                case CalendarDatePrecision.Day:
                    return new CalendarDate((long)year, (int)month, (int)day);

                default:
                    return null;
            }
        }
Ejemplo n.º 3
0
        public static CalendarDateTime Add(CalendarDateTime x, DesignatedDuration y)
        {
            double day = x.Day;
            double month = x.Month;
            double year = x.Year;
            var second = x.Second;
            var minute = x.Minute;
            var hour = x.Hour;
            var precision = x.Precision;

            if (y.Years != null)
            {
                year += y.Years.Value;
            }

            if (y.Months != null)
            {
                month += y.Months.Value;

                if (year != (int)year)
                {
                    month += (year - (int)year) * 60;
                }
            }

            if (y.Days != null)
            {
                day += y.Days.Value;

                if (month != (int)month)
                {
                    day += (month - (int)month) * 60;
                }
            }

            if (y.Hours != null)
            {
                hour += y.Hours.Value;

                if (day != (int)day)
                {
                    hour += (day - (int)day) * 24;
                }
            }

            if (y.Minutes != null)
            {
                minute += y.Minutes.Value;

                if (hour != (int)hour)
                {
                    minute += (hour - (int)hour) * 60;
                }

                if (precision < TimePrecision.Minute)
                {
                    precision = TimePrecision.Minute;
                }
            }

            if (y.Seconds != null)
            {
                second += y.Seconds.Value;

                if (minute != (int)minute)
                {
                    second += (minute - (int)minute) * 60;
                }

                if (precision < TimePrecision.Second)
                {
                    precision = TimePrecision.Second;
                }
            }

            while (!(second < 60))
            {
                second -= 60;
                minute++;
            }

            while ((int)minute > 59 || (precision == TimePrecision.Minute && !(minute < 60d)))
            {
                minute -= 60;
                hour++;
            }

            while ((int)hour > 23 || (precision == TimePrecision.Hour && hour >= 24d))
            {
                hour -= 24;
                day++;
            }

            while ((int)month > 12)
            {
                month -= 12;
                year++;
            }

            int daysInMonth = DaysInMonth((long)year, (int)month);

            while (day > daysInMonth)
            {
                day -= daysInMonth;
                month++;

                daysInMonth = DaysInMonth((long)year, (int)month);
            }

            switch (precision)
            {
                case TimePrecision.Hour:
                    return new CalendarDateTime(new CalendarDate((long)year, (int)month, (int)day), new Time(hour));

                case TimePrecision.Minute:
                    return new CalendarDateTime(new CalendarDate((long)year, (int)month, (int)day), new Time((int)hour, minute));

                case TimePrecision.Second:
                    return new CalendarDateTime(new CalendarDate((long)year, (int)month, (int)day), new Time((int)hour, (int)minute, second));

                default:
                    return null;
            }
        }
Ejemplo n.º 4
0
        public static Time Subtract(Time x, DesignatedDuration y)
        {
            if (y.Years != null)
            {
                throw new InvalidOperationException("The Years component of the DesignatedDuration cannot be subtracted from a Time");
            }

            if (y.Months != null)
            {
                throw new InvalidOperationException("The Months component of the DesignatedDuration cannot be subtracted from a Time");
            }

            if (y.Days != null)
            {
                throw new InvalidOperationException("The Days component of the DesignatedDuration cannot be subtracted from a Time");
            }

            var hour = x.Hour;
            var minute = x.Minute;
            var second = x.Second;
            var precision = x.Precision;

            if (y.Seconds != null)
            {
                second -= y.Seconds.Value;

                if (precision < TimePrecision.Second)
                {
                    precision = TimePrecision.Second;
                }
            }

            if (y.Minutes != null)
            {
                if (y.Minutes != (int)y.Minutes && precision > TimePrecision.Minute)
                {
                    second -= (y.Minutes.Value - (int)y.Minutes.Value) * 60d;
                    minute -= (int)y.Minutes.Value;
                }
                else
                {
                    minute -= y.Minutes.Value;
                }

                if (precision < TimePrecision.Minute)
                {
                    precision = TimePrecision.Minute;
                }
            }

            if (y.Hours != null)
            {
                if (y.Hours != (int)y.Hours && precision > TimePrecision.Hour)
                {
                    minute -= (y.Hours.Value - (int)y.Hours.Value) * 60d;
                    hour -= (int)y.Hours.Value;
                }
                else
                {
                    hour -= y.Hours.Value;
                }
            }

            while (second < -59d)
            {
                second += 60d;
                minute--;
            }

            while (minute < -59d)
            {
                minute += 60d;
                hour--;
            }

            if (hour < 0d)
            {
                throw new InvalidOperationException("The resulting Time is is in the previous day.");
            }

            switch (precision)
            {
                case TimePrecision.Hour:
                    return new Time(hour);

                case TimePrecision.Minute:
                    return new Time((int)hour, minute);

                case TimePrecision.Second:
                    return new Time((int)hour, (int)minute, second);

                default:
                    return null;
            }
        }
Ejemplo n.º 5
0
        public static CalendarDate Subtract(CalendarDate x, DesignatedDuration y)
        {
            long century = x.Century;
            long year = x.Year;
            double month = x.Month;
            double day = x.Day;
            CalendarDatePrecision precision = x.Precision;

            if (y.Seconds != null)
            {
                day += y.Seconds.Value / SecondsPerDay;
            }

            if (y.Minutes != null)
            {
                day += y.Minutes.Value / MinutesPerDay;
            }

            if (y.Hours != null)
            {
                day += y.Hours.Value / HoursPerDay;
            }

            if (y.Days != null)
            {
                day -= y.Days.Value;

                if (precision < CalendarDatePrecision.Day)
                {
                    precision = CalendarDatePrecision.Day;
                }
            }

            if (y.Months != null)
            {
                month -= y.Months.Value;

                if (precision < CalendarDatePrecision.Month)
                {
                    precision = CalendarDatePrecision.Month;
                }
            }

            if (y.Years != null)
            {
                if (y.Years != (int)y.Years)
                {
                    month += (y.Years.Value - (int)y.Years.Value) * MonthsPerYear;
                }

                year -= (long)y.Years.Value;

                if (precision < CalendarDatePrecision.Year)
                {
                    precision = CalendarDatePrecision.Year;
                }
            }

            while (month < 1)
            {
                month += MonthsPerYear;
                year--;
            }

            if (month != (int)month && precision > CalendarDatePrecision.Month)
            {
                day -= (month - (int)month) * DaysInMonth(year, (int)month);
                month = (int)month;
            }

            while (day < 1)
            {
                day += DaysInMonth(year, (int)month);
                month--;
            }

            if (day != (int)day)
            {
                throw new InvalidOperationException("A CalendarDate cannot have a fractional day.");
            }

            if (month != (int)month)
            {
                throw new InvalidOperationException("A CalendarDate cannot have a fractional month.");
            }

            switch (precision)
            {
                case CalendarDatePrecision.Century:
                    return CalendarDate.FromCentury(century);

                case CalendarDatePrecision.Year:
                    return new CalendarDate(year);

                case CalendarDatePrecision.Month:
                    return new CalendarDate(year, (int)month);

                case CalendarDatePrecision.Day:
                    return new CalendarDate(year, (int)month, (int)day);

                default:
                    return null;
            }
        }
Ejemplo n.º 6
0
        public static CalendarDateTime Subtract(CalendarDateTime x, DesignatedDuration y)
        {
            long year = x.Year;
            double month = x.Month;
            double day = x.Day;
            double hour = x.Hour;
            double minute = x.Minute;
            double second = x.Second;
            TimePrecision precision = x.Precision;

            if (y.Years != null)
            {
                year -= (long)y.Years.Value;

                if (y.Years != (int)y.Years)
                {
                    month += (y.Years.Value - (long)y.Years.Value) * MonthsPerYear;
                }
            }

            if (y.Months != null)
            {
                month -= y.Months.Value;

                if (month != (int)month)
                {
                    day += (month - (int)month) * DaysInMonth(year, (int)month);
                    month = (int)month;
                }
            }

            if (y.Days != null)
            {
                day -= (int)y.Days.Value;

                if (day != (int)day)
                {
                    hour += (day - (int)day) * HoursPerDay;
                    day = (int)day;
                }
            }

            if (y.Hours != null)
            {
                hour -= y.Hours.Value;

                if (hour != (int)hour && precision > TimePrecision.Hour)
                {
                    minute += (hour - (int)hour) * MinutesPerHour;
                    hour = (int)hour;
                }
            }

            if (y.Minutes != null)
            {
                minute -= y.Minutes.Value;

                if (minute != (int)minute && precision > TimePrecision.Minute)
                {
                    second += (minute - (int)minute) * SecondsPerMinute;
                    minute = (int)minute;
                }

                if (precision < TimePrecision.Minute)
                {
                    precision = TimePrecision.Minute;
                }
            }

            if (y.Seconds != null)
            {
                second -= y.Seconds.Value;

                if (precision < TimePrecision.Second)
                {
                    precision = TimePrecision.Second;
                }
            }

            while (second <= -SecondsPerMinute)
            {
                second += SecondsPerMinute;
                minute--;
            }

            while (minute <= -MinutesPerHour)
            {
                minute += MinutesPerHour;
                hour--;
            }

            while (hour <= -HoursPerDay)
            {
                hour += HoursPerDay;
                day--;
            }

            while (month < 1)
            {
                month += MonthsPerYear;
                year--;
            }

            while (day < 1)
            {
                day += DaysInMonth(year, (int)month);
                month--;
            }

            switch (precision)
            {
                case TimePrecision.Hour:
                    return new CalendarDateTime(new CalendarDate(year, (int)month, (int)day), new Time(hour));

                case TimePrecision.Minute:
                    return new CalendarDateTime(new CalendarDate(year, (int)month, (int)day), new Time((int)hour, minute));

                case TimePrecision.Second:
                    return new CalendarDateTime(new CalendarDate(year, (int)month, (int)day), new Time((int)hour, (int)minute, second));

                default:
                    return null;
            }
        }