| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeSpanExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Primitives.Intervals; |
| | | 9 | | using MyNet.Primitives.Temporal; |
| | | 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 TimeSpanExtensions |
| | | 16 | | { |
| | | 17 | | extension(TimeSpan timeSpan) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Converts a <see cref="TimeSpan"/> to a <see cref="TimeOnly"/> by taking the time component of the <see cref= |
| | | 21 | | /// </summary> |
| | | 22 | | /// <returns>A <see cref="TimeOnly"/> representing the time component of the <see cref="TimeSpan"/>.</returns> |
| | 3 | 23 | | public TimeOnly ToTime() => TimeOnly.FromTimeSpan(timeSpan); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Adds the given <see cref="FluentTimeSpan"/> from a <see cref="TimeSpan"/> and returns resulting <see cref="F |
| | | 27 | | /// </summary> |
| | 6 | 28 | | public FluentTimeSpan AddFluentTimeSpan(FluentTimeSpan fluentTimeSpan) => fluentTimeSpan.Add(timeSpan); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Subtracts the given <see cref="FluentTimeSpan"/> from a <see cref="TimeSpan"/> and returns resulting <see cr |
| | | 32 | | /// </summary> |
| | 3 | 33 | | public FluentTimeSpan SubtractFluentTimeSpan(FluentTimeSpan fluentTimeSpan) => FluentTimeSpan.SubtractInternal(t |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Subtracts given <see cref="TimeSpan"/> from current date (<see cref="DateTime.Now"/>) and returns resulting |
| | | 37 | | /// </summary> |
| | 0 | 38 | | public DateTime Ago() => timeSpan.Before(DateTime.Now); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Subtracts given <see cref="TimeSpan"/> from <paramref name="originalValue"/> <see cref="DateTime"/> and retu |
| | | 42 | | /// </summary> |
| | 0 | 43 | | public DateTime Ago(DateTime originalValue) => timeSpan.Before(originalValue); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Adds given <see cref="TimeSpan"/> to supplied <paramref name="originalValue"/> <see cref="DateTime"/> and re |
| | | 47 | | /// </summary> |
| | | 48 | | /// <seealso cref="From(TimeSpan, DateTime)"/> |
| | | 49 | | /// <remarks> |
| | | 50 | | /// Synonym of <see cref="From(TimeSpan, DateTime)"/> method. |
| | | 51 | | /// </remarks> |
| | 3 | 52 | | public DateTime Since(DateTime originalValue) => timeSpan.From(originalValue); |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Adds given <see cref="TimeSpan"/> to current <see cref="DateTime.Now"/> and returns resulting <see cref="Dat |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public DateTime FromNow() => timeSpan.From(DateTime.Now); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Adds given <see cref="TimeSpan"/> to supplied <paramref name="originalValue"/> <see cref="DateTime"/> and re |
| | | 61 | | /// </summary> |
| | 6 | 62 | | public DateTime From(DateTime originalValue) => originalValue + timeSpan; |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Subtracts given <see cref="TimeSpan"/> from <paramref name="originalValue"/> <see cref="DateTime"/> and retu |
| | | 66 | | /// </summary> |
| | 3 | 67 | | public DateTime Before(DateTime originalValue) => originalValue - timeSpan; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Rounds the <see cref="TimeSpan"/> to the nearest specified time unit (second, minute, hour, or day) based on |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="rt">The time unit to which the <see cref="TimeSpan"/> should be rounded.</param> |
| | | 73 | | /// <returns>The rounded <see cref="TimeSpan"/>.</returns> |
| | | 74 | | /// <exception cref="ArgumentException">Thrown when an unsupported rounding unit is provided.</exception> |
| | | 75 | | public TimeSpan Round(RoundTo rt) |
| | | 76 | | { |
| | | 77 | | TimeSpan rounded; |
| | | 78 | | |
| | | 79 | | switch (rt) |
| | | 80 | | { |
| | | 81 | | case RoundTo.Second: |
| | | 82 | | { |
| | 0 | 83 | | rounded = new(timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); |
| | 0 | 84 | | if (timeSpan.Milliseconds >= 500) |
| | | 85 | | { |
| | 0 | 86 | | rounded += 1.Seconds(); |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | break; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | case RoundTo.Minute: |
| | | 93 | | { |
| | 3 | 94 | | rounded = new(timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, 0); |
| | 3 | 95 | | if (timeSpan.Seconds >= 30) |
| | | 96 | | { |
| | 3 | 97 | | rounded += 1.Minutes(); |
| | | 98 | | } |
| | | 99 | | |
| | 3 | 100 | | break; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | case RoundTo.Hour: |
| | | 104 | | { |
| | 0 | 105 | | rounded = new(timeSpan.Days, timeSpan.Hours, 0, 0); |
| | 0 | 106 | | if (timeSpan.Minutes >= 30) |
| | | 107 | | { |
| | 0 | 108 | | rounded += 1.Hours(); |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | break; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | case RoundTo.Day: |
| | | 115 | | { |
| | 0 | 116 | | rounded = new(timeSpan.Days, 0, 0, 0); |
| | 0 | 117 | | if (timeSpan.Hours >= 12) |
| | | 118 | | { |
| | 0 | 119 | | rounded += 1.Days(); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | break; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | default: |
| | | 126 | | { |
| | 0 | 127 | | throw new ArgumentException(null, nameof(rt)); |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 3 | 131 | | return rounded; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Adds a specified value of a given time unit to the current <see cref="TimeSpan"/> and returns the resulting |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="value">The amount of time to add.</param> |
| | | 138 | | /// <param name="timeUnitToGet">The unit of time for the value to add.</param> |
| | | 139 | | /// <returns>The resulting <see cref="TimeSpan"/> after adding the specified time.</returns> |
| | 0 | 140 | | public TimeSpan Add(int value, TimeUnit timeUnitToGet) => timeSpan.Add(value.ToTimeSpan(timeUnitToGet)); |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Converts the current <see cref="TimeSpan"/> to an integer value representing the total amount of time in the |
| | | 144 | | /// </summary> |
| | | 145 | | /// <param name="unit">The time unit to convert to.</param> |
| | | 146 | | /// <returns>The total amount of time in the specified time unit.</returns> |
| | | 147 | | public double ConvertTo(TimeUnit unit) |
| | | 148 | | { |
| | 0 | 149 | | var abs = timeSpan.Duration(); |
| | | 150 | | |
| | 0 | 151 | | return unit switch |
| | 0 | 152 | | { |
| | 0 | 153 | | TimeUnit.Millisecond => Math.Round(abs.TotalMilliseconds), |
| | 0 | 154 | | TimeUnit.Second => Math.Round(abs.TotalSeconds), |
| | 0 | 155 | | TimeUnit.Minute => Math.Round(abs.TotalMinutes), |
| | 0 | 156 | | TimeUnit.Hour => Math.Round(abs.TotalHours), |
| | 0 | 157 | | TimeUnit.Day => Math.Round(abs.TotalDays), |
| | 0 | 158 | | TimeUnit.Week => Math.Round(abs.TotalDays / DateTimeHelper.DaysPerWeek), |
| | 0 | 159 | | TimeUnit.Month => Math.Round(abs.TotalDays / DateTimeHelper.DaysPerMonth), |
| | 0 | 160 | | TimeUnit.Year => Math.Round(abs.TotalDays / DateTimeHelper.DaysPerYear), |
| | 0 | 161 | | _ => 0 |
| | 0 | 162 | | }; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// When same-day ranges are required, aligns the non-edited boundary to the edited one if <paramref name="end"/ |
| | | 167 | | /// </summary> |
| | | 168 | | /// <param name="end">The end time of the range.</param> |
| | | 169 | | /// <param name="editedEnd">When <see langword="true"/>, the end boundary was edited; otherwise the start bounda |
| | | 170 | | public (TimeSpan Start, TimeSpan End) CoerceSameDayRange(TimeSpan end, bool editedEnd = false) => |
| | 3 | 171 | | end >= timeSpan ? (timeSpan, end) |
| | 3 | 172 | | : editedEnd ? (end, end) : (timeSpan, timeSpan); |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Builds a <see cref="Period"/> from time-of-day values on a reference calendar date. |
| | | 176 | | /// </summary> |
| | | 177 | | public Period ToPeriod(TimeSpan end, DateTime referenceDate, bool allowOvernight = false) |
| | | 178 | | { |
| | 6 | 179 | | referenceDate = referenceDate.DiscardTime(); |
| | | 180 | | |
| | 6 | 181 | | var startDateTime = referenceDate.Add(timeSpan); |
| | 6 | 182 | | var endDateTime = allowOvernight && end <= timeSpan |
| | 6 | 183 | | ? referenceDate.AddDays(1).Add(end) |
| | 6 | 184 | | : referenceDate.Add(end); |
| | | 185 | | |
| | 6 | 186 | | if (!allowOvernight && end == timeSpan) |
| | 0 | 187 | | endDateTime = startDateTime.AddTicks(1); |
| | | 188 | | |
| | 6 | 189 | | return startDateTime.ToPeriod(endDateTime); |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | extension(FluentTimeSpan fluentTimeSpan) |
| | | 194 | | { |
| | | 195 | | /// <summary> |
| | | 196 | | /// Converts a <see cref="FluentTimeSpan"/> to a <see cref="TimeOnly"/> by taking the time component of the <see |
| | | 197 | | /// </summary> |
| | | 198 | | /// <returns>A <see cref="TimeOnly"/> representing the time component of the <see cref="FluentTimeSpan"/>.</retu |
| | 0 | 199 | | public TimeOnly ToTime() => TimeOnly.FromTimeSpan(fluentTimeSpan); |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Subtracts given <see cref="FluentTimeSpan"/> from current date (<see cref="DateTime.Now"/>) and returns resu |
| | | 203 | | /// </summary> |
| | 0 | 204 | | public DateTime Ago() => fluentTimeSpan.Before(DateTime.Now); |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Subtracts given <see cref="TimeSpan"/> from <paramref name="originalValue"/> <see cref="DateTime"/> and retu |
| | | 208 | | /// </summary> |
| | 6 | 209 | | public DateTime Ago(DateTime originalValue) => fluentTimeSpan.Before(originalValue); |
| | | 210 | | |
| | | 211 | | /// <summary> |
| | | 212 | | /// Subtracts given <see cref="TimeSpan"/> from <paramref name="originalValue"/> <see cref="DateTime"/> and retu |
| | | 213 | | /// </summary> |
| | 228 | 214 | | public DateTime Before(DateTime originalValue) => originalValue.AddMonths(-fluentTimeSpan.Months).AddYears(-flue |
| | | 215 | | |
| | | 216 | | /// <summary> |
| | | 217 | | /// Adds given <see cref="TimeSpan"/> to current <see cref="DateTime.Now"/> and returns resulting <see cref="Dat |
| | | 218 | | /// </summary> |
| | 0 | 219 | | public DateTime FromNow() => fluentTimeSpan.From(DateTime.Now); |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Adds given <see cref="TimeSpan"/> to supplied <paramref name="originalValue"/> <see cref="DateTime"/> and re |
| | | 223 | | /// </summary> |
| | 204 | 224 | | public DateTime From(DateTime originalValue) => originalValue.AddMonths(fluentTimeSpan.Months).AddYears(fluentTi |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Adds given <see cref="TimeSpan"/> to supplied <paramref name="originalValue"/> <see cref="DateTime"/> and re |
| | | 228 | | /// </summary> |
| | | 229 | | /// <seealso cref="From(FluentTimeSpan, DateTime)"/> |
| | | 230 | | /// <remarks> |
| | | 231 | | /// Synonym of <see cref="From(FluentTimeSpan, DateTime)"/> method. |
| | | 232 | | /// </remarks> |
| | 3 | 233 | | public DateTime Since(DateTime originalValue) => fluentTimeSpan.From(originalValue); |
| | | 234 | | } |
| | | 235 | | } |
| | | 236 | | |