Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public DvDate(int year, int month, int day)
        {
            Check.Require(year >= 0, "Year value must not be less than zero.");
            this.isoDate = new Iso8601Date(year, month, day);

            CheckInvariants();
        }
Ejemplo n.º 3
0
        public override DvDuration Diff(DvTemporal <DvDate> b)
        {
            DesignByContract.Check.Require(b is DvDate, "Expected a DvDate instance in Diff function.");

            DvDate      bObj        = b as DvDate;
            Iso8601Date bObjIsoDate = bObj.isoDate;

            if (bObj == this)
            {
                return(new DvDuration("PT0S"));
            }

            double fractionalSecondsDiff = 0;
            int    secondsDiff           = 0;
            int    hoursDiff             = 0;
            int    minutesDiff           = 0;
            int    daysDiff   = 0;
            int    weeksDiff  = 0;
            int    monthsDiff = 0;
            int    yearsDiff  = 0;

            Iso8601Date leftOperand  = new Iso8601Date(this.Value);
            Iso8601Date rightOperand = new Iso8601Date(bObj.Value);

            if (leftOperand < rightOperand)
            {
                leftOperand  = new Iso8601Date(bObj.Value);;
                rightOperand = new Iso8601Date(this.Value);
            }

            if (!leftOperand.DayUnknown && !rightOperand.DayUnknown)
            {
                daysDiff = leftOperand.Day - rightOperand.Day;
            }

            int daysInMonth = 0;

            if (!leftOperand.MonthUnknown && !rightOperand.MonthUnknown)
            {
                monthsDiff = leftOperand.Month - rightOperand.Month;
                if (leftOperand.Month > 1)
                {
                    daysInMonth = System.DateTime.DaysInMonth(leftOperand.Year, leftOperand.Month - 1);
                }
                else
                {
                    daysInMonth = System.DateTime.DaysInMonth(leftOperand.Year - 1, leftOperand.Month - 1 + Iso8601DateTime.monthsInYear);
                }
            }
            yearsDiff = leftOperand.Year - rightOperand.Year;

            Iso8601Duration diff = Date.NormaliseDuration(yearsDiff, monthsDiff, weeksDiff, daysDiff, hoursDiff, minutesDiff, secondsDiff, fractionalSecondsDiff, daysInMonth);

            return(new DvDuration(diff.ToString()));
        }
Ejemplo n.º 4
0
        public override DvAbsoluteQuantity <DvDate, DvDuration> Add(DvAmount <DvDuration> b)
        {
            DesignByContract.Check.Require(b is DvDuration, "b object must be a DvDuration instance");

            DvDuration duration = b as DvDuration;

            Iso8601Duration isoDuration = new Iso8601Duration(duration.Value);
            Iso8601Date     newIsoDate  = this.isoDate.Add(isoDuration);

            return(new DvDate(newIsoDate.ToString()));
        }
Ejemplo n.º 5
0
        public DvDate(string dateString, DvDuration accuracy, string magnitudeStatus, CodePhrase normalStatus,
                      DvInterval <DvDate> normalRange, ReferenceRange <DvDate>[] otherReferenceRanges)
            : this()
        {
            Check.Require(Iso8601Date.ValidIso8601Date(dateString), "Date string(" + dateString + ") must be a valid ISO 8601 date.");

            this.isoDate = new Iso8601Date(dateString);

            base.SetBaseData(accuracy, magnitudeStatus, normalStatus, normalRange, otherReferenceRanges);

            CheckInvariants();
        }
Ejemplo n.º 6
0
        protected override void ReadXmlBase(System.Xml.XmlReader reader)
        {
            base.ReadXmlBase(reader);

            // Get value

            Check.Assert(reader.LocalName == "value", "reader.LocalName must be 'value'");
            string value = reader.ReadElementString("value", RmXmlSerializer.OpenEhrNamespace);

            this.isoDate = new Iso8601Date(value);

            reader.MoveToContent();
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        private static DvDate GetValidDvDate(double totoalDates, int year, int month, int date)
        {
            DesignByContract.Check.Require(date > 0, "date must >0");

            int resultDate  = date;
            int resultMonth = month;
            int resultYear  = year;

            while (!Iso8601Date.ValidDay(resultYear, resultMonth, resultDate))
            {
                int daysInMonth = System.DateTime.DaysInMonth(resultYear, resultMonth);
                if (resultDate > daysInMonth)
                {
                    resultMonth++;
                    if (resultMonth > 12)
                    {
                        resultMonth = 1;
                        resultYear++;
                    }
                }

                double remain = (totoalDates - (resultYear * TimeDefinitions.nominalDaysInYear)) - (resultMonth * TimeDefinitions.nominalDaysInMonth);

                resultDate = (int)(Math.Truncate(remain));
                if (resultDate < 0)
                {
                    throw new ApplicationException("resultDate must not be <=0");
                }

                if (remain - resultDate > 0)
                {
                    resultDate = resultDate + 1;
                }
            }

            return(new DvDate(resultYear, resultMonth, resultDate));
        }
Ejemplo n.º 9
0
 public DvDate(System.DateTime date)
 {
     this.isoDate = new Iso8601Date(date);
     CheckInvariants();
 }