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