internal Iso8601Time Add(Iso8601Duration duration) { DesignByContract.Check.Require(duration != null, "duration must not be null."); Iso8601Time newTime = new Iso8601Time(this.ToString()); Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration); if (normalisedDuration.FractionalSecond > 0) { if (newTime.SecondUnknown) { throw new NotSupportedException("Cannot add a duration with fractionalSeconds when the datetime seconds unknow."); } if (newTime.HasFractionalSecond) { newTime.fractionalSecond += normalisedDuration.FractionalSecond; NormaliseFractionalSecond(newTime); } else { newTime.fractionalSecond = normalisedDuration.FractionalSecond; } } if (normalisedDuration.Seconds > 0) { if (newTime.SecondUnknown) { throw new NotSupportedException("Cannot add a duration with seconds when the time seconds unknow."); } newTime.second += normalisedDuration.Seconds; NormaliseSecond(newTime); } if (normalisedDuration.Minutes > 0) { if (newTime.MinuteUnknown) { throw new NotSupportedException("Cannot add a duration with minutes when the time minutes unknow."); } newTime.minute += normalisedDuration.Minutes; NormaliseMinute(newTime); } if (normalisedDuration.Hours > 0) { newTime.hour += normalisedDuration.Hours; if (newTime.hour >= 24) { throw new ApplicationException("Invalid durtion results in the hours value greater or equal to 24."); } } return(newTime); }
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); }
internal Iso8601Time Subtract(Iso8601Duration duration) { DesignByContract.Check.Require(duration != null, "duration must not be null."); Iso8601Time newTime = new Iso8601Time(this.ToString()); Iso8601Duration normalisedDuration = Iso8601Duration.Normalise(duration); if (normalisedDuration.FractionalSecond > 0) { if (newTime.SecondUnknown) { throw new NotSupportedException("Cannot subtract a duration with fractionalSeconds when the time seconds unknow."); } if (newTime.HasFractionalSecond) { newTime.fractionalSecond -= normalisedDuration.FractionalSecond; } else { newTime.fractionalSecond = (normalisedDuration.FractionalSecond) * -1; } NormaliseSubtractedFractionalSecond(newTime); } if (normalisedDuration.Seconds > 0) { if (newTime.SecondUnknown) { throw new NotSupportedException("Cannot subtract a duration with seconds when the time seconds unknow."); } newTime.second -= normalisedDuration.Seconds; NormaliseSubtractedSecond(newTime); } if (normalisedDuration.Minutes > 0) { if (newTime.MinuteUnknown) { throw new NotSupportedException("Cannot subtract a duration with minutes when the time minutes unknow."); } newTime.minute -= normalisedDuration.Minutes; NormaliseSubtractedMinute(newTime); } if (normalisedDuration.Hours > 0) { newTime.hour -= normalisedDuration.Hours; if (newTime.hour < 0) { throw new ApplicationException("Invalid duration results the Iso8601Time hour less than 0 after subtraction."); } } return(newTime); }