< Summary

Information
Class: MyNet.Temporal.TimeOnlyExtensions
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Extensions/TimeOnlyExtensions.cs
Tag: 342_31701152804
Line coverage
87%
Covered lines: 27
Uncovered lines: 4
Coverable lines: 31
Total lines: 124
Line coverage: 87%
Branch coverage
66%
Covered branches: 14
Total branches: 21
Branch coverage: 66.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BeginningOfHour(...)100%11100%
EndOfHour(...)100%11100%
BeginningOfMinute(...)100%11100%
EndOfMinute(...)100%11100%
SetHour(...)100%11100%
SetMinute(...)100%11100%
SetSecond(...)100%11100%
SetMillisecond(...)100%11100%
SameMilliSecond(...)50%22100%
SameSecond(...)50%22100%
SameMinute(...)50%22100%
SameHour(...)100%11100%
Range()73.33%171578.94%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Extensions/TimeOnlyExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TimeOnlyExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using MyNet.Primitives;
 10
 11#pragma warning disable IDE0130 // Namespace does not match folder structure
 12namespace MyNet.Temporal;
 13#pragma warning restore IDE0130 // Namespace does not match folder structure
 14
 15public 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>
 322        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>
 327        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>
 332        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>
 337        public TimeOnly EndOfMinute() => new(time.Hour, time.Minute, 59, 999);
 38
 39        /// <summary>
 40        /// Returns <see cref="DateTime"/> with changed Hour part.
 41        /// </summary>
 342        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>
 347        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>
 352        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>
 357        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
 664        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
 1271        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, 
 1878        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>
 2485        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        {
 2196            ArgumentOutOfRangeException.ThrowIfZero(step);
 97
 1898            Func<TimeOnly, TimeOnly> increment = unit switch
 1899            {
 3100                TimeUnit.Millisecond => x => x.Add(step.Milliseconds()),
 3101                TimeUnit.Second => x => x.Add(step.Seconds()),
 3102                TimeUnit.Minute => x => x.AddMinutes(step),
 6103                TimeUnit.Hour => x => x.AddHours(step),
 3104                TimeUnit.Day => throw new InvalidOperationException(),
 0105                TimeUnit.Week => throw new InvalidOperationException(),
 0106                TimeUnit.Month => throw new InvalidOperationException(),
 0107                TimeUnit.Year => throw new InvalidOperationException(),
 0108                _ => x => x.AddHours(step)
 18109            };
 110
 15111            if (step > 0)
 112            {
 114113                for (var i = time; i <= max; i = increment(i))
 45114                    yield return i;
 115            }
 116            else
 117            {
 24118                for (var i = time; i >= max; i = increment(i))
 9119                    yield return i;
 120            }
 15121        }
 122    }
 123}
 124