| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeOnlyExtensions.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 | | |
| | | 10 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 11 | | namespace MyNet.Primitives; |
| | | 12 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 13 | | |
| | | 14 | | public static class TimeOnlyExtensions |
| | | 15 | | { |
| | | 16 | | extension(TimeOnly time) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Returns the Start of the given day (the first millisecond of the given <see cref="DateTime"/>). |
| | | 20 | | /// </summary> |
| | 3 | 21 | | public TimeOnly BeginningOfHour() => new(time.Hour, 0, 0, 0); |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Returns the very end of the given day (the last millisecond of the last hour for the given <see cref="DateTi |
| | | 25 | | /// </summary> |
| | 3 | 26 | | public TimeOnly EndOfHour() => new(time.Hour, 59, 59, 999); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Returns the Start of the given day (the first millisecond of the given <see cref="DateTime"/>). |
| | | 30 | | /// </summary> |
| | 3 | 31 | | public TimeOnly BeginningOfMinute() => new(time.Hour, time.Minute, 0, 0); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns the very end of the given day (the last millisecond of the last hour for the given <see cref="DateTi |
| | | 35 | | /// </summary> |
| | 3 | 36 | | public TimeOnly EndOfMinute() => new(time.Hour, time.Minute, 59, 999); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Returns <see cref="DateTime"/> with changed Hour part. |
| | | 40 | | /// </summary> |
| | 3 | 41 | | public TimeOnly SetHour(int hour) => new(hour, time.Minute, time.Second, time.Millisecond); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Returns <see cref="DateTime"/> with changed Minute part. |
| | | 45 | | /// </summary> |
| | 3 | 46 | | public TimeOnly SetMinute(int minute) => new(time.Hour, minute, time.Second, time.Millisecond); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns <see cref="DateTime"/> with changed Second part. |
| | | 50 | | /// </summary> |
| | 3 | 51 | | public TimeOnly SetSecond(int second) => new(time.Hour, time.Minute, second, time.Millisecond); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Returns <see cref="DateTime"/> with changed Millisecond part. |
| | | 55 | | /// </summary> |
| | 3 | 56 | | public TimeOnly SetMillisecond(int millisecond) => new(time.Hour, time.Minute, time.Second, millisecond); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Determines if the given <see cref="DateTime"/> has the same Hour, Minute, Second and Millisecond parts as th |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="date">The <see cref="DateTime"/> to compare with.</param> |
| | | 62 | | /// <returns><c>true</c> if the specified <see cref="DateTime"/> has the same Hour, Minute, Second and Milliseco |
| | 6 | 63 | | public bool SameMilliSecond(TimeOnly date) => time.SameSecond(date) && time.Millisecond == date.Millisecond; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Determines if the given <see cref="DateTime"/> has the same Hour, Minute and Second parts as the current <se |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="date">The <see cref="DateTime"/> to compare with.</param> |
| | | 69 | | /// <returns><c>true</c> if the specified <see cref="DateTime"/> has the same Hour, Minute, Second and Milliseco |
| | 12 | 70 | | public bool SameSecond(TimeOnly date) => time.SameMinute(date) && time.Second == date.Second; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Determines if the given <see cref="DateTime"/> has the same Hour and Minute parts as the current <see cref=" |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="date">The <see cref="DateTime"/> to compare with.</param> |
| | | 76 | | /// <returns><c>true</c> if the specified <see cref="DateTime"/> has the same Hour and Minute parts; otherwise, |
| | 18 | 77 | | public bool SameMinute(TimeOnly date) => time.SameHour(date) && time.Minute == date.Minute; |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Determines if the given <see cref="DateTime"/> has the same Hour part as the current <see cref="DateTime"/>. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="date">The <see cref="DateTime"/> to compare with.</param> |
| | | 83 | | /// <returns><c>true</c> if the specified <see cref="DateTime"/> has the same Hour part; otherwise, <c>false</c> |
| | 24 | 84 | | public bool SameHour(TimeOnly date) => time.Hour == date.Hour; |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Generates a sequence of TimeOnly values starting from a minimum value and ending at a maximum value, with a |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="max">The maximum value of the range.</param> |
| | | 90 | | /// <param name="step">The step value for each iteration.</param> |
| | | 91 | | /// <param name="unit">The time unit for the step.</param> |
| | | 92 | | /// <returns>A sequence of TimeOnly values within the specified range.</returns> |
| | | 93 | | public IEnumerable<TimeOnly> Range(TimeOnly max, int step = 1, TimeUnit unit = TimeUnit.Hour) |
| | | 94 | | { |
| | 21 | 95 | | ArgumentOutOfRangeException.ThrowIfZero(step); |
| | | 96 | | |
| | 18 | 97 | | Func<TimeOnly, TimeOnly> increment = unit switch |
| | 18 | 98 | | { |
| | 3 | 99 | | TimeUnit.Millisecond => x => x.Add(step.Milliseconds()), |
| | 3 | 100 | | TimeUnit.Second => x => x.Add(step.Seconds()), |
| | 3 | 101 | | TimeUnit.Minute => x => x.AddMinutes(step), |
| | 6 | 102 | | TimeUnit.Hour => x => x.AddHours(step), |
| | 3 | 103 | | TimeUnit.Day => throw new InvalidOperationException(), |
| | 0 | 104 | | TimeUnit.Week => throw new InvalidOperationException(), |
| | 0 | 105 | | TimeUnit.Month => throw new InvalidOperationException(), |
| | 0 | 106 | | TimeUnit.Year => throw new InvalidOperationException(), |
| | 0 | 107 | | _ => x => x.AddHours(step) |
| | 18 | 108 | | }; |
| | | 109 | | |
| | 15 | 110 | | if (step > 0) |
| | | 111 | | { |
| | 114 | 112 | | for (var i = time; i <= max; i = increment(i)) |
| | 45 | 113 | | yield return i; |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | 24 | 117 | | for (var i = time; i >= max; i = increment(i)) |
| | 9 | 118 | | yield return i; |
| | | 119 | | } |
| | 15 | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |