| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeOffsetPeriod.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | using MyNet.Primitives.Intervals; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Temporal.Intervals; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Half-open absolute <see cref="DateTimeOffset"/> interval <c>[start, end)</c>: start inclusive, end exclusive. |
| | | 15 | | /// Prefer this type for API / domain occupation spans across offsets. For civil UI time, use |
| | | 16 | | /// <see cref="DateTimePeriod"/> instead. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="start">Inclusive start.</param> |
| | | 19 | | /// <param name="end">Exclusive end.</param> |
| | | 20 | | public sealed class DateTimeOffsetPeriod(DateTimeOffset start, DateTimeOffset end) |
| | 36 | 21 | | : DateTimeOffsetInterval<DateTimeOffsetPeriod>(start, end, inclusiveStart: true, inclusiveEnd: false) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates a period from a start and a fixed <see cref="TimeSpan"/> duration. |
| | | 25 | | /// </summary> |
| | 3 | 26 | | public static DateTimeOffsetPeriod FromDuration(DateTimeOffset start, TimeSpan duration) => new(start, start + durat |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Returns a new DateTimeOffsetPeriod shifted by <paramref name="offset"/>. |
| | | 30 | | /// </summary> |
| | 3 | 31 | | public DateTimeOffsetPeriod Shift(TimeSpan offset) => new(Start!.Value.Value + offset, End!.Value.Value + offset); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns a new DateTimeOffsetPeriod with the end extended by <paramref name="offset"/>. |
| | | 35 | | /// </summary> |
| | 3 | 36 | | public DateTimeOffsetPeriod Extend(TimeSpan offset) => new(Start!.Value.Value, End!.Value.Value + offset); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Preserves wall-clock components as <see cref="DateTimeKind.Unspecified"/> (civil / UI). |
| | | 40 | | /// Use <see cref="ToUtcDateTimePeriod"/> when you need absolute UTC clock faces. |
| | | 41 | | /// </summary> |
| | | 42 | | public DateTimePeriod ToDateTimePeriod() => |
| | 3 | 43 | | new( |
| | 3 | 44 | | DateTime.SpecifyKind(Start!.Value.Value.DateTime, DateTimeKind.Unspecified), |
| | 3 | 45 | | DateTime.SpecifyKind(End!.Value.Value.DateTime, DateTimeKind.Unspecified)); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Converts bounds via <see cref="DateTimeOffset.UtcDateTime"/> (<see cref="DateTimeKind.Utc"/>). |
| | | 49 | | /// </summary> |
| | | 50 | | public DateTimePeriod ToUtcDateTimePeriod() => |
| | 3 | 51 | | new(Start!.Value.Value.UtcDateTime, End!.Value.Value.UtcDateTime); |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | protected override DateTimeOffsetPeriod Create(IntervalBoundary<DateTimeOffset>? start, IntervalBoundary<DateTimeOff |
| | 3 | 55 | | !start.HasValue || !end.HasValue |
| | 3 | 56 | | ? throw new InvalidOperationException("DateTimeOffsetPeriod must be bounded.") |
| | 3 | 57 | | : !start.Value.IsInclusive || end.Value.IsInclusive |
| | 3 | 58 | | ? throw new InvalidOperationException("DateTimeOffsetPeriod only supports [start, end) boundaries.") |
| | 3 | 59 | | : new DateTimeOffsetPeriod(start.Value.Value, end.Value.Value); |
| | | 60 | | } |
| | | 61 | | |