private static void NormaliseSubtractedMonth(Iso8601Date isoDate) { Date date = new Date(isoDate.year, isoDate.month); date.NormaliseSubtractedMonth(); isoDate.year = date.Year; isoDate.month = date.Month; }
private static void NormaliseMonth(Iso8601Date isoDate) { DesignByContract.Check.Require(!isoDate.MonthUnknown, "isoDate monthUnknown must be false."); Date date = new Date(isoDate.year, isoDate.month); date.NormaliseMonth(); isoDate.year = date.Year; isoDate.month = date.Month; }
internal Iso8601Date Subtract(Iso8601Duration duration) { DesignByContract.Check.Require(duration != null, "duration must not be null."); Iso8601Date newDate = new Iso8601Date(this.ToString()); Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration); if (normalisedDuration.Days > 0) { if (newDate.DayUnknown) { throw new NotSupportedException("Cannot subtract a duration with days when the datetime day unknow."); } newDate.day -= normalisedDuration.Days; NormaliseSubtractedDay(newDate); } if (normalisedDuration.Months > 0) { if (newDate.MonthUnknown) { throw new NotSupportedException("Cannot subtract a duration with months when the datetime month unknow."); } newDate.month -= normalisedDuration.Months; NormaliseSubtractedMonth(newDate); if (!newDate.DayUnknown && (newDate.day > System.DateTime.DaysInMonth(newDate.year, newDate.month))) { newDate.day = System.DateTime.DaysInMonth(newDate.year, newDate.month); } } if (normalisedDuration.Years > 0) { if (normalisedDuration.Years > newDate.year) { throw new ApplicationException("duration.Years must not greater than the dateTime.year"); } newDate.year -= normalisedDuration.Years; } return(newDate); }
internal Iso8601Date Add(Iso8601Duration duration) { DesignByContract.Check.Require(duration != null, "duration must not be null."); Iso8601Date newDate = new Iso8601Date(this.ToString()); Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration); if (normalisedDuration.Months > 0) { if (newDate.MonthUnknown) { throw new NotSupportedException("Cannot add a duration with months when the datetime month unknow."); } newDate.month += normalisedDuration.Months; NormaliseMonth(newDate); if (normalisedDuration.Days <= 0) { if (!newDate.DayUnknown && newDate.day > System.DateTime.DaysInMonth(newDate.year, newDate.month)) { newDate.day = System.DateTime.DaysInMonth(newDate.year, newDate.month); } } } if (normalisedDuration.Years > 0) { newDate.year += normalisedDuration.Years; } if (normalisedDuration.Days > 0) { if (newDate.DayUnknown) { throw new NotSupportedException("Cannot add a duration with days when the datetime day unknow."); } newDate.day += normalisedDuration.Days; NormaliseDay(newDate); } return(newDate); }
private static void NormaliseSubtractedDay(Iso8601Date isoDate) { DesignByContract.Check.Require(!isoDate.MonthUnknown, "isoDate monthUnknown must be false."); Date date = new Date(isoDate.year, isoDate.month, isoDate.day); date.NormaliseSubtractedDay(); isoDate.year = date.Year; isoDate.month = date.Month; isoDate.day = date.Day; }
internal Iso8601Date Subtract(Iso8601Duration duration) { DesignByContract.Check.Require(duration != null, "duration must not be null."); Iso8601Date newDate = new Iso8601Date(this.ToString()); Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration); if (normalisedDuration.Days > 0) { if (newDate.DayUnknown) throw new NotSupportedException("Cannot subtract a duration with days when the datetime day unknow."); newDate.day -= normalisedDuration.Days; NormaliseSubtractedDay(newDate); } if (normalisedDuration.Months > 0) { if (newDate.MonthUnknown) throw new NotSupportedException("Cannot subtract a duration with months when the datetime month unknow."); newDate.month -= normalisedDuration.Months; NormaliseSubtractedMonth(newDate); if (!newDate.DayUnknown &&(newDate.day>System.DateTime.DaysInMonth(newDate.year, newDate.month))) { newDate.day = System.DateTime.DaysInMonth(newDate.year, newDate.month); } } if (normalisedDuration.Years > 0) { if (normalisedDuration.Years > newDate.year) throw new ApplicationException("duration.Years must not greater than the dateTime.year"); newDate.year -= normalisedDuration.Years; } return newDate; }
internal Iso8601Date Add(Iso8601Duration duration) { DesignByContract.Check.Require(duration != null, "duration must not be null."); Iso8601Date newDate = new Iso8601Date(this.ToString()); Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration); if (normalisedDuration.Months > 0) { if (newDate.MonthUnknown) throw new NotSupportedException("Cannot add a duration with months when the datetime month unknow."); newDate.month += normalisedDuration.Months; NormaliseMonth(newDate); if (normalisedDuration.Days <= 0) { if (!newDate.DayUnknown && newDate.day > System.DateTime.DaysInMonth(newDate.year, newDate.month)) newDate.day = System.DateTime.DaysInMonth(newDate.year, newDate.month); } } if (normalisedDuration.Years > 0) newDate.year += normalisedDuration.Years; if (normalisedDuration.Days > 0) { if (newDate.DayUnknown) throw new NotSupportedException("Cannot add a duration with days when the datetime day unknow."); newDate.day += normalisedDuration.Days; NormaliseDay(newDate); } return newDate; }
private bool IsMatchPattern(Iso8601Date isoDate) { DesignByContract.Check.Require(isoDate != null, string.Format(CommonStrings.XMustNotBeNull, "isoDate")); DesignByContract.Check.Require(this.Pattern != null, string.Format(CommonStrings.XMustNotBeNull, "Pattern")); if (!CDateTime.IsMatchValidityKind(this.MonthValidity, isoDate.MonthUnknown)) return false; if (!CDateTime.IsMatchValidityKind(this.DayValidity, isoDate.DayUnknown)) return false; return true; }
internal override string ValidValue(object aValue) { DesignByContract.Check.Require(aValue != null, string.Format(CommonStrings.XMustNotBeNull, "aValue")); if (!Iso8601Date.ValidIso8601Date(aValue.ToString())) return string.Format(AmValidationStrings.InvalidIsoDateX, aValue); Iso8601Date isoDate = new Iso8601Date(aValue.ToString()); if (isoDate == null) throw new ApplicationException(string.Format(CommonStrings.XIsNull, "isoDate")); if (this.Pattern != null) { if (!IsMatchPattern(isoDate)) { return string.Format(AmValidationStrings.DateXDoesNotMatchPatternY, aValue, Pattern); } } if (this.Range != null) { if (!this.Range.Has(isoDate)) { return string.Format(AmValidationStrings.DateXOutOfRange, aValue); } } return string.Empty; }
/// <summary> /// Compare the current date instance with the obj. Returns 0 if they are equal, /// -1 if the current instance less than obj, 1 if the current instance greater than obj. /// </summary> /// <param name="obj"></param> /// <returns></returns> public int CompareTo(object obj) { // precondition is that the current instance must be the same type as obj Check.Require(this.GetType() == obj.GetType(), "The current instance (" + this.GetType().ToString() + ") must be the same type as the obj(" + obj.GetType().ToString() + ")"); Iso8601Date objDate = obj as Iso8601Date; // compare yyyy if (this.year < objDate.year) { return(-1); } else if (this.year > objDate.year) { return(1); } // if yyyy is the same, compare month if (this.MonthUnknown && objDate.MonthUnknown) { return(0); } else if ((!this.MonthUnknown && objDate.MonthUnknown) || (this.MonthUnknown && !objDate.MonthUnknown)) { throw new FormatException ("An unknown month cannot be compared with a valid month value."); } // if the two dates have month value else { if (this.month > objDate.month) { return(1); } else if (this.month < objDate.month) { return(-1); } else // if the month is the same, compare day { if (this.DayUnknown && objDate.DayUnknown) { return(0); } else if ((!this.DayUnknown && objDate.DayUnknown) || (this.DayUnknown && !objDate.DayUnknown)) { throw new FormatException ("An unknown day cannot be compared with a valid day value."); } else { if (this.day > objDate.day) { return(1); } else if (this.day < objDate.day) { return(-1); } else { return(0); } } } } }