Exemplo n.º 1
0
        internal static DateTime Quantize(this DateTime target, DateTimeRangeGranularity granularity)
        {
            if (granularity == DateTimeRangeGranularity.Unspecified)
            {
                throw new ArgumentOutOfRangeException(nameof(granularity));
            }

            switch (granularity)
            {
            case DateTimeRangeGranularity.Exact:

                return(target);

            case DateTimeRangeGranularity.AccurateToTheMillisecond:

                return(target.MidpointOfThisMillisecond());

            case DateTimeRangeGranularity.AccurateToTheSecond:

                return(target.MidpointOfThisSecond());

            case DateTimeRangeGranularity.AccurateToTheMinute:

                return(target.MidpointOfThisMinute());

            case DateTimeRangeGranularity.AccurateToTheHour:

                return(target.MidpointOfThisHour());

            case DateTimeRangeGranularity.AccurateToTheDay:

                return(target.MidpointOfThisDay());

            case DateTimeRangeGranularity.AccurateToTheMonth:

                return(target.MidpointOfThisMonth());

            case DateTimeRangeGranularity.AccurateToTheYear:

                return(target.MidpointOfThisYear());

            default:

                throw new ArgumentException($"{granularity} is not a supported {nameof(DateTimeRangeGranularity)}.", nameof(granularity));
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DateTimeRange" /> class.
 /// </summary>
 /// <param name="start">
 /// A starting <see cref="DateTime" /> for the range.
 /// </param>
 /// <param name="end">
 /// An ending <see cref="DateTime" /> for the range.
 /// </param>
 /// <param name="granularity">
 /// A value that specifies how granular the range should be.
 /// </param>
 /// <exception cref="ArgumentException">
 /// One of the <see cref="DateTime" /> s has a <see cref="DateTimeKind" /> equal to <see cref="DateTimeKind.Unspecified" />
 /// and the other specifies a kind.
 /// </exception>
 /// <exception cref="ArgumentOutOfRangeException">
 /// <paramref name="start" /> is later than <paramref name="end" /> -or- <paramref name="granularity" /> is equal to
 /// <see cref="DateTimeRangeGranularity.Unspecified" />.
 /// </exception>
 public DateTimeRange(DateTime start, DateTime end, DateTimeRangeGranularity granularity)
 {
     if (start.Kind == end.Kind)
     {
         Initialize(start, end, granularity);
     }
     else if (start.Kind == DateTimeKind.Unspecified)
     {
         throw new ArgumentException($"The constructor for {nameof(DateTimeRange)} cannot accept a {nameof(DateTime)} with unspecified kind as an argument for {nameof(start)} if the argument for {nameof(end)} specifies a kind.");
     }
     else if (end.Kind == DateTimeKind.Unspecified)
     {
         throw new ArgumentException($"The constructor for {nameof(DateTimeRange)} cannot accept a {nameof(DateTime)} with unspecified kind as an argument for {nameof(end)} if the argument for {nameof(start)} specifies a kind.");
     }
     else if (start.Kind == DateTimeKind.Utc)
     {
         Initialize(start, end.ToUniversalTime(), granularity);
     }
     else if (end.Kind == DateTimeKind.Utc)
     {
         Initialize(start.ToUniversalTime(), end, granularity);
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Converts the current <see cref="DateTimeRangeGranularity" /> to an array of bytes.
 /// </summary>
 /// <param name="target">
 /// The current instance of the <see cref="DateTimeRangeGranularity" />.
 /// </param>
 /// <returns>
 /// An array of bytes representing the current <see cref="DateTimeRangeGranularity" />.
 /// </returns>
 public static Byte[] ToByteArray(this DateTimeRangeGranularity target) => BitConverter.GetBytes((Int32)target);