Пример #1
0
        private bool IsMatchPattern(Iso8601DateTime aValue)
        {
            DesignByContract.Check.Require(aValue != null, string.Format(CommonStrings.XMustNotBeNull, "aValue"));
            DesignByContract.Check.Require(this.Pattern != null, string.Format(CommonStrings.XMustNotBeNull, "Pattern"));

            if (!CDateTime.IsMatchValidityKind(this.MonthValidity, aValue.MonthUnknown))
                return false;
            if (!CDateTime.IsMatchValidityKind(this.DayValidity, aValue.DayUnknown))
                return false;
            if(!CDateTime.IsMatchValidityKind(this.HourValidity, aValue.HourUnknown))
                return false;
            if(!CDateTime.IsMatchValidityKind(this.MinuteValidity, aValue.MinuteUnknown))
                return false;
            if(!CDateTime.IsMatchValidityKind(this.SecondValidity, aValue.SecondUnknown))
                return false;
            if(!CDateTime.IsMatchValidityKind(this.MillisecondValidity, !aValue.HasFractionalSecond))
                return false;

            return true;
        }
Пример #2
0
 private static void NormaliseHour(Iso8601DateTime isoDateTime)
 {
     if (isoDateTime.hour >= 24)
     {
         isoDateTime.day += isoDateTime.hour / 24;
         isoDateTime.hour = isoDateTime.hour % 24;
         NormaliseDay(isoDateTime);
     }
 }
Пример #3
0
        internal override string ValidValue(object aValue)
        {
            DesignByContract.Check.Require(aValue != null, string.Format(CommonStrings.XMustNotBeNull, "aValue"));

            string dateTimeString = aValue.ToString();
            if (!Iso8601DateTime.ValidIso8601DateTime(dateTimeString))
            {
                return string.Format(AmValidationStrings.InvalidIsoDateTimeX, dateTimeString);
            }

            Iso8601DateTime isoDateTime = new Iso8601DateTime(dateTimeString);
            if (isoDateTime == null)
                throw new ApplicationException(string.Format(CommonStrings.XIsNull, "isoDateTime"));

            if (this.Pattern != null)
            {
                if (!IsMatchPattern(isoDateTime))
                {
                    return string.Format(AmValidationStrings.DateTimeXDoesNotMatchPatternY, dateTimeString, Pattern);
                }
            }

            if (this.Range != null)
            {
                if (!this.Range.Has(isoDateTime))
                {
                    return string.Format(AmValidationStrings.DateTimeXOutOfRange, dateTimeString);
                }
            }

            return string.Empty;
        }
Пример #4
0
        private static void NormaliseDay(Iso8601DateTime isoDateTime)
        {
            DesignByContract.Check.Require(!isoDateTime.MonthUnknown, "isoDateTime monthUnknown must be false.");
            DesignByContract.Check.Require(!isoDateTime.DayUnknown, "isoDateTime dayUnknown must be false.");

            Date date = new Date(isoDateTime.year, isoDateTime.month, isoDateTime.day);
            date.NormaliseDay();
            isoDateTime.year = date.Year;
            isoDateTime.month = date.Month;
            isoDateTime.day = date.Day;
        }
Пример #5
0
        private static void NormaliseFractionalSecond(Iso8601DateTime isoDateTime)
        {
            DesignByContract.Check.Require(!isoDateTime.MinuteUnknown, "isoDateTime.MinuteUnknow must be false.");
            DesignByContract.Check.Require(!isoDateTime.SecondUnknown, "isoDateTime.SecondUnknown must be false.");
            DesignByContract.Check.Require(isoDateTime.HasFractionalSecond, "isoDateTime.HasFractionalSecond must be true.");

            Time time = new Time(isoDateTime.hour, isoDateTime.minute, isoDateTime.second, isoDateTime.fractionalSecond);
            time.NormaliseFractionalSecond();

            isoDateTime.hour = time.Hour;
            isoDateTime.minute = time.Minute;
            isoDateTime.second = time.Second;
            isoDateTime.fractionalSecond = time.FractionalSeconds;

            NormaliseHour(isoDateTime);
        }
Пример #6
0
        internal Iso8601DateTime Add(Iso8601Duration duration)
        {
            DesignByContract.Check.Require(duration != null, "duration must not be null.");

            Iso8601DateTime newDateTime = new Iso8601DateTime(this.ToString());

            Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration);

            if (normalisedDuration.FractionalSecond > 0)
            {
                if (newDateTime.SecondUnknown)
                    throw new NotSupportedException("Cannot add a duration with fractionalSeconds when the datetime seconds unknow.");

                if (newDateTime.HasFractionalSecond)
                {
                    newDateTime.fractionalSecond += normalisedDuration.FractionalSecond;
                    NormaliseFractionalSecond(newDateTime);
                }
                else
                    newDateTime.fractionalSecond = normalisedDuration.FractionalSecond;
            }

            if (normalisedDuration.Seconds > 0)
            {
                if (newDateTime.SecondUnknown)
                    throw new NotSupportedException("Cannot add a duration with seconds when the datetime seconds unknow.");
                newDateTime.second += normalisedDuration.Seconds;
                NormaliseSecond(newDateTime);
            }

            if (normalisedDuration.Minutes > 0)
            {
                if (newDateTime.MinuteUnknown)
                    throw new NotSupportedException("Cannot add a duration with minutes when the datetime minutes unknow.");
                newDateTime.minute += normalisedDuration.Minutes;
                NormaliseMinute(newDateTime);
            }

            if (normalisedDuration.Hours > 0)
            {
                if (newDateTime.HourUnknown)
                    throw new NotSupportedException("Cannot add a duration with hours when the datetime hour unknow.");
                newDateTime.hour += normalisedDuration.Hours;
                NormaliseHour(newDateTime);
                NormaliseDay(newDateTime);
            }

            if (normalisedDuration.Months > 0)
            {
                if (newDateTime.MonthUnknown)
                    throw new NotSupportedException("Cannot add a duration with months when the datetime month unknow.");
                newDateTime.month += normalisedDuration.Months;

                NormaliseMonth(newDateTime);

                if (normalisedDuration.Days <= 0)
                {
                    if (!newDateTime.DayUnknown && (newDateTime.day > System.DateTime.DaysInMonth(newDateTime.year, newDateTime.month)))
                    {
                        newDateTime.day = System.DateTime.DaysInMonth(newDateTime.year, newDateTime.month);
                    }
                }
            }

            if (normalisedDuration.Years > 0)
                newDateTime.year += normalisedDuration.Years;

            if (normalisedDuration.Days > 0)
            {
                if (newDateTime.DayUnknown)
                    throw new NotSupportedException("Cannot add a duration with days when the datetime day unknow.");

                newDateTime.day += normalisedDuration.Days;
                NormaliseDay(newDateTime);
            }

            return newDateTime;
        }
Пример #7
0
        internal Iso8601DateTime Subtract(Iso8601Duration duration)
        {
            DesignByContract.Check.Require(duration != null, "duration must not be null.");

            Iso8601DateTime newDateTime = new Iso8601DateTime(this.ToString());

            Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration);

            if (normalisedDuration.FractionalSecond > 0)
            {
                if (newDateTime.SecondUnknown)
                    throw new NotSupportedException("Cannot subtract a duration with fractionalSeconds when the datetime seconds unknow.");

                if (newDateTime.HasFractionalSecond)
                    newDateTime.fractionalSecond -= normalisedDuration.FractionalSecond;
                else
                    newDateTime.fractionalSecond = (normalisedDuration.FractionalSecond) * -1;

                NormaliseSubtractedFractionalSecond(newDateTime);
            }

            if (normalisedDuration.Seconds > 0)
            {
                if (newDateTime.SecondUnknown)
                    throw new NotSupportedException("Cannot subtract a duration with seconds when the datetime seconds unknow.");
                newDateTime.second -= normalisedDuration.Seconds;
                NormaliseSubtractedSecond(newDateTime);
            }

            if (normalisedDuration.Minutes > 0)
            {
                if (newDateTime.MinuteUnknown)
                    throw new NotSupportedException("Cannot subtract a duration with minutes when the datetime minutes unknow.");
                newDateTime.minute -= normalisedDuration.Minutes;
                NormaliseSubtractedMinute(newDateTime);
            }

            if (normalisedDuration.Hours > 0)
            {
                if (newDateTime.HourUnknown)
                    throw new NotSupportedException("Cannot subtract a duration with hours when the datetime hour unknow.");
                newDateTime.hour -= normalisedDuration.Hours;
                NormaliseSubtractedHour(newDateTime);
            }

            if (normalisedDuration.Days > 0)
            {
                if (newDateTime.DayUnknown)
                    throw new NotSupportedException("Cannot subtract a duration with days when the datetime day unknow.");

                newDateTime.day -= normalisedDuration.Days;
                NormaliseSubtractedDay(newDateTime);
            }

            if (normalisedDuration.Months > 0)
            {
                if (newDateTime.MonthUnknown)
                    throw new NotSupportedException("Cannot subtract a duration with months when the datetime month unknow.");

                newDateTime.month -= normalisedDuration.Months;

                NormaliseSubtractedMonth(newDateTime);

                if (!newDateTime.DayUnknown &&(newDateTime.day>System.DateTime.DaysInMonth(newDateTime.year, newDateTime.month)))
                {
                    newDateTime.day = System.DateTime.DaysInMonth(newDateTime.year, newDateTime.month);
                }
            }

            if (normalisedDuration.Years > 0)
            {
                if (normalisedDuration.Years > newDateTime.year)
                    throw new ApplicationException("duration.Years must not greater than the dateTime.year");

                newDateTime.year -= normalisedDuration.Years;
            }

            return newDateTime;
        }
Пример #8
0
        /// <summary>
        /// Take a DvDateTime and convert it to a System.DateTime
        /// </summary>
        /// <param name="dvDateTime"></param>
        /// <returns></returns>
        public static System.DateTime ToDateTime(Iso8601DateTime isoDateTime)
        {
            if (isoDateTime == null)
                return System.DateTime.MinValue; // All we can do, really!

            string timeString = isoDateTime.ToString(true, false);

            // pad out years < 1000
            int i = timeString.IndexOf('-');
            if (i < 0)
                i = timeString.Length;
            if (i < 4)
            {
                timeString = "000".Substring(0, 4 - i) + timeString;
            }

            // pad out partial datetime
            int timeDelimiter = timeString.IndexOf('T');

            if (timeDelimiter < 0)
            {
                if (timeString.Length < 5)
                    timeString += "-01";
                if (timeString.Length < 8)
                    timeString += "-01";
                timeString += "T00:00:00";
            }
            else
            {
                int timezone = timeString.IndexOfAny(new char[] { 'Z', 'z', '+', '-' }, timeDelimiter);
                int minuteDelimiter = timeString.IndexOf(':');
                if (minuteDelimiter < 0)
                {
                    if (timezone < 0)
                        timeString += ":00:00";
                    else
                        timeString = timeString.Substring(0, timeDelimiter + 3) + ":00:00" + timeString.Substring(timezone);
                }
                else
                {
                    if (timezone < 0)
                        i = timeString.IndexOf(':', minuteDelimiter + 1);
                    else
                        i = timeString.IndexOf(':', minuteDelimiter + 1, timezone - minuteDelimiter);

                    if (i < 0)
                    {
                        if (timezone < 0)
                            timeString += ":00";
                        else
                            timeString = timeString.Substring(0, minuteDelimiter + 3) + ":00" + timeString.Substring(timezone);
                    }
                }
            }
            System.DateTime dateTime = System.Xml.XmlConvert.ToDateTime(timeString,
                System.Xml.XmlDateTimeSerializationMode.RoundtripKind);

            // NOTE: timezone is only preserved when in local timezone or UTC.
            // Unspecified timezone is assumed to be local
            // A date/time with non-local and non-UTC timezone will be converted into local time
            // i.e the datetime value will be different and in the local time zone unless in
            // local or UTC timezone (or unspecified)

            return dateTime;
        }
Пример #9
0
        private static void NormaliseSubtractedSecond(Iso8601DateTime isoDateTime)
        {
            DesignByContract.Check.Require(!isoDateTime.HourUnknown, "isoDateTime.HourUnknown must be false.");

            Time time = new Time(isoDateTime.hour, isoDateTime.minute, isoDateTime.second);
            time.NormaliseSubtractedSecond();

            isoDateTime.hour = time.Hour;
            isoDateTime.minute = time.Minute;
            isoDateTime.second = time.Second;

            NormaliseSubtractedHour(isoDateTime);
        }
Пример #10
0
 private static void NormaliseSubtractedMonth(Iso8601DateTime isoDateTime)
 {
     Date date = new Date(isoDateTime.year, isoDateTime.month);
     date.NormaliseSubtractedMonth();
     isoDateTime.year = date.Year;
     isoDateTime.month = date.Month;
 }
Пример #11
0
 private static void NormaliseSubtractedHour(Iso8601DateTime isoDateTime)
 {
     if (isoDateTime.hour < 0)
     {
         isoDateTime.hour += 24;
         isoDateTime.day--;
         NormaliseSubtractedDay(isoDateTime);
     }
 }
Пример #12
0
        private static void NormaliseMinute(Iso8601DateTime isoDateTime)
        {
            DesignByContract.Check.Require(!isoDateTime.HourUnknown, "isoDateTime.HourUnknown must be false.");
            DesignByContract.Check.Require(!isoDateTime.MinuteUnknown, "isoDateTime.MinuteUnknow must be false.");

            Time time = new Time(isoDateTime.hour, isoDateTime.minute);
            time.NormaliseMinute();

            isoDateTime.hour = time.Hour;
            isoDateTime.minute = time.Minute;

            NormaliseHour(isoDateTime);
        }