Esempio n. 1
0
        // Converts an OLE Date to a tick count.
        // This function is duplicated in COMDateTime.cpp
        internal static long DoubleDateToTicks(double value)
        {
            // The check done this way will take care of NaN
            if (!(value < OADateMaxAsDouble) || !(value > OADateMinAsDouble))
            {
                ExceptionThrower.ThrowArgumentException("OLE Date is invalid.", nameof(value));
            }

            // Conversion to long will not cause an overflow here, as at this point the "value" is in between OADateMinAsDouble and OADateMaxAsDouble
            long millis = (long)(value * MillisPerDay + (value >= 0 ? 0.5 : -0.5));

            // The interesting thing here is when you have a value like 12.5 it all positive 12 days and 12 hours from 01/01/1899
            // However if you a value of -12.25 it is minus 12 days but still positive 6 hours, almost as though you meant -11.75 all negative
            // This line below fixes up the millis in the negative case
            if (millis < 0)
            {
                millis -= (millis % MillisPerDay) * 2;
            }

            millis += DoubleDateOffset / TicksPerMillisecond;

            if (millis < 0 || millis >= MaxMillis)
            {
                ExceptionThrower.ThrowArgumentException("OLE Date scale is invalid.", nameof(value));
            }

            return(millis * TicksPerMillisecond);
        }
Esempio n. 2
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));
        }
Esempio n. 3
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));
        }