| | | 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 | | #> |
| | | 28 | | function ConvertTo-TimeSpan { |
| | | 29 | | [CmdletBinding()] |
| | | 30 | | [OutputType('System.TimeSpan')] |
| | | 31 | | param( |
| | | 32 | | [Parameter(Mandatory)] |
| | | 33 | | [object]$InputObject |
| | | 34 | | ) |
| | | 35 | | |
| | | 36 | | # 1) Already a TimeSpan |
| | 0 | 37 | | if ($InputObject -is [TimeSpan]) { return $InputObject } |
| | | 38 | | |
| | | 39 | | # 2) Numeric => seconds |
| | 0 | 40 | | if ($InputObject -is [int] -or |
| | | 41 | | $InputObject -is [long] -or |
| | | 42 | | $InputObject -is [double] -or |
| | | 43 | | $InputObject -is [decimal]) { |
| | 0 | 44 | | return [TimeSpan]::FromSeconds([double]$InputObject) |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | # 3) String parsing |
| | 0 | 48 | | if ($InputObject -is [string]) { |
| | 0 | 49 | | $s = $InputObject.Trim() |
| | | 50 | | |
| | | 51 | | # Try .NET built-in formats first: "c", "g", "G" (e.g., "00:30:00", "1.02:03:04") |
| | 0 | 52 | | $ts = [TimeSpan]::Zero |
| | 0 | 53 | | if ([TimeSpan]::TryParse($s, [ref]$ts)) { return $ts } |
| | | 54 | | |
| | | 55 | | # Compact tokens: 1d2h30m15s250ms (order-insensitive, any subset) |
| | 0 | 56 | | 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*(? |
| | 0 | 57 | | $days = [int]::Parse(('0' + $Matches['d'])) |
| | 0 | 58 | | $hrs = [int]::Parse(('0' + $Matches['h'])) |
| | 0 | 59 | | $min = [int]::Parse(('0' + $Matches['m'])) |
| | 0 | 60 | | $sec = [int]::Parse(('0' + $Matches['s'])) |
| | 0 | 61 | | $msec = [int]::Parse(('0' + $Matches['ms'])) |
| | 0 | 62 | | return [TimeSpan]::new($days, $hrs, $min, $sec, $msec) |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | throw "Invalid TimeSpan format: '$s'. Try '00:30:00', '1.02:03:04', or tokens like '1d2h30m15s'." |
| | | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | throw "Cannot convert value of type [$($InputObject.GetType().FullName)] to [TimeSpan]." |
| | | 69 | | } |