| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimeOffsetPeriodExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using MyNet.Primitives; |
| | | 10 | | using MyNet.Temporal.Intervals; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Temporal; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Fluent helpers for <see cref="DateTimeOffsetPeriod"/>. Status helpers require an explicit <c>now</c>; |
| | | 18 | | /// they do not call <see cref="DateTimeOffset.Now"/>. |
| | | 19 | | /// </summary> |
| | | 20 | | public static class DateTimeOffsetPeriodExtensions |
| | | 21 | | { |
| | | 22 | | extension(DateTimeOffsetPeriod left) |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Returns whether <paramref name="now"/> lies inside the period. |
| | | 26 | | /// </summary> |
| | 0 | 27 | | public bool IsCurrent(DateTimeOffset now) => left.Contains(now); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns whether the period is entirely before <paramref name="now"/>. |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public bool IsPast(DateTimeOffset now) => left.IsPast(now); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Returns whether the period is entirely after <paramref name="now"/>. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public bool IsFuture(DateTimeOffset now) => left.IsFuture(now); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Enumerates hour ticks from start (inclusive) while strictly before end. |
| | | 41 | | /// </summary> |
| | | 42 | | public IEnumerable<DateTimeOffset> EnumerateHours() |
| | | 43 | | { |
| | 0 | 44 | | for (var current = left.Start!.Value.Value; |
| | 0 | 45 | | current < left.End!.Value.Value; |
| | 0 | 46 | | current = current.AddHours(1)) |
| | | 47 | | { |
| | 0 | 48 | | yield return current; |
| | | 49 | | } |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Enumerates calendar-day midnights (in the start offset) from the start date while strictly before end. |
| | | 54 | | /// </summary> |
| | | 55 | | public IEnumerable<DateTimeOffset> EnumerateDays() |
| | | 56 | | { |
| | 0 | 57 | | var start = left.Start!.Value.Value; |
| | 0 | 58 | | for (var current = new DateTimeOffset(start.Year, start.Month, start.Day, 0, 0, 0, start.Offset); |
| | 0 | 59 | | current < left.End!.Value.Value; |
| | 0 | 60 | | current = current.AddDays(1)) |
| | | 61 | | { |
| | 0 | 62 | | yield return current; |
| | | 63 | | } |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Fixed-duration gap between two non-overlapping periods; <see cref="TimeSpan.Zero"/> if they overlap. |
| | | 68 | | /// For the gap as an interval, use the generic interval <c>Gap</c> method. |
| | | 69 | | /// </summary> |
| | | 70 | | public TimeSpan TimeGap(DateTimeOffsetPeriod right) => |
| | 0 | 71 | | left.Overlaps(right) |
| | 0 | 72 | | ? TimeSpan.Zero |
| | 0 | 73 | | : left.End!.Value.Value < right.Start!.Value.Value |
| | 0 | 74 | | ? right.Start.Value.Value - left.End.Value.Value |
| | 0 | 75 | | : left.Start!.Value.Value - right.End!.Value.Value; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets the start time-of-day component. |
| | | 79 | | /// </summary> |
| | 0 | 80 | | public TimeSpan StartTimeOfDay => left.Start!.Value.Value.TimeOfDay; |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// End time-of-day, collapsing sub-minute intervals to the start time when below <paramref name="minimumDuratio |
| | | 84 | | /// </summary> |
| | | 85 | | public TimeSpan EndTimeOfDay(TimeSpan minimumDuration = default) |
| | | 86 | | { |
| | 0 | 87 | | var minimum = minimumDuration == TimeSpan.Zero ? TimeSpan.FromMinutes(1) : minimumDuration; |
| | 0 | 88 | | var start = left.StartTimeOfDay; |
| | 0 | 89 | | var end = left.End!.Value.Value.TimeOfDay; |
| | | 90 | | |
| | 0 | 91 | | return end > start && end - start < minimum ? start : end; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets a value indicating whether the exclusive end falls on a later calendar day than the start (using each b |
| | | 96 | | /// </summary> |
| | 0 | 97 | | public bool SpansMidnight => left.End!.Value.Value.Date > left.Start!.Value.Value.Date; |
| | | 98 | | } |
| | | 99 | | } |
| | | 100 | | |