Public/ConvertFrom-DattoUnixTime.ps1
|
function ConvertFrom-DattoUnixTime { <# .SYNOPSIS Converts Datto epoch values to DateTimeOffset values. .DESCRIPTION Converts Unix timestamps expressed in seconds or milliseconds. Auto mode treats absolute values of 100000000000 or greater as milliseconds, which covers the mixed epoch formats documented by the Datto schemas. .PARAMETER Value The epoch value. .PARAMETER Unit Seconds, Milliseconds, or Auto. .EXAMPLE ConvertFrom-DattoUnixTime -Value 1640098562 .EXAMPLE 1688546431064 | ConvertFrom-DattoUnixTime -Unit Milliseconds .OUTPUTS System.DateTimeOffset #> [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline)] [long]$Value, [Parameter()] [ValidateSet('Auto', 'Seconds', 'Milliseconds')] [string]$Unit = 'Auto' ) process { $resolvedUnit = $Unit if ($resolvedUnit -eq 'Auto') { if ([Math]::Abs([double]$Value) -ge 100000000000) { $resolvedUnit = 'Milliseconds' } else { $resolvedUnit = 'Seconds' } } $epoch = [datetimeoffset]'1970-01-01T00:00:00+00:00' if ($resolvedUnit -eq 'Milliseconds') { $epoch.AddMilliseconds($Value) } else { $epoch.AddSeconds($Value) } } } |