示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ReportingPeriod"/> class.
        /// </summary>
        /// <param name="start">The start of the reporting period.</param>
        /// <param name="end">The end of the reporting period.</param>
        /// <exception cref="ArgumentNullException"><paramref name="start"/> or <paramref name="end"/> are null.</exception>
        /// <exception cref="ArgumentException"><paramref name="start"/> and <paramref name="end"/> are bounded and are different concrete types of units-of-time.</exception>
        /// <exception cref="ArgumentException"><paramref name="start"/> and/or <paramref name="end"/> is unbounded and are different kinds of units-of-time.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="start"/> is greater than <paramref name="end"/>.</exception>
        public ReportingPeriod(
            UnitOfTime start,
            UnitOfTime end)
        {
            if (start == null)
            {
                throw new ArgumentNullException(nameof(start));
            }

            if (end == null)
            {
                throw new ArgumentNullException(nameof(end));
            }

            if ((start is IAmUnboundedTime) || (end is IAmUnboundedTime))
            {
                if (start.UnitOfTimeKind != end.UnitOfTimeKind)
                {
                    throw new ArgumentException(Invariant($"{nameof(start)} and/or {nameof(end)} is unbounded and are different kinds of units-of-time."));
                }
            }
            else
            {
                if (start.GetType() != end.GetType())
                {
                    throw new ArgumentException(Invariant($"{nameof(start)} and {nameof(end)} are bounded and are different concrete types of units-of-time."));
                }

                if (start > end)
                {
                    throw new ArgumentOutOfRangeException(nameof(start), Invariant($"{nameof(start)} is greater than {nameof(end)}."));
                }
            }

            this.Start = start;
            this.End   = end;
        }
示例#2
0
        /// <inheritdoc />
        public override RelativeSortOrder CompareToForRelativeSortOrder(UnitOfTime other)
        {
            if (ReferenceEquals(other, null))
            {
                return(RelativeSortOrder.ThisInstanceFollowsTheOtherInstance);
            }

            if (!(other is CalendarMonth otherAsCalendarMonth))
            {
                throw new ArgumentException(Invariant($"Attempting to compare objects of different types.  This object is of type 'CalendarMonth' whereas the other object is of type '{other.GetType().ToStringReadable()}'."));
            }

            var result = this.CompareToForRelativeSortOrder(otherAsCalendarMonth);

            return(result);
        }
示例#3
0
        /// <summary>
        /// Serializes a <see cref="UnitOfTime"/> to a sortable string.
        /// </summary>
        /// <param name="unitOfTime">The unit-of-time to serialize.</param>
        /// <returns>
        /// Gets a string representation of a unit-of-time that can be deserialized
        /// into the same unit-of-time and which sorts in the same way that the
        /// other unit-of-times (of the same type) would sort (earlier time first, later time last).
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="unitOfTime"/> is null.</exception>
        public static string SerializeToSortableString(
            this UnitOfTime unitOfTime)
        {
            if (unitOfTime == null)
            {
                throw new ArgumentNullException(nameof(unitOfTime));
            }

            var unitOfTimeType = unitOfTime.GetType();

            switch (unitOfTime)
            {
            case CalendarDay unitOfTimeAsCalendarDay:
            {
                var result = Invariant($"c-{unitOfTimeAsCalendarDay.Year:D4}-{(int)unitOfTimeAsCalendarDay.MonthNumber:D2}-{(int)unitOfTimeAsCalendarDay.DayOfMonth:D2}");

                return(result);
            }

            case CalendarMonth unitOfTimeAsCalendarMonth:
            {
                var result = Invariant($"c-{unitOfTimeAsCalendarMonth.Year:D4}-{(int)unitOfTimeAsCalendarMonth.MonthNumber:D2}");

                return(result);
            }

            case CalendarQuarter unitOfTimeAsCalendarQuarter:
            {
                var result = Invariant($"c-{unitOfTimeAsCalendarQuarter.Year:D4}-Q{(int)unitOfTimeAsCalendarQuarter.QuarterNumber}");

                return(result);
            }

            case CalendarYear unitOfTimeAsCalendarYear:
            {
                var result = Invariant($"c-{unitOfTimeAsCalendarYear.Year:D4}");

                return(result);
            }

            case CalendarUnbounded _:
            {
                var result = "c-unbounded";
                return(result);
            }

            case FiscalMonth unitOfTimeAsFiscalMonth:
            {
                var result = Invariant($"f-{unitOfTimeAsFiscalMonth.Year:D4}-{(int)unitOfTimeAsFiscalMonth.MonthNumber:D2}");

                return(result);
            }

            case FiscalQuarter unitOfTimeAsFiscalQuarter:
            {
                var result = Invariant($"f-{unitOfTimeAsFiscalQuarter.Year:D4}-Q{(int)unitOfTimeAsFiscalQuarter.QuarterNumber}");

                return(result);
            }

            case FiscalYear unitOfTimeAsFiscalYear:
            {
                var result = Invariant($"f-{unitOfTimeAsFiscalYear.Year:D4}");

                return(result);
            }

            case FiscalUnbounded _:
            {
                var result = "f-unbounded";
                return(result);
            }

            case GenericMonth unitOfTimeAsGenericMonth:
            {
                var result = Invariant($"g-{unitOfTimeAsGenericMonth.Year:D4}-{(int)unitOfTimeAsGenericMonth.MonthNumber:D2}");

                return(result);
            }

            case GenericQuarter unitOfTimeAsGenericQuarter:
            {
                var result = Invariant($"g-{unitOfTimeAsGenericQuarter.Year:D4}-Q{(int)unitOfTimeAsGenericQuarter.QuarterNumber}");

                return(result);
            }

            case GenericYear unitOfTimeAsGenericYear:
            {
                var result = Invariant($"g-{unitOfTimeAsGenericYear.Year:D4}");

                return(result);
            }

            case GenericUnbounded _:
            {
                var result = "g-unbounded";

                return(result);
            }

            default:
                throw new NotSupportedException("this type of unit-of-time is not supported: " + unitOfTimeType);
            }
        }
        /// <summary>
        /// Adds the specified number of units to a unit-of-time.
        /// </summary>
        /// <param name="unitOfTime">The unit-of-time to add to.</param>
        /// <param name="unitsToAdd">The number of units to add (use negative numbers to subtract units).</param>
        /// <returns>
        /// The result of adding the specified number of units to the specified units-of-time.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="unitOfTime"/> is null.</exception>
        /// <exception cref="InvalidOperationException"><paramref name="unitOfTime"/> is unbounded.</exception>
        public static UnitOfTime Plus(
            this UnitOfTime unitOfTime,
            int unitsToAdd)
        {
            switch (unitOfTime)
            {
            case null:
                throw new ArgumentNullException(nameof(unitOfTime));

            case CalendarDay unitOfTimeAsCalendarDay:
                return(unitOfTimeAsCalendarDay.Plus(unitsToAdd));

            case CalendarMonth unitOfTimeAsCalendarMonth:
                return(unitOfTimeAsCalendarMonth.Plus(unitsToAdd));

            case CalendarQuarter unitOfTimeAsCalendarQuarter:
                return(unitOfTimeAsCalendarQuarter.Plus(unitsToAdd));

            case CalendarYear unitOfTimeAsCalendarYear:
                return(unitOfTimeAsCalendarYear.Plus(unitsToAdd));

            case CalendarUnbounded _:
                throw new InvalidOperationException("Cannot add to unbounded time.");

            case FiscalMonth unitOfTimeAsFiscalMonth:
                return(unitOfTimeAsFiscalMonth.Plus(unitsToAdd));

            case FiscalQuarter unitOfTimeAsFiscalQuarter:
                return(unitOfTimeAsFiscalQuarter.Plus(unitsToAdd));

            case FiscalYear unitOfTimeAsFiscalYear:
                return(unitOfTimeAsFiscalYear.Plus(unitsToAdd));

            case FiscalUnbounded _:
                throw new InvalidOperationException("Cannot add to unbounded time.");

            case GenericMonth unitOfTimeAsGenericMonth:
                return(unitOfTimeAsGenericMonth.Plus(unitsToAdd));

            case GenericQuarter unitOfTimeAsGenericQuarter:
                return(unitOfTimeAsGenericQuarter.Plus(unitsToAdd));

            case GenericYear unitOfTimeAsGenericYear:
                return(unitOfTimeAsGenericYear.Plus(unitsToAdd));

            case GenericUnbounded _:
                throw new InvalidOperationException("Cannot add to unbounded time.");
            }

            throw new NotSupportedException("this type of unit-of-time is not supported: " + unitOfTime.GetType());
        }