Beispiel #1
0
        public static CalendarDate Add(CalendarDate x, Duration y)
        {
            if (y is CalendarDateDuration)
            {
                return Add(x, (CalendarDateDuration)y);
            }

            if (y is OrdinalDateDuration)
            {
                return Add(x, ((OrdinalDateDuration)y).ToCalendarDateDuration());
            }

            if (y is DesignatedDuration)
            {
                return Add(x, (DesignatedDuration)y);
            }

            throw new InvalidOperationException($"A {y.GetType()} cannot be added to a {x.GetType()}.");
        }
Beispiel #2
0
        public static CalendarDate Subtract(CalendarDate x, Duration y)
        {
            if (y is CalendarDateDuration)
            {
                return Subtract(x, (CalendarDateDuration)y);
            }

            if (y is OrdinalDateDuration)
            {
                return Subtract(x, ((OrdinalDateDuration)y).ToCalendarDateDuration());
            }

            if (y is DesignatedDuration)
            {
                return Subtract(x, (DesignatedDuration)y);
            }

            throw new InvalidOperationException($"A {y.GetType()} cannot be subtracted from a {x.GetType()}.");
        }
Beispiel #3
0
 public static int DayOfYear(CalendarDate calendarDate)
 {
     return (IsLeapYear(calendarDate.Year) ? DaysToMonth366[calendarDate.Month] : DaysToMonth365[calendarDate.Month]) + calendarDate.Day;
 }
Beispiel #4
0
        // http://www.stoimen.com/blog/2012/04/24/computer-algorithms-how-to-determine-the-day-of-the-week/
        public static DayOfWeek DayOfWeek(CalendarDate calendarDate)
        {
            var yearOfCentury = YearOfCentury(calendarDate.Year);

            return (DayOfWeek)(
                         (calendarDate.Day
                       + (IsLeapYear(calendarDate.Year) ? DayOfWeekMonthKeys366[calendarDate.Month] : DayOfWeekMonthKeys365[calendarDate.Month])
                       + yearOfCentury + yearOfCentury / 4
                       + DayOfWeekCenturyKeys[calendarDate.Century % 4 + calendarDate.Century < 0 ? 4 : 0])
                     % 7);
        }
Beispiel #5
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;
            }
        }
Beispiel #6
0
 internal static int WeekOfYear(CalendarDate calendarDate)
 {
     return (DayOfYear(calendarDate) - (int)DayOfWeek(calendarDate) + 10) / 7;
 }
Beispiel #7
0
        public static CalendarDate Add(CalendarDate x, CalendarDateDuration y)
        {
            var day = x.Day;
            var month = x.Month;
            var year = x.Year;
            var century = x.Century;
            var precision = x.Precision;

            if (y.Centuries != null)
            {
                century += y.Centuries.Value;
            }

            year += y.Years;

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

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

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

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

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

            var daysInMonth = DaysInMonth(year, month);

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

                daysInMonth = DaysInMonth(year, month);
            }

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

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

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

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

                default:
                    return null;
            }
        }
Beispiel #8
0
        internal static TimeSpan Subtract(CalendarDate later, TimePoint earlier)
        {
            if (earlier is CalendarDate)
            {
                return later - (CalendarDate)earlier;
            }

            if (earlier is OrdinalDate)
            {
                return later - ((OrdinalDate)earlier).ToCalendarDate();
            }

            if (earlier is WeekDate)
            {
                return later - ((WeekDate)earlier).ToCalendarDate();
            }

            throw new InvalidOperationException($"A {earlier.GetType()} cannot be subtracted from a {later.GetType()}");
        }
Beispiel #9
0
 public static TimeSpan Subtract(CalendarDateTime later, CalendarDate earlier)
 {
     return TimeSpan.FromDays(
                  (later.Year - earlier.Year) * 365
                + (later.Year - 1) / 4 - (earlier.Year - 1) / 4
                - (later.Year - 1) / 100 + (earlier.Year - 1) / 100
                + (later.Year - 1) / 400 - (earlier.Year - 1) / 400
                + DaysToMonth(later.Year, later.Month)
                - DaysToMonth(earlier.Year, earlier.Month)
                + later.Day
                - earlier.Day)
          + TimeSpan.FromHours(later.Hour)
          + TimeSpan.FromMinutes(later.Minute)
          + TimeSpan.FromSeconds(later.Second);
 }
Beispiel #10
0
 public static TimeSpan Subtract(CalendarDate later, CalendarDate earlier)
 {
     return TimeSpan.FromDays(
                  (later.Year - earlier.Year) * 365
                + (later.Year - 1) / 4 - (earlier.Year - 1) / 4
                - (later.Year - 1) / 100 + (earlier.Year - 1) / 100
                + (later.Year - 1) / 400 - (earlier.Year - 1) / 400
                + DaysToMonth(later.Year, later.Month)
                - DaysToMonth(earlier.Year, earlier.Month)
                + later.Day
                - earlier.Day);
 }
Beispiel #11
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;
            }
        }
Beispiel #12
0
        public static CalendarDate Subtract(CalendarDate x, CalendarDateDuration y)
        {
            long century = x.Century;
            long year = x.Year - y.Years;
            double month = x.Month;
            double day = x.Day;
            CalendarDatePrecision precision = x.Precision;

            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;
                }
            }

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

            while (day < 1)
            {
                day += DaysInMonth(year, (int)month);
                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;
            }
        }