< Summary

Information
Class: MyNet.Primitives.Conversion.TimeConverter
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Primitives/Conversion/TimeConverter.cs
Tag: 342_31701152804
Line coverage
73%
Covered lines: 11
Uncovered lines: 4
Coverable lines: 15
Total lines: 37
Line coverage: 73.3%
Branch coverage
55%
Covered branches: 5
Total branches: 9
Branch coverage: 55.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Convert(...)100%11100%
GetMillisecondsFactor(...)55.55%12966.66%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Primitives/Conversion/TimeConverter.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TimeConverter.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9namespace MyNet.Primitives.Conversion;
 10
 11/// <summary>
 12/// Provides conversion between time units using shared calendar constants for week, month and year.
 13/// </summary>
 14public sealed class TimeConverter : IUnitConverter<TimeUnit>
 15{
 16    public double Convert(double value, TimeUnit from, TimeUnit to)
 17    {
 1818        var fromFactor = GetMillisecondsFactor(from);
 1819        var toFactor = GetMillisecondsFactor(to);
 20
 1821        return value * fromFactor / toFactor;
 22    }
 23
 3624    private static double GetMillisecondsFactor(TimeUnit unit) => unit switch
 3625    {
 326        TimeUnit.Millisecond => 1d,
 927        TimeUnit.Second => 1000d,
 1228        TimeUnit.Minute => 60d * 1000d,
 929        TimeUnit.Hour => 60d * 60d * 1000d,
 330        TimeUnit.Day => 24d * 60d * 60d * 1000d,
 031        TimeUnit.Week => 7 * 24d * 60d * 60d * 1000d, // DaysPerWeek
 032        TimeUnit.Month => (365.2425 / 12) * 24d * 60d * 60d * 1000d, // Gregorian avg days/month (DateTimeHelper.DaysPer
 033        TimeUnit.Year => 365.2425 * 24d * 60d * 60d * 1000d, // Gregorian avg days/year (DateTimeHelper.DaysPerYear)
 034        _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, "Unsupported time unit.")
 3635    };
 36}
 37