< Summary

Information
Class: MyNet.Primitives.NumberToTimeSpanExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Temporal/Extensions/NumberToTimeSpanExtensions.cs
Tag: 323_28699572109
Line coverage
58%
Covered lines: 30
Uncovered lines: 21
Coverable lines: 51
Total lines: 192
Line coverage: 58.8%
Branch coverage
33%
Covered branches: 6
Total branches: 18
Branch coverage: 33.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDecadeStart(...)100%11100%
GetCenturyStart(...)100%11100%
Years(...)100%11100%
Quarters(...)100%11100%
Months(...)100%11100%
Weeks(...)100%11100%
Days(...)100%11100%
Hours(...)100%11100%
Minutes(...)100%11100%
Seconds(...)100%11100%
Milliseconds(...)100%11100%
Ticks(...)100%11100%
Unit(...)0%9090%
ToTimeSpan(...)66.66%10975%
DecadeStart(...)100%11100%
DecadeEnd(...)100%11100%
Decade(...)100%11100%
CenturyStart(...)100%11100%
CenturyEnd(...)100%11100%
Century(...)100%11100%
Weeks(...)100%210%
Days(...)100%11100%
Hours(...)100%210%
Minutes(...)100%210%
Seconds(...)100%210%
Milliseconds(...)100%210%
Ticks(...)100%210%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NumberToTimeSpanExtensions.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 NumberToTimeSpanExtensions
 16{
 3317    private static int GetDecadeStart(int year) => year / 10 * 10;
 18
 2719    private static int GetCenturyStart(int year) => year / 100 * 100;
 20
 21    extension(int value)
 22    {
 23        /// <summary>
 24        /// Generates <see cref="TimeSpan"/> value for given number of Years.
 25        /// </summary>
 7826        public FluentTimeSpan Years() => new() { Years = value };
 27
 28        /// <summary>
 29        /// Generates <see cref="TimeSpan"/> value for given number of Quarters.
 30        /// </summary>
 1231        public FluentTimeSpan Quarters() => new() { Months = value * 3 };
 32
 33        /// <summary>
 34        /// Returns <see cref="TimeSpan"/> value for given number of Months.
 35        /// </summary>
 7836        public FluentTimeSpan Months() => new() { Months = value };
 37
 38        /// <summary>
 39        /// Returns <see cref="TimeSpan"/> for given number of Weeks (number of weeks * 7).
 40        /// </summary>
 7541        public FluentTimeSpan Weeks() => new() { TimeSpan = TimeSpan.FromDays(value * DateTimeHelper.DaysPerWeek) };
 42
 43        /// <summary>
 44        /// Returns <see cref="TimeSpan"/> for given number of Days.
 45        /// </summary>
 28846        public FluentTimeSpan Days() => new() { TimeSpan = TimeSpan.FromDays(value) };
 47
 48        /// <summary>
 49        /// Returns <see cref="TimeSpan"/> for given number of Hours.
 50        /// </summary>
 7851        public FluentTimeSpan Hours() => new() { TimeSpan = TimeSpan.FromHours(value) };
 52
 53        /// <summary>
 54        /// Returns <see cref="TimeSpan"/> for given number of Minutes.
 55        /// </summary>
 8156        public FluentTimeSpan Minutes() => new() { TimeSpan = TimeSpan.FromMinutes(value) };
 57
 58        /// <summary>
 59        /// Returns <see cref="TimeSpan"/> for given number of Seconds.
 60        /// </summary>
 27361        public FluentTimeSpan Seconds() => new() { TimeSpan = TimeSpan.FromSeconds(value) };
 62
 63        /// <summary>
 64        /// Returns <see cref="TimeSpan"/> for given number of Milliseconds.
 65        /// </summary>
 9066        public FluentTimeSpan Milliseconds() => new() { TimeSpan = TimeSpan.FromMilliseconds(value) };
 67
 68        /// <summary>
 69        /// Returns <see cref="TimeSpan"/> for given number of ticks.
 70        /// </summary>
 6671        public FluentTimeSpan Ticks() => new() { TimeSpan = TimeSpan.FromTicks(value) };
 72
 73        /// <summary>
 74        /// Returns <see cref="TimeSpan"/> for given number of units.
 75        /// </summary>
 76        /// <param name="unit">The unit of time.</param>
 77        /// <returns>A <see cref="FluentTimeSpan"/> representing the specified number of units.</returns>
 078        public FluentTimeSpan Unit(TimeUnit unit) => unit switch
 079        {
 080            TimeUnit.Millisecond => new() { TimeSpan = TimeSpan.FromMilliseconds(value) },
 081            TimeUnit.Second => new() { TimeSpan = TimeSpan.FromSeconds(value) },
 082            TimeUnit.Minute => new() { TimeSpan = TimeSpan.FromMinutes(value) },
 083            TimeUnit.Hour => new() { TimeSpan = TimeSpan.FromHours(value) },
 084            TimeUnit.Day => new() { TimeSpan = TimeSpan.FromDays(value) },
 085            TimeUnit.Week => new() { TimeSpan = TimeSpan.FromDays(value * DateTimeHelper.DaysPerWeek) },
 086            TimeUnit.Month => new() { Months = value },
 087            TimeUnit.Year => new() { Years = value },
 088            _ => default
 089        };
 90
 91        /// <summary>
 92        /// Converts an integer value to a <see cref="TimeSpan"/> based on the specified time unit. For example, if you 
 93        /// </summary>
 94        /// <param name="timeUnitToGet">The time unit to convert the integer value to.</param>
 95        /// <returns>A <see cref="TimeSpan"/> representing the specified value in the specified time unit.</returns>
 27396        public TimeSpan ToTimeSpan(TimeUnit timeUnitToGet) => timeUnitToGet switch
 27397        {
 098            TimeUnit.Millisecond => new(0, 0, 0, 0, value),
 4599            TimeUnit.Second => new(0, 0, 0, value, 0),
 63100            TimeUnit.Minute => new(0, 0, value, 0, 0),
 42101            TimeUnit.Hour => new(0, value, 0, 0, 0),
 48102            TimeUnit.Day => new(value, 0, 0, 0, 0),
 0103            TimeUnit.Week => new(value * DateTimeHelper.DaysPerWeek, 0, 0, 0, 0),
 42104            TimeUnit.Month => new((int)Math.Round(value * DateTimeHelper.DaysPerMonth), 0, 0, 0, 0),
 33105            TimeUnit.Year => new((int)Math.Round(value * DateTimeHelper.DaysPerYear), 0, 0, 0, 0),
 0106            _ => TimeSpan.Zero
 273107        };
 108
 109        /// <summary>
 110        /// Returns the first year of the decade containing the given year. For example, if the input year is 1995, this
 111        /// </summary>
 112        /// <returns>The first year of the decade containing the given year.</returns>
 15113        public int DecadeStart() => GetDecadeStart(value);
 114
 115        /// <summary>
 116        /// Returns the last year of the decade containing the given year. For example, if the input year is 1995, this 
 117        /// </summary>
 118        /// <returns>The last year of the decade containing the given year.</returns>
 12119        public int DecadeEnd() => GetDecadeStart(value) + 9;
 120
 121        /// <summary>
 122        /// Returns a ClosedInterval representing the decade of the given year. The start of the interval is calculated 
 123        /// </summary>
 124        /// <returns>A ClosedInterval representing the decade of the given year.</returns>
 125        public ClosedInterval<int> Decade()
 126        {
 6127            var start = GetDecadeStart(value);
 6128            return new(start, start + 9);
 129        }
 130
 131        /// <summary>
 132        /// Returns the first year of the century containing the given year. For example, if the input year is 1995, thi
 133        /// </summary>
 134        /// <returns>The first year of the century containing the given year.</returns>
 12135        public int CenturyStart() => GetCenturyStart(value);
 136
 137        /// <summary>
 138        /// Returns the last year of the century containing the given year. For example, if the input year is 1995, this
 139        /// </summary>
 140        /// <returns>The last year of the century containing the given year.</returns>
 9141        public int CenturyEnd() => GetCenturyStart(value) + 99;
 142
 143        /// <summary>
 144        /// Returns a ClosedInterval representing the century of the given year. The start of the interval is calculated
 145        /// </summary>
 146        /// <returns>A ClosedInterval representing the century of the given year.</returns>
 147        public ClosedInterval<int> Century()
 148        {
 6149            var start = GetCenturyStart(value);
 6150            return new(start, start + 99);
 151        }
 152    }
 153
 154    extension(double value)
 155    {
 156        /// <summary>
 157        /// Returns <see cref="TimeSpan"/> for given number of Weeks (number of weeks * 7).
 158        /// </summary>
 0159        public FluentTimeSpan Weeks() => new() { TimeSpan = TimeSpan.FromDays((int)(value * DateTimeHelper.DaysPerWeek))
 160
 161        /// <summary>
 162        /// Returns <see cref="TimeSpan"/> for given number of Days.
 163        /// </summary>
 18164        public FluentTimeSpan Days() => new() { TimeSpan = TimeSpan.FromDays(value) };
 165
 166        /// <summary>
 167        /// Returns <see cref="TimeSpan"/> for given number of Hours.
 168        /// </summary>
 0169        public FluentTimeSpan Hours() => new() { TimeSpan = TimeSpan.FromHours(value) };
 170
 171        /// <summary>
 172        /// Returns <see cref="TimeSpan"/> for given number of Minutes.
 173        /// </summary>
 0174        public FluentTimeSpan Minutes() => new() { TimeSpan = TimeSpan.FromMinutes(value) };
 175
 176        /// <summary>
 177        /// Returns <see cref="TimeSpan"/> for given number of Seconds.
 178        /// </summary>
 0179        public FluentTimeSpan Seconds() => new() { TimeSpan = TimeSpan.FromSeconds(value) };
 180
 181        /// <summary>
 182        /// Returns <see cref="TimeSpan"/> for given number of Milliseconds.
 183        /// </summary>
 0184        public FluentTimeSpan Milliseconds() => new() { TimeSpan = TimeSpan.FromMilliseconds(value) };
 185    }
 186
 187    /// <summary>
 188    /// Returns <see cref="TimeSpan"/> for given number of ticks.
 189    /// </summary>
 0190    public static FluentTimeSpan Ticks(this long ticks) => new() { TimeSpan = TimeSpan.FromTicks(ticks) };
 191}
 192