| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PeriodExtensions.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.Intervals; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Primitives; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class PeriodExtensions |
| | | 16 | | { |
| | | 17 | | extension(Period left) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Determines whether the current period is current, meaning that the current date and time falls within the pe |
| | | 21 | | /// </summary> |
| | | 22 | | /// <returns>True if the current date and time falls within the period; otherwise, false.</returns> |
| | 3 | 23 | | public bool IsCurrent() => left.Contains(DateTime.Now); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Determines whether the current period is in the past, meaning that the end of the period is before the curre |
| | | 27 | | /// </summary> |
| | | 28 | | /// <returns>True if the period is in the past; otherwise, false.</returns> |
| | 6 | 29 | | public bool IsPast() => left.IsPast(DateTime.Now); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Determines whether the current period is in the future, meaning that the start of the period is after the cu |
| | | 33 | | /// </summary> |
| | | 34 | | /// <returns>True if the period is in the future; otherwise, false.</returns> |
| | 6 | 35 | | public bool IsFuture() => left.IsFuture(DateTime.Now); |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Shifts the current period by a specified offset. This method creates a new period with the start and end tim |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="offset">The time span to shift the period by.</param> |
| | | 41 | | /// <returns>A new period shifted by the specified offset.</returns> |
| | 0 | 42 | | public Period Shift(TimeSpan offset) => new(left.Start!.Value.Value + offset, left.End!.Value.Value + offset); |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Extends the current period by a specified duration. This method creates a new period with the end time exten |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="duration">The duration to extend the period by.</param> |
| | | 48 | | /// <returns>A new period extended by the specified duration.</returns> |
| | 0 | 49 | | public Period Extend(TimeSpan duration) => new(left.Start!.Value.Value, left.End!.Value.Value + duration); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Enumerates the hours within the current period. This method generates a sequence of DateTime values represen |
| | | 53 | | /// </summary> |
| | | 54 | | /// <returns>An enumerable of DateTime values for each hour within the period.</returns> |
| | | 55 | | public IEnumerable<DateTime> EnumerateHours() |
| | | 56 | | { |
| | 6 | 57 | | for (var current = left.Start!.Value.Value; |
| | 18 | 58 | | current < left.End!.Value.Value; |
| | 12 | 59 | | current = current.AddHours(1)) |
| | | 60 | | { |
| | 12 | 61 | | yield return current; |
| | | 62 | | } |
| | 6 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Enumerates the days within the current period. This method generates a sequence of DateTime values represent |
| | | 67 | | /// </summary> |
| | | 68 | | /// <returns>An enumerable of DateTime values for each day within the period.</returns> |
| | | 69 | | public IEnumerable<DateTime> EnumerateDays() |
| | | 70 | | { |
| | 3 | 71 | | for (var current = left.Start!.Value.Value.Date; |
| | 12 | 72 | | current < left.End!.Value.Value; |
| | 9 | 73 | | current = current.AddDays(1)) |
| | | 74 | | { |
| | 9 | 75 | | yield return current; |
| | | 76 | | } |
| | 3 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Calculates the gap between the current period and another period. This method returns the time span between |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="right">The other period to calculate the gap with.</param> |
| | | 83 | | /// <returns>The time span representing the gap between the two periods, or TimeSpan.Zero if they overlap.</retu |
| | | 84 | | public TimeSpan Gap(Period right) => |
| | 6 | 85 | | left.Overlaps(right) |
| | 6 | 86 | | ? TimeSpan.Zero |
| | 6 | 87 | | : left.End!.Value.Value < right.Start!.Value.Value |
| | 6 | 88 | | ? right.Start.Value.Value - left.End.Value.Value |
| | 6 | 89 | | : left.Start!.Value.Value - right.End!.Value.Value; |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets the start time-of-day component of the period. |
| | | 93 | | /// </summary> |
| | 6 | 94 | | public TimeSpan StartTimeOfDay => left.Start!.Value.Value.TimeOfDay; |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Gets the end time-of-day component, collapsing sub-minute intervals to the start time. |
| | | 98 | | /// </summary> |
| | | 99 | | /// <param name="minimumDuration">Minimum duration before end time is returned; defaults to one minute.</param> |
| | | 100 | | public TimeSpan EndTimeOfDay(TimeSpan minimumDuration = default) |
| | | 101 | | { |
| | 3 | 102 | | var minimum = minimumDuration == TimeSpan.Zero ? TimeSpan.FromMinutes(1) : minimumDuration; |
| | 3 | 103 | | var start = left.StartTimeOfDay; |
| | 3 | 104 | | var end = left.End!.Value.Value.TimeOfDay; |
| | | 105 | | |
| | 3 | 106 | | return end > start && end - start < minimum ? start : end; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets a value indicating whether the period spans more than one calendar day. |
| | | 111 | | /// </summary> |
| | 3 | 112 | | public bool SpansMidnight => left.End!.Value.Value.Date > left.Start!.Value.Value.Date; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |