< Summary - Kestrun — Combined Coverage

Information
Class: Private.ConvertTo.ConvertTo-DateTimeOffset
Assembly: Kestrun.PowerShell.Private
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/ConvertTo/ConvertTo-DateTimeOffset.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 15
Coverable lines: 15
Total lines: 80
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/15) Total lines: 81 Tag: Kestrun/Kestrun@b7a12f5205442de0d384c1a861c1f8f30c74dcb509/16/2025 - 04:01:29 Line coverage: 0% (0/15) Total lines: 80 Tag: Kestrun/Kestrun@e5263347b0baba68d9fd62ffbf60a7dd87f994bb

Metrics

File(s)

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

#LineLine coverage
 1<#
 2.SYNOPSIS
 3    Converts various input types to [DateTimeOffset].
 4.DESCRIPTION
 5    Accepts input as:
 6        - [DateTimeOffset] instances
 7        - [DateTime] instances (converted to local DateTimeOffset)
 8        - Strings (parsed into DateTimeOffset or interpreted as duration from now)
 9        - [TimeSpan] instances (added to current time)
 10        - Numeric values (interpreted as seconds from now)
 11.PARAMETER InputObject
 12    The input value to convert to a DateTimeOffset.
 13.EXAMPLE
 14    # From DateTimeOffset
 15    $dto = [DateTimeOffset]::Now
 16    ConvertTo-DateTimeOffset -InputObject $dto
 17
 18.EXAMPLE
 19    # From DateTime
 20    $dt = [DateTime]::Now
 21    ConvertTo-DateTimeOffset -InputObject $dt
 22
 23.EXAMPLE
 24    # From string
 25    ConvertTo-DateTimeOffset -InputObject "2025-09-10T23:00Z"
 26
 27.EXAMPLE
 28    # From TimeSpan
 29    $ts = [TimeSpan]::FromHours(1)
 30    ConvertTo-DateTimeOffset -InputObject $ts
 31
 32.EXAMPLE
 33    # From numeric seconds
 34    ConvertTo-DateTimeOffset -InputObject 3600
 35.OUTPUTS
 36    System.DateTimeOffset
 37#>
 38function ConvertTo-DateTimeOffset {
 39    [CmdletBinding()]
 40    [OutputType('System.DateTimeOffset')]
 41    param(
 42        [Parameter(Mandatory)]
 43        [object]$InputObject
 44    )
 45
 46    # Already DateTimeOffset
 047    if ($InputObject -is [DateTimeOffset]) { return $InputObject }
 48
 49    # DateTime -> DateTimeOffset (local)
 050    if ($InputObject -is [DateTime]) {
 051        return [DateTimeOffset]::new([DateTime]$InputObject)
 52    }
 53
 54    # String → try absolute date/time first
 055    if ($InputObject -is [string]) {
 056        $s = $InputObject.Trim()
 057        $dto = $null
 058        if ([DateTimeOffset]::TryParse($s, [ref]$dto)) { return $dto }
 59
 60        # If not a date, try treating the string as a duration and add to now
 61        try {
 062            $ts = ConvertTo-TimeSpan -InputObject $s
 063            return [DateTimeOffset]::Now.Add($ts)
 64        } catch {
 065            throw "Invalid Expires value '$s'. Provide an absolute date (e.g. '2025-09-10T23:00Z') or a duration (e.g. '
 66        }
 67    }
 68
 69    # TimeSpan => relative expiry from now
 070    if ($InputObject -is [TimeSpan]) {
 071        return [DateTimeOffset]::Now.Add([TimeSpan]$InputObject)
 72    }
 73
 74    # Numeric seconds => relative expiry
 075    if ($InputObject -is [int] -or $InputObject -is [long] -or $InputObject -is [double] -or $InputObject -is [decimal])
 076        return [DateTimeOffset]::Now.AddSeconds([double]$InputObject)
 77    }
 78
 079    throw "Cannot convert value of type [$($InputObject.GetType().FullName)] to [DateTimeOffset]."
 80}

Methods/Properties

ConvertTo-DateTimeOffset()