< Summary

Information
Class: MyNet.Primitives.TimeOnlyExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Temporal/Extensions/TimeOnlyExtensions.cs
Tag: 323_28699572109
Line coverage
87%
Covered lines: 27
Uncovered lines: 4
Coverable lines: 31
Total lines: 123
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/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/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;
 9
 10#pragma warning disable IDE0130 // Namespace does not match folder structure
 11namespace MyNet.Primitives;
 12#pragma warning restore IDE0130 // Namespace does not match folder structure
 13
 14public 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>
 321        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>
 326        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>
 331        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>
 336        public TimeOnly EndOfMinute() => new(time.Hour, time.Minute, 59, 999);
 37
 38        /// <summary>
 39        /// Returns <see cref="DateTime"/> with changed Hour part.
 40        /// </summary>
 341        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>
 346        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>
 351        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>
 356        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
 663        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
 1270        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, 
 1877        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>
 2484        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        {
 2195            ArgumentOutOfRangeException.ThrowIfZero(step);
 96
 1897            Func<TimeOnly, TimeOnly> increment = unit switch
 1898            {
 399                TimeUnit.Millisecond => x => x.Add(step.Milliseconds()),
 3100                TimeUnit.Second => x => x.Add(step.Seconds()),
 3101                TimeUnit.Minute => x => x.AddMinutes(step),
 6102                TimeUnit.Hour => x => x.AddHours(step),
 3103                TimeUnit.Day => throw new InvalidOperationException(),
 0104                TimeUnit.Week => throw new InvalidOperationException(),
 0105                TimeUnit.Month => throw new InvalidOperationException(),
 0106                TimeUnit.Year => throw new InvalidOperationException(),
 0107                _ => x => x.AddHours(step)
 18108            };
 109
 15110            if (step > 0)
 111            {
 114112                for (var i = time; i <= max; i = increment(i))
 45113                    yield return i;
 114            }
 115            else
 116            {
 24117                for (var i = time; i >= max; i = increment(i))
 9118                    yield return i;
 119            }
 15120        }
 121    }
 122}
 123