| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimePeriod.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 civil <see cref="DateTime"/> interval <c>[start, end)</c>: start inclusive, end exclusive. |
| | | 15 | | /// Prefer this type for UI / desktop occupation spans. For absolute instants (APIs, multi-offset), use |
| | | 16 | | /// <see cref="DateTimeOffsetPeriod"/> instead. <see cref="DateTime.Kind"/> is not normalized by constructors. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="start">Inclusive start.</param> |
| | | 19 | | /// <param name="end">Exclusive end.</param> |
| | 96 | 20 | | public sealed class DateTimePeriod(DateTime start, DateTime end) : DateTimeInterval<DateTimePeriod>(start, end, inclusiv |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Creates a period from a start and a fixed <see cref="TimeSpan"/> duration. |
| | | 24 | | /// </summary> |
| | 3 | 25 | | public static DateTimePeriod FromDuration(DateTime start, TimeSpan duration) => new(start, start + duration); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Returns a new DateTimePeriod shifted by <paramref name="offset"/>. |
| | | 29 | | /// </summary> |
| | 9 | 30 | | public DateTimePeriod Shift(TimeSpan offset) => new(Start!.Value.Value + offset, End!.Value.Value + offset); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Returns a new DateTimePeriod with the end extended by <paramref name="offset"/>. |
| | | 34 | | /// </summary> |
| | 6 | 35 | | public DateTimePeriod Extend(TimeSpan offset) => new(Start!.Value.Value, End!.Value.Value + offset); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Converts bounds to <see cref="DateTimeOffset"/> with the given fixed offset. |
| | | 39 | | /// <see cref="DateTimeKind.Utc"/> / <see cref="DateTimeKind.Local"/> preserve the absolute instant then project to |
| | | 40 | | /// <see cref="DateTimeKind.Unspecified"/> is treated as civil wall time with that offset attached. |
| | | 41 | | /// </summary> |
| | | 42 | | public DateTimeOffsetPeriod ToOffsetPeriod(TimeSpan offset) => |
| | 6 | 43 | | new(ToOffsetBound(Start!.Value.Value, offset), ToOffsetBound(End!.Value.Value, offset)); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Converts bounds into <paramref name="timeZone"/> (DST-aware). |
| | | 47 | | /// Utc/Local preserve the absolute instant; Unspecified is treated as wall time in that zone. |
| | | 48 | | /// </summary> |
| | | 49 | | public DateTimeOffsetPeriod ToOffsetPeriod(TimeZoneInfo timeZone) |
| | | 50 | | { |
| | 0 | 51 | | ArgumentNullException.ThrowIfNull(timeZone); |
| | | 52 | | |
| | 0 | 53 | | return new DateTimeOffsetPeriod(ToOffsetBound(Start!.Value.Value, timeZone), ToOffsetBound(End!.Value.Value, tim |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | protected override DateTimePeriod Create(IntervalBoundary<DateTime>? start, IntervalBoundary<DateTime>? end) => |
| | 6 | 58 | | !start.HasValue || !end.HasValue |
| | 6 | 59 | | ? throw new InvalidOperationException("DateTimePeriod must be bounded.") |
| | 6 | 60 | | : !start.Value.IsInclusive || end.Value.IsInclusive |
| | 6 | 61 | | ? throw new InvalidOperationException("DateTimePeriod only supports [start, end) boundaries.") |
| | 6 | 62 | | : new DateTimePeriod(start.Value.Value, end.Value.Value); |
| | | 63 | | |
| | 12 | 64 | | private static DateTimeOffset ToOffsetBound(DateTime value, TimeSpan offset) => value.Kind switch |
| | 12 | 65 | | { |
| | 6 | 66 | | DateTimeKind.Utc or DateTimeKind.Local => new DateTimeOffset(value).ToOffset(offset), |
| | 6 | 67 | | _ => new DateTimeOffset(DateTime.SpecifyKind(value, DateTimeKind.Unspecified), offset) |
| | 12 | 68 | | }; |
| | | 69 | | |
| | 0 | 70 | | private static DateTimeOffset ToOffsetBound(DateTime value, TimeZoneInfo timeZone) => value.Kind switch |
| | 0 | 71 | | { |
| | 0 | 72 | | DateTimeKind.Utc or DateTimeKind.Local => TimeZoneInfo.ConvertTime(new DateTimeOffset(value), timeZone), |
| | 0 | 73 | | _ => new DateTimeOffset( |
| | 0 | 74 | | DateTime.SpecifyKind(value, DateTimeKind.Unspecified), |
| | 0 | 75 | | timeZone.GetUtcOffset(DateTime.SpecifyKind(value, DateTimeKind.Unspecified))) |
| | 0 | 76 | | }; |
| | | 77 | | } |
| | | 78 | | |