/// <summary>
        /// Gets the nth instance of this recurring date.
        /// </summary>
        /// <param name="count">The nth instance to retrieve.</param>
        /// <returns>The simple date of the nth instance.</returns>
        public GedcomxDateSimple GetNth(Int32 count)
        {
            GedcomxDateDuration duration = GedcomxDateUtil.MultiplyDuration(range.Duration, count);

            return(GedcomxDateUtil.AddDuration(range.Start, duration));
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GedcomxDateRange"/> class.
        /// </summary>
        /// <param name="date">The formal date string that describes a GEDCOM X date range.</param>
        /// <exception cref="Gedcomx.Date.GedcomxDateException">
        /// Thrown if the formal date is null, empty, or does not meet the expected format.
        /// </exception>
        public GedcomxDateRange(String date)
        {
            if (date == null || date.Length < 1)
            {
                throw new GedcomxDateException("Invalid Range");
            }

            String range = date;

            // If range starts with A it is recurring
            if (date[0] == 'A')
            {
                approximate = true;
                range       = date.Substring(1);
            }

            // / is required
            if (!date.Contains("/"))
            {
                throw new GedcomxDateException("Invalid Range: / is required");
            }

            /*
             * range -> parts
             * / -> []
             * +1000/ -> ["+1000"]
             * /+1000 -> ["","+1000"]
             * +1000/+2000 -> ["+1000","+2000"]
             */
            String[] parts = range.Split('/');

            if (parts.Length < 1 || parts.Length > 2)
            {
                throw new GedcomxDateException("Invalid Range: One or two parts are required");
            }

            if (!parts[0].Equals(""))
            {
                try
                {
                    start = new GedcomxDateSimple(parts[0]);
                }
                catch (GedcomxDateException e)
                {
                    throw new GedcomxDateException(e.Message + " in Range Start Date");
                }
            }

            if (parts.Length == 2)
            {
                if (parts[1][0] == 'P')
                {
                    if (start == null)
                    {
                        throw new GedcomxDateException("Invalid Range: A range may not end with a duration if missing a start date");
                    }
                    try
                    {
                        duration = new GedcomxDateDuration(parts[1]);
                    }
                    catch (GedcomxDateException e)
                    {
                        throw new GedcomxDateException(e.Message + " in Range End Duration");
                    }
                    // Use the duration to calculate the end date
                    end = GedcomxDateUtil.AddDuration(start, duration);
                }
                else
                {
                    try
                    {
                        end = new GedcomxDateSimple(parts[1]);
                    }
                    catch (GedcomxDateException e)
                    {
                        throw new GedcomxDateException(e.Message + " in Range End Date");
                    }
                    if (start != null)
                    {
                        duration = GedcomxDateUtil.GetDuration(start, end);
                    }
                }
            }
        }