| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DateTimePeriodExtensions.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="DateTimePeriod"/>. Status helpers require an explicit <c>now</c>; |
| | | 18 | | /// they do not call <see cref="DateTime.Now"/>. |
| | | 19 | | /// </summary> |
| | | 20 | | public static class DateTimePeriodExtensions |
| | | 21 | | { |
| | | 22 | | extension(DateTimePeriod left) |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// Returns whether <paramref name="now"/> lies inside the period. |
| | | 26 | | /// </summary> |
| | 0 | 27 | | public bool IsCurrent(DateTime 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(DateTime 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(DateTime now) => left.IsFuture(now); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Enumerates hour ticks from start (inclusive) while strictly before end. |
| | | 41 | | /// </summary> |
| | | 42 | | public IEnumerable<DateTime> EnumerateHours() |
| | | 43 | | { |
| | 6 | 44 | | for (var current = left.Start!.Value.Value; |
| | 18 | 45 | | current < left.End!.Value.Value; |
| | 12 | 46 | | current = current.AddHours(1)) |
| | | 47 | | { |
| | 12 | 48 | | yield return current; |
| | | 49 | | } |
| | 6 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Enumerates calendar-day midnights from start.Date while strictly before end. |
| | | 54 | | /// </summary> |
| | | 55 | | public IEnumerable<DateTime> EnumerateDays() |
| | | 56 | | { |
| | 3 | 57 | | for (var current = left.Start!.Value.Value.Date; |
| | 15 | 58 | | current < left.End!.Value.Value; |
| | 12 | 59 | | current = current.AddDays(1)) |
| | | 60 | | { |
| | 12 | 61 | | yield return current; |
| | | 62 | | } |
| | 3 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Fixed-duration gap between two non-overlapping periods; <see cref="TimeSpan.Zero"/> if they overlap. |
| | | 67 | | /// For the gap as an interval, use the generic interval <c>Gap</c> method. |
| | | 68 | | /// </summary> |
| | | 69 | | public TimeSpan TimeGap(DateTimePeriod right) => |
| | 6 | 70 | | left.Overlaps(right) |
| | 6 | 71 | | ? TimeSpan.Zero |
| | 6 | 72 | | : left.End!.Value.Value < right.Start!.Value.Value |
| | 6 | 73 | | ? right.Start.Value.Value - left.End.Value.Value |
| | 6 | 74 | | : left.Start!.Value.Value - right.End!.Value.Value; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the start time-of-day component. |
| | | 78 | | /// </summary> |
| | 6 | 79 | | public TimeSpan StartTimeOfDay => left.Start!.Value.Value.TimeOfDay; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the end time-of-day, collapsing sub-minute intervals to the start time when below <paramref name="minim |
| | | 83 | | /// </summary> |
| | | 84 | | public TimeSpan EndTimeOfDay(TimeSpan minimumDuration = default) |
| | | 85 | | { |
| | 3 | 86 | | var minimum = minimumDuration == TimeSpan.Zero ? TimeSpan.FromMinutes(1) : minimumDuration; |
| | 3 | 87 | | var start = left.StartTimeOfDay; |
| | 3 | 88 | | var end = left.End!.Value.Value.TimeOfDay; |
| | | 89 | | |
| | 3 | 90 | | return end > start && end - start < minimum ? start : end; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets a value indicating whether the exclusive end falls on a later calendar day than the start. |
| | | 95 | | /// </summary> |
| | 3 | 96 | | public bool SpansMidnight => left.End!.Value.Value.Date > left.Start!.Value.Value.Date; |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |