< Summary

Information
Class: MyNet.Primitives.Conversion.TimeConverter
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/TimeConverter.cs
Tag: 323_28699572109
Line coverage
73%
Covered lines: 11
Uncovered lines: 4
Coverable lines: 15
Total lines: 38
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/85372080fe102cd9ee155ceab49ae000e7f66103/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;
 8using MyNet.Primitives.Temporal;
 9
 10namespace MyNet.Primitives.Conversion;
 11
 12/// <summary>
 13/// Provides conversion between time units using shared calendar constants for week, month and year.
 14/// </summary>
 15public sealed class TimeConverter : IUnitConverter<TimeUnit>
 16{
 17    public double Convert(double value, TimeUnit from, TimeUnit to)
 18    {
 1819        var fromFactor = GetMillisecondsFactor(from);
 1820        var toFactor = GetMillisecondsFactor(to);
 21
 1822        return value * fromFactor / toFactor;
 23    }
 24
 3625    private static double GetMillisecondsFactor(TimeUnit unit) => unit switch
 3626    {
 327        TimeUnit.Millisecond => 1d,
 928        TimeUnit.Second => 1000d,
 1229        TimeUnit.Minute => 60d * 1000d,
 930        TimeUnit.Hour => 60d * 60d * 1000d,
 331        TimeUnit.Day => 24d * 60d * 60d * 1000d,
 032        TimeUnit.Week => DateTimeHelper.DaysPerWeek * 24d * 60d * 60d * 1000d,
 033        TimeUnit.Month => DateTimeHelper.DaysPerMonth * 24d * 60d * 60d * 1000d,
 034        TimeUnit.Year => DateTimeHelper.DaysPerYear * 24d * 60d * 60d * 1000d,
 035        _ => throw new ArgumentOutOfRangeException(nameof(unit), unit, "Unsupported time unit.")
 3636    };
 37}
 38