| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="Period.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Primitives.Intervals; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a closed temporal interval where both the start and end boundaries are defined and are inclusive. This cl |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="start">The start DateTime of the interval.</param> |
| | | 15 | | /// <param name="end">The end DateTime of the interval.</param> |
| | 81 | 16 | | public sealed class Period(DateTime start, DateTime end) : TemporalInterval<Period>(start, end, inclusiveStart: true, in |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates a new instance of the <see cref="Period"/> class based on a specified start DateTime and a duration repr |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="start">The start DateTime of the period.</param> |
| | | 22 | | /// <param name="duration">The duration of the period.</param> |
| | | 23 | | /// <returns>A new instance of the <see cref="Period"/> class.</returns> |
| | 3 | 24 | | public static Period FromDuration(DateTime start, TimeSpan duration) => new(start, start + duration); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Shifts the period by a specified TimeSpan offset, creating a new period that starts and ends at times that are o |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="offset">The TimeSpan offset to apply to the period.</param> |
| | | 30 | | /// <returns>A new instance of the <see cref="Period"/> class with the shifted start and end times.</returns> |
| | 9 | 31 | | public Period Shift(TimeSpan offset) => new(Start!.Value.Value + offset, End!.Value.Value + offset); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Extends the period by a specified TimeSpan offset, creating a new period that starts at the original start time |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="offset">The TimeSpan offset to apply to the end of the period.</param> |
| | | 37 | | /// <returns>A new instance of the <see cref="Period"/> class with the extended end time.</returns> |
| | 6 | 38 | | public Period Extend(TimeSpan offset) => new(Start!.Value.Value, End!.Value.Value + offset); |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | protected override Period Create(IntervalBoundary<DateTime>? start, IntervalBoundary<DateTime>? end) => |
| | 6 | 42 | | !start.HasValue || !end.HasValue |
| | 6 | 43 | | ? throw new InvalidOperationException("Period must be bounded.") |
| | 6 | 44 | | : !start.Value.IsInclusive || end.Value.IsInclusive |
| | 6 | 45 | | ? throw new InvalidOperationException("Period only supports [start, end) boundaries.") |
| | 6 | 46 | | : new(start.Value.Value, end.Value.Value); |
| | | 47 | | } |
| | | 48 | | |