Beispiel #1
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));
 }
Beispiel #2
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));
        }
Beispiel #3
0
        // A version of ToBinary that uses the real representation and does not adjust local times. This is needed for
        // scenarios where the serialized data must maintain compatibility
        internal static Date FromBinaryRaw(long dateData)
        {
            long ticks = dateData & (long)TicksMask;

            if (ticks < MinTicks || ticks > MaxTicks)
            {
                ExceptionThrower.ThrowArgumentException("Bad binary data.", nameof(dateData));
            }

            return(new Date((ulong)dateData));
        }
Beispiel #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));
        }
Beispiel #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));
        }
Beispiel #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));
        }
Beispiel #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]);
        }
Beispiel #8
0
        //TODO: CHANGE PROVIDER TO NULLABLE IN C#8
        public static Date Parse(string input, IFormatProvider provider, DateTimeStyles styles)
        {
            if (input == null)
            {
                ExceptionThrower.ThrowArgumentNullException(nameof(input));
            }
            else if (DateTime.TryParse(input, DateTimeFormatInfo.GetInstance(provider), styles, out var dateTime))
            {
                return(new Date(dateTime.Ticks));
            }

            throw new ArgumentOutOfRangeException(nameof(input));
        }
Beispiel #9
0
        public static Date Parse(string input)
        {
            if (input == null)
            {
                ExceptionThrower.ThrowArgumentNullException(nameof(input));
            }
            else if (DateTime.TryParse(input, out var dateTime))
            {
                return(new Date(dateTime.Ticks));
            }

            throw new ArgumentOutOfRangeException(nameof(input));
        }
Beispiel #10
0
        //TODO: CHANGE TO NULLABLE IN C#8
        public int CompareTo(object value)
        {
            if (value == null)
            {
                return(1);
            }

            if (!(value is Date))
            {
                ExceptionThrower.ThrowArgumentException("Must be a date.", nameof(value));
            }

            return(Compare(this, (Date)value));
        }