コード例 #1
0
        public Date AddMonths(int months)
        {
            if (months < -120000 || months > 120000)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(nameof(months));
            }

            GetDatePart(out int y, out int m, out int d);
            int i = m - 1 + months;

            if (i >= 0)
            {
                m = i % 12 + 1;
                y = y + i / 12;
            }
            else
            {
                m = 12 + (i + 1) % 12;
                y = y + (i - 11) / 12;
            }
            if (y < 1 || y > 9999)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(nameof(months));
            }
            int days = DaysInMonth(y, m);

            if (d > days)
            {
                d = days;
            }

            return(new Date((ulong)(DateToTicks(y, m, d) + InternalTicks % TicksPerDay) | InternalKind));
        }
コード例 #2
0
 public Date AddYears(int value)
 {
     if (value < -10000 || value > 10000)
     {
         // DateTimeOffset.AddYears(int years) is implemented on top of DateTime.AddYears(int value). Use the more appropriate
         // parameter name out of the two for the exception.
         ExceptionThrower.ThrowArgumentOutOfRangeException("years");
     }
     return(AddMonths(value * 12));
 }
コード例 #3
0
        internal Date(long ticks, DateTimeKind kind, bool isAmbiguousDst)
        {
            if (ticks < MinTicks || ticks > MaxTicks)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(nameof(ticks));
            }

            Debug.Assert(kind == DateTimeKind.Local, "Internal Constructor is for local times only");
            _dateData = ((ulong)ticks | (isAmbiguousDst ? KindLocalAmbiguousDst : KindLocal));
        }
コード例 #4
0
        public Date AddTicks(long value)
        {
            long ticks = InternalTicks;

            if (value > MaxTicks - ticks || value < MinTicks - ticks)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(nameof(value));
            }

            return(new Date((ulong)(ticks + value) | InternalKind));
        }
コード例 #5
0
        private Date Add(double value, int scale)
        {
            double millis_double = value * (double)scale + (value >= 0 ? 0.5 : -0.5);

            if (millis_double <= (double)-MaxMillis || millis_double >= (double)MaxMillis)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(nameof(value));
            }

            return(AddTicks((long)millis_double * TicksPerMillisecond));
        }
コード例 #6
0
        public Date Subtract(TimeSpan value)
        {
            long ticks      = InternalTicks;
            long valueTicks = value.Ticks;

            if (ticks - MinTicks < valueTicks || ticks - MaxTicks > valueTicks)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(nameof(value));
            }

            return(new Date((ulong)(ticks - valueTicks) | InternalKind));
        }
コード例 #7
0
        public static int DaysInMonth(int year, int month)
        {
            if (month < 1 || month > 12)
            {
                ExceptionThrower.ThrowArgumentOutOfRangeException(nameof(month));
            }

            // IsLeapYear checks the year argument
            int[] days = IsLeapYear(year) ? s_daysToMonth366 : s_daysToMonth365;

            return(days[month] - days[month - 1]);
        }