| | | 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 | | #> |
| | | 38 | | function ConvertTo-DateTimeOffset { |
| | | 39 | | [CmdletBinding()] |
| | | 40 | | [OutputType('System.DateTimeOffset')] |
| | | 41 | | param( |
| | | 42 | | [Parameter(Mandatory)] |
| | | 43 | | [object]$InputObject |
| | | 44 | | ) |
| | | 45 | | |
| | | 46 | | # Already DateTimeOffset |
| | 0 | 47 | | if ($InputObject -is [DateTimeOffset]) { return $InputObject } |
| | | 48 | | |
| | | 49 | | # DateTime -> DateTimeOffset (local) |
| | 0 | 50 | | if ($InputObject -is [DateTime]) { |
| | 0 | 51 | | return [DateTimeOffset]::new([DateTime]$InputObject) |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | # String → try absolute date/time first |
| | 0 | 55 | | if ($InputObject -is [string]) { |
| | 0 | 56 | | $s = $InputObject.Trim() |
| | 0 | 57 | | $dto = $null |
| | 0 | 58 | | 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 { |
| | 0 | 62 | | $ts = ConvertTo-TimeSpan -InputObject $s |
| | 0 | 63 | | return [DateTimeOffset]::Now.Add($ts) |
| | | 64 | | } catch { |
| | 0 | 65 | | 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 |
| | 0 | 70 | | if ($InputObject -is [TimeSpan]) { |
| | 0 | 71 | | return [DateTimeOffset]::Now.Add([TimeSpan]$InputObject) |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | # Numeric seconds => relative expiry |
| | 0 | 75 | | if ($InputObject -is [int] -or $InputObject -is [long] -or $InputObject -is [double] -or $InputObject -is [decimal]) |
| | 0 | 76 | | return [DateTimeOffset]::Now.AddSeconds([double]$InputObject) |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | throw "Cannot convert value of type [$($InputObject.GetType().FullName)] to [DateTimeOffset]." |
| | | 80 | | } |