< Summary

Information
Class: MyNet.Temporal.NumberToTimeSpanExtensions
Assembly: MyNet.Temporal
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Temporal/Extensions/NumberToTimeSpanExtensions.cs
Tag: 342_31701152804
Line coverage
60%
Covered lines: 31
Uncovered lines: 20
Coverable lines: 51
Total lines: 184
Line coverage: 60.7%
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%11100%
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/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.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;
 9using MyNet.Primitives.Intervals;
 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 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 a <see cref="CalendarAmount"/> for the given number of years.
 25        /// </summary>
 7526        public CalendarAmount Years() => new() { Years = value };
 27
 28        /// <summary>
 29        /// Generates a <see cref="CalendarAmount"/> for the given number of quarters.
 30        /// </summary>
 2731        public CalendarAmount Quarters() => new() { Months = value * 3 };
 32
 33        /// <summary>
 34        /// Returns a <see cref="CalendarAmount"/> for the given number of months.
 35        /// </summary>
 7236        public CalendarAmount Months() => new() { Months = value };
 37
 38        /// <summary>
 39        /// Returns a fixed <see cref="TimeSpan"/> for the given number of weeks (weeks * 7 days).
 40        /// </summary>
 6941        public TimeSpan Weeks() => TimeSpan.FromDays(value * DateTimeHelper.DaysPerWeek);
 42
 43        /// <summary>
 44        /// Returns a fixed <see cref="TimeSpan"/> for the given number of days.
 45        /// </summary>
 21346        public TimeSpan Days() => TimeSpan.FromDays(value);
 47
 48        /// <summary>
 49        /// Returns a fixed <see cref="TimeSpan"/> for the given number of hours.
 50        /// </summary>
 6651        public TimeSpan Hours() => TimeSpan.FromHours(value);
 52
 53        /// <summary>
 54        /// Returns a fixed <see cref="TimeSpan"/> for the given number of minutes.
 55        /// </summary>
 6956        public TimeSpan Minutes() => TimeSpan.FromMinutes(value);
 57
 58        /// <summary>
 59        /// Returns a fixed <see cref="TimeSpan"/> for the given number of seconds.
 60        /// </summary>
 26161        public TimeSpan Seconds() => TimeSpan.FromSeconds(value);
 62
 63        /// <summary>
 64        /// Returns a fixed <see cref="TimeSpan"/> for the given number of milliseconds.
 65        /// </summary>
 7266        public TimeSpan Milliseconds() => TimeSpan.FromMilliseconds(value);
 67
 68        /// <summary>
 69        /// Returns a fixed <see cref="TimeSpan"/> for the given number of ticks.
 70        /// </summary>
 6071        public TimeSpan Ticks() => TimeSpan.FromTicks(value);
 72
 73        /// <summary>
 74        /// Returns a <see cref="CalendarAmount"/> for calendar units (month/year) or a fixed-duration amount for other 
 75        /// </summary>
 076        public CalendarAmount Unit(TimeUnit unit) => unit switch
 077        {
 078            TimeUnit.Millisecond => TimeSpan.FromMilliseconds(value),
 079            TimeUnit.Second => TimeSpan.FromSeconds(value),
 080            TimeUnit.Minute => TimeSpan.FromMinutes(value),
 081            TimeUnit.Hour => TimeSpan.FromHours(value),
 082            TimeUnit.Day => TimeSpan.FromDays(value),
 083            TimeUnit.Week => TimeSpan.FromDays(value * DateTimeHelper.DaysPerWeek),
 084            TimeUnit.Month => new CalendarAmount { Months = value },
 085            TimeUnit.Year => new CalendarAmount { Years = value },
 086            _ => default
 087        };
 88
 89        /// <summary>
 90        /// Converts an integer value to a fixed <see cref="TimeSpan"/>. Month/Year use approximate average lengths
 91        /// (<see cref="DateTimeHelper.DaysPerMonth"/> / <see cref="DateTimeHelper.DaysPerYear"/>).
 92        /// Prefer <see cref="Months"/> / <see cref="Years"/> with <c>AddCalendarAmount</c> for calendar arithmetic.
 93        /// </summary>
 27394        public TimeSpan ToTimeSpan(TimeUnit timeUnitToGet) => timeUnitToGet switch
 27395        {
 096            TimeUnit.Millisecond => new TimeSpan(0, 0, 0, 0, value),
 4597            TimeUnit.Second => new TimeSpan(0, 0, 0, value, 0),
 6398            TimeUnit.Minute => new TimeSpan(0, 0, value, 0, 0),
 4299            TimeUnit.Hour => new TimeSpan(0, value, 0, 0, 0),
 48100            TimeUnit.Day => new TimeSpan(value, 0, 0, 0, 0),
 0101            TimeUnit.Week => new TimeSpan(value * DateTimeHelper.DaysPerWeek, 0, 0, 0, 0),
 42102            TimeUnit.Month => new TimeSpan((int)Math.Round(value * DateTimeHelper.DaysPerMonth), 0, 0, 0, 0),
 33103            TimeUnit.Year => new TimeSpan((int)Math.Round(value * DateTimeHelper.DaysPerYear), 0, 0, 0, 0),
 0104            _ => TimeSpan.Zero
 273105        };
 106
 107        /// <summary>
 108        /// Returns the first year of the decade containing the given year.
 109        /// </summary>
 15110        public int DecadeStart() => GetDecadeStart(value);
 111
 112        /// <summary>
 113        /// Returns the last year of the decade containing the given year.
 114        /// </summary>
 12115        public int DecadeEnd() => GetDecadeStart(value) + 9;
 116
 117        /// <summary>
 118        /// Returns a closed interval covering the decade of the given year.
 119        /// </summary>
 120        public ClosedInterval<int> Decade()
 121        {
 6122            var start = GetDecadeStart(value);
 6123            return new ClosedInterval<int>(start, start + 9);
 124        }
 125
 126        /// <summary>
 127        /// Returns the first year of the century containing the given year.
 128        /// </summary>
 12129        public int CenturyStart() => GetCenturyStart(value);
 130
 131        /// <summary>
 132        /// Returns the last year of the century containing the given year.
 133        /// </summary>
 9134        public int CenturyEnd() => GetCenturyStart(value) + 99;
 135
 136        /// <summary>
 137        /// Returns a closed interval covering the century of the given year.
 138        /// </summary>
 139        public ClosedInterval<int> Century()
 140        {
 6141            var start = GetCenturyStart(value);
 6142            return new ClosedInterval<int>(start, start + 99);
 143        }
 144    }
 145
 146    extension(double value)
 147    {
 148        /// <summary>
 149        /// Returns a fixed <see cref="TimeSpan"/> for the given number of weeks (fractional weeks allowed).
 150        /// </summary>
 3151        public TimeSpan Weeks() => TimeSpan.FromDays(value * DateTimeHelper.DaysPerWeek);
 152
 153        /// <summary>
 154        /// Returns a fixed <see cref="TimeSpan"/> for the given number of days.
 155        /// </summary>
 12156        public TimeSpan Days() => TimeSpan.FromDays(value);
 157
 158        /// <summary>
 159        /// Returns a fixed <see cref="TimeSpan"/> for the given number of hours.
 160        /// </summary>
 0161        public TimeSpan Hours() => TimeSpan.FromHours(value);
 162
 163        /// <summary>
 164        /// Returns a fixed <see cref="TimeSpan"/> for the given number of minutes.
 165        /// </summary>
 0166        public TimeSpan Minutes() => TimeSpan.FromMinutes(value);
 167
 168        /// <summary>
 169        /// Returns a fixed <see cref="TimeSpan"/> for the given number of seconds.
 170        /// </summary>
 0171        public TimeSpan Seconds() => TimeSpan.FromSeconds(value);
 172
 173        /// <summary>
 174        /// Returns a fixed <see cref="TimeSpan"/> for the given number of milliseconds.
 175        /// </summary>
 0176        public TimeSpan Milliseconds() => TimeSpan.FromMilliseconds(value);
 177    }
 178
 179    /// <summary>
 180    /// Returns a fixed <see cref="TimeSpan"/> for the given number of ticks.
 181    /// </summary>
 0182    public static TimeSpan Ticks(this long ticks) => TimeSpan.FromTicks(ticks);
 183}
 184