< Summary

Information
Class: MyNet.Primitives.TimeSpanExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Temporal/Extensions/TimeSpanExtensions.cs
Tag: 323_28699572109
Line coverage
41%
Covered lines: 24
Uncovered lines: 34
Coverable lines: 58
Total lines: 236
Line coverage: 41.3%
Branch coverage
41%
Covered branches: 14
Total branches: 34
Branch coverage: 41.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ToTime(...)100%11100%
AddFluentTimeSpan(...)100%11100%
SubtractFluentTimeSpan(...)100%11100%
Ago(...)100%210%
Ago(...)100%210%
Since(...)100%11100%
FromNow(...)100%210%
From(...)100%11100%
Before(...)100%11100%
Round(...)46.15%771327.77%
Add(...)100%210%
ConvertTo(...)0%9090%
CoerceSameDayRange(...)50%44100%
ToPeriod(...)75%8887.5%
ToTime(...)100%210%
Ago(...)100%210%
Ago(...)100%11100%
Before(...)100%11100%
FromNow(...)100%210%
From(...)100%11100%
Since(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Temporal/Extensions/TimeSpanExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TimeSpanExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using MyNet.Primitives.Intervals;
 9using MyNet.Primitives.Temporal;
 10
 11#pragma warning disable IDE0130 // Namespace does not match folder structure
 12namespace MyNet.Primitives;
 13#pragma warning restore IDE0130 // Namespace does not match folder structure
 14
 15public 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>
 323        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>
 628        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>
 333        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>
 038        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>
 043        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>
 352        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>
 057        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>
 662        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>
 367        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                    {
 083                        rounded = new(timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
 084                        if (timeSpan.Milliseconds >= 500)
 85                        {
 086                            rounded += 1.Seconds();
 87                        }
 88
 089                        break;
 90                    }
 91
 92                case RoundTo.Minute:
 93                    {
 394                        rounded = new(timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, 0);
 395                        if (timeSpan.Seconds >= 30)
 96                        {
 397                            rounded += 1.Minutes();
 98                        }
 99
 3100                        break;
 101                    }
 102
 103                case RoundTo.Hour:
 104                    {
 0105                        rounded = new(timeSpan.Days, timeSpan.Hours, 0, 0);
 0106                        if (timeSpan.Minutes >= 30)
 107                        {
 0108                            rounded += 1.Hours();
 109                        }
 110
 0111                        break;
 112                    }
 113
 114                case RoundTo.Day:
 115                    {
 0116                        rounded = new(timeSpan.Days, 0, 0, 0);
 0117                        if (timeSpan.Hours >= 12)
 118                        {
 0119                            rounded += 1.Days();
 120                        }
 121
 0122                        break;
 123                    }
 124
 125                default:
 126                    {
 0127                        throw new ArgumentException(null, nameof(rt));
 128                    }
 129            }
 130
 3131            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>
 0140        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        {
 0149            var abs = timeSpan.Duration();
 150
 0151            return unit switch
 0152            {
 0153                TimeUnit.Millisecond => Math.Round(abs.TotalMilliseconds),
 0154                TimeUnit.Second => Math.Round(abs.TotalSeconds),
 0155                TimeUnit.Minute => Math.Round(abs.TotalMinutes),
 0156                TimeUnit.Hour => Math.Round(abs.TotalHours),
 0157                TimeUnit.Day => Math.Round(abs.TotalDays),
 0158                TimeUnit.Week => Math.Round(abs.TotalDays / DateTimeHelper.DaysPerWeek),
 0159                TimeUnit.Month => Math.Round(abs.TotalDays / DateTimeHelper.DaysPerMonth),
 0160                TimeUnit.Year => Math.Round(abs.TotalDays / DateTimeHelper.DaysPerYear),
 0161                _ => 0
 0162            };
 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) =>
 3171            end >= timeSpan ? (timeSpan, end)
 3172                : 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        {
 6179            referenceDate = referenceDate.DiscardTime();
 180
 6181            var startDateTime = referenceDate.Add(timeSpan);
 6182            var endDateTime = allowOvernight && end <= timeSpan
 6183                ? referenceDate.AddDays(1).Add(end)
 6184                : referenceDate.Add(end);
 185
 6186            if (!allowOvernight && end == timeSpan)
 0187                endDateTime = startDateTime.AddTicks(1);
 188
 6189            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
 0199        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>
 0204        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>
 6209        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>
 228214        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>
 0219        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>
 204224        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>
 3233        public DateTime Since(DateTime originalValue) => fluentTimeSpan.From(originalValue);
 234    }
 235}
 236