Пример #1
0
        public OrdinalDateTimeDuration(int years, int days, int hours, double minutes)
        {
            if (years < 0 || years >= 10000)
            {
                throw new ArgumentOutOfRangeException(nameof(years), "Years must be a number between 0 and 9999.");
            }

            if (days < 0 || days > 999)
            {
                throw new ArgumentOutOfRangeException(nameof(days), "Days must be a number between 0 and 999.");
            }

            if (hours < 0 || hours > 24)
            {
                throw new ArgumentOutOfRangeException(nameof(hours), "Hours must be a number between 0 and 24.");
            }

            if (minutes < 0 || minutes > 60)
            {
                throw new ArgumentOutOfRangeException(nameof(minutes), "Minutes must be a number between 0 and 60.");
            }

            _dateDuration = new OrdinalDateDuration(years, days);
            _timeDuration = new TimeDuration(hours, minutes);
        }
Пример #2
0
        public CalendarDateTimeDuration(CalendarDateDuration dateDuration, TimeDuration timeDuration)
        {
            if (dateDuration == null)
            {
                throw new ArgumentNullException(nameof(dateDuration));
            }

            if (timeDuration == null)
            {
                throw new ArgumentNullException(nameof(timeDuration));
            }

            _dateDuration = dateDuration;
            _timeDuration = timeDuration;
        }
Пример #3
0
 internal OrdinalDateTimeDuration(OrdinalDateDuration dateDuration, TimeDuration timeDuration)
 {
     _dateDuration = dateDuration;
     _timeDuration = timeDuration;
 }
Пример #4
0
        public static Time Add(Time x, TimeDuration y)
        {
            var second = x.Second;
            var minute = x.Minute;
            var hour = x.Hour;
            var precision = x.Precision;

            hour += y.Hours;

            if (y.Minutes != null)
            {
                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.Seconds != null)
            {
                second += y.Seconds.Value;

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

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

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

            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;
            }
        }
Пример #5
0
        public static CalendarDateTime Add(CalendarDateTime x, TimeDuration y)
        {
            double day = x.Day;
            double month = x.Month;
            long year = x.Year;
            double second = x.Second;
            double minute = x.Minute;
            double hour = x.Hour + y.Hours;
            TimePrecision precision = x.Precision;

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

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

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

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

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

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

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

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

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

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

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

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

                daysInMonth = DaysInMonth(year, (int)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;
            }
        }
Пример #6
0
        public static Time Subtract(Time x, TimeDuration y)
        {
            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) * SecondsPerMinute;
                    minute -= (int)y.Minutes.Value;
                }
                else
                {
                    minute -= y.Minutes.Value;
                }

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

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

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

            while (minute < -59d)
            {
                minute += MinutesPerHour;
                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;
            }
        }
Пример #7
0
        public static CalendarDateTime Subtract(CalendarDateTime x, TimeDuration y)
        {
            long year = x.Year;
            double month = x.Month;
            double day = x.Day;
            double hour = x.Hour - y.Hours;
            double minute = x.Minute;
            double second = x.Second;
            TimePrecision precision = x.Precision;

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