Skip to content

Instantly share code, notes, and snippets.

@rasmuseeg
Created October 8, 2021 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rasmuseeg/7e8040a6b4cb1158272f901c9c9a17a4 to your computer and use it in GitHub Desktop.
Save rasmuseeg/7e8040a6b4cb1158272f901c9c9a17a4 to your computer and use it in GitHub Desktop.
Converts unix or epoch times to DateTime object.
function ConvertFrom-UnixTime {
[CmdletBinding(DefaultParameterSetName = "Seconds")]
param (
[Parameter(Position = 0,
ValueFromPipeline = $true,
Mandatory = $true,
ParameterSetName = "Seconds")]
[int]
$Seconds,
# Parameter help description
[Parameter(Position = 0,
ValueFromPipeline = $true,
Mandatory = $true, ParameterSetName = "Miliseconds")]
[bigint]
$Miliseconds
)
Begin {
$date = (Get-Date "1970-01-01 00:00:00.000Z")
}
Process {
switch ($PSCmdlet.ParameterSetName) {
"Miliseconds" {
$date = $date.AddMilliseconds($Miliseconds)
}
Default {
$date = $date.AddSeconds($Seconds);
}
}
}
End {
$date
}
}
Set-Alias -Name epoch -Value ConvertFrom-UnixTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment