< Summary - Kestrun — Combined Coverage

Information
Class: Private.ConvertTo.ConvertTo-TimeSpan
Assembly: Kestrun.PowerShell.Private
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/ConvertTo/ConvertTo-TimeSpan.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 69
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 09/11/2025 - 03:23:38 Line coverage: 0% (0/16) Total lines: 69 Tag: Kestrun/Kestrun@b7a12f5205442de0d384c1a861c1f8f30c74dcb5

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/ConvertTo/ConvertTo-TimeSpan.ps1

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Converts various input types to a [TimeSpan] instance.
 4.DESCRIPTION
 5    Accepts input as:
 6        - [TimeSpan] instances
 7        - Numeric values (interpreted as seconds)
 8        - Strings (parsed into TimeSpan)
 9    The string parser supports standard .NET TimeSpan formats (c, g, G) as well as compact
 10    token formats like "1d2h30m15s250ms" (order-insensitive, any subset).
 11.PARAMETER InputObject
 12    The input value to convert to a TimeSpan.
 13.EXAMPLE
 14    # From TimeSpan
 15    $ts = [TimeSpan]::FromHours(1.5)
 16    ConvertTo-TimeSpan -InputObject $ts
 17
 18.EXAMPLE
 19    # From numeric seconds
 20    ConvertTo-TimeSpan -InputObject 90
 21
 22.EXAMPLE
 23    # From string
 24    ConvertTo-TimeSpan -InputObject "1d2h30m15s250ms"
 25.OUTPUTS
 26    System.TimeSpan
 27#>
 28function ConvertTo-TimeSpan {
 29    [CmdletBinding()]
 30    [OutputType('System.TimeSpan')]
 31    param(
 32        [Parameter(Mandatory)]
 33        [object]$InputObject
 34    )
 35
 36    # 1) Already a TimeSpan
 037    if ($InputObject -is [TimeSpan]) { return $InputObject }
 38
 39    # 2) Numeric => seconds
 040    if ($InputObject -is [int] -or
 41        $InputObject -is [long] -or
 42        $InputObject -is [double] -or
 43        $InputObject -is [decimal]) {
 044        return [TimeSpan]::FromSeconds([double]$InputObject)
 45    }
 46
 47    # 3) String parsing
 048    if ($InputObject -is [string]) {
 049        $s = $InputObject.Trim()
 50
 51        # Try .NET built-in formats first: "c", "g", "G" (e.g., "00:30:00", "1.02:03:04")
 052        $ts = [TimeSpan]::Zero
 053        if ([TimeSpan]::TryParse($s, [ref]$ts)) { return $ts }
 54
 55        # Compact tokens: 1d2h30m15s250ms (order-insensitive, any subset)
 056        if ($s -match '^(?i)(?:\s*(?<d>\d+)\s*d)?(?:\s*(?<h>\d+)\s*h)?(?:\s*(?<m>\d+)\s*m)?(?:\s*(?<s>\d+)\s*s)?(?:\s*(?
 057            $days = [int]::Parse(('0' + $Matches['d']))
 058            $hrs = [int]::Parse(('0' + $Matches['h']))
 059            $min = [int]::Parse(('0' + $Matches['m']))
 060            $sec = [int]::Parse(('0' + $Matches['s']))
 061            $msec = [int]::Parse(('0' + $Matches['ms']))
 062            return [TimeSpan]::new($days, $hrs, $min, $sec, $msec)
 63        }
 64
 065        throw "Invalid TimeSpan format: '$s'. Try '00:30:00', '1.02:03:04', or tokens like '1d2h30m15s'."
 66    }
 67
 068    throw "Cannot convert value of type [$($InputObject.GetType().FullName)] to [TimeSpan]."
 69}

Methods/Properties

ConvertTo-TimeSpan()