Last active
January 18, 2019 20:12
-
-
Save jhochwald/60fbd60fb26baaee593a5d5a27446d8f to your computer and use it in GitHub Desktop.
Converts a Timestamp (Epochdate) into Datetime AND Converts a Datetime into a Unix Timestamp (Epochdate)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ConvertFrom-UnixTimeStamp | |
{ | |
<# | |
.SYNOPSIS | |
Converts a Timestamp (Epochdate) into Datetime | |
.DESCRIPTION | |
Converts a Timestamp (Epochdate) into Datetime | |
.PARAMETER TimeStamp | |
Timestamp (Epochdate) | |
.PARAMETER Milliseconds | |
Is the given Timestamp (Epochdate) in Miliseconds instead of Seconds? | |
.EXAMPLE | |
PS C:\> ConvertFrom-UnixTimeStamp -TimeStamp 1547839380 | |
Converts a Timestamp (Epochdate) into Datetime | |
.EXAMPLE | |
PS C:\> ConvertFrom-UnixTimeStamp -TimeStamp 1547839380712 -Milliseconds | |
Converts a Timestamp (Epochdate) into Datetime, given value is in Milliseconds | |
.NOTES | |
Added the 'UniFi' (Alias for the switch 'Milliseconds') because the API returns miliseconds instead of seconds | |
#> | |
[CmdletBinding(ConfirmImpact = 'None')] | |
[OutputType([datetime])] | |
param | |
( | |
[Parameter(Mandatory, | |
ValueFromPipeline, | |
Position = 0, | |
HelpMessage = 'Timestamp (Epochdate)')] | |
[ValidateNotNullOrEmpty()] | |
[Alias('Epochdate')] | |
[long] | |
$TimeStamp, | |
[Parameter(ValueFromPipeline, | |
ValueFromPipelineByPropertyName, | |
Position = 1)] | |
[Alias('UniFi')] | |
[switch] | |
$Milliseconds = $false | |
) | |
begin | |
{ | |
# Set some defaults | |
$UnixStartTime = '1/1/1970' | |
# Cleanup | |
$Result = $null | |
} | |
process | |
{ | |
try | |
{ | |
if ($Milliseconds) | |
{ | |
$Result = ((Get-Date -Date $UnixStartTime -ErrorAction Stop -WarningAction SilentlyContinue).AddMilliseconds($TimeStamp)) | |
} | |
else | |
{ | |
try | |
{ | |
$Result = ((Get-Date -Date $UnixStartTime -ErrorAction Stop -WarningAction SilentlyContinue).AddSeconds($TimeStamp)) | |
} | |
catch | |
{ | |
# Try a Fallback! | |
$Result = ((Get-Date -Date $UnixStartTime -ErrorAction Stop -WarningAction SilentlyContinue).AddMilliseconds($TimeStamp)) | |
} | |
} | |
} | |
catch | |
{ | |
# get error record | |
[Management.Automation.ErrorRecord]$e = $_ | |
# retrieve information about runtime error | |
$info = [PSCustomObject]@{ | |
Exception = $e.Exception.Message | |
Reason = $e.CategoryInfo.Reason | |
Target = $e.CategoryInfo.TargetName | |
Script = $e.InvocationInfo.ScriptName | |
Line = $e.InvocationInfo.ScriptLineNumber | |
Column = $e.InvocationInfo.OffsetInLine | |
} | |
Write-Verbose -Message $info | |
Write-Error -Message ($info.Exception) -ErrorAction Stop | |
# Only here to catch a global ErrorAction overwrite | |
break | |
} | |
} | |
end | |
{ | |
$Result | |
} | |
} | |
function ConvertTo-UnixTimeStamp | |
{ | |
<# | |
.SYNOPSIS | |
Converts a Datetime into a Unix Timestamp (Epochdate) | |
.DESCRIPTION | |
Converts a Datetime into a Unix Timestamp (Epochdate) | |
.PARAMETER Date | |
The Date String that shoul be converted, default is now (if none is given) | |
.PARAMETER Milliseconds | |
Should the Timestamp (Epochdate) in Miliseconds instead of Seconds? | |
.EXAMPLE | |
PS C:\> ConvertTo-UnixTimeStamp | |
Converts the actual time into a Unix Timestamp (Epochdate) | |
.EXAMPLE | |
PS C:\> ConvertTo-UnixTimeStamp -Milliseconds | |
Converts the actual time into a Unix Timestamp (Epochdate), in milliseconds | |
.EXAMPLE | |
PS C:\> ConvertTo-UnixTimeStamp -Date ((Get-Date).AddDays(-1)) | |
Covert the same time yesterday into a Unix Timestamp (Epochdate) | |
.EXAMPLE | |
PS C:\> ConvertTo-UnixTimeStamp -Date ((Get-Date).AddDays(-1)) -Milliseconds | |
Covert the same time yesterday into a Unix Timestamp (Epochdate), in milliseconds | |
.NOTES | |
Added the 'UniFi' (Alias for the switch 'Milliseconds') because the API returns miliseconds instead of seconds | |
#> | |
[CmdletBinding(ConfirmImpact = 'None')] | |
[OutputType([long])] | |
param | |
( | |
[Parameter(ValueFromPipeline, | |
ValueFromPipelineByPropertyName, | |
Position = 0)] | |
[ValidateNotNullOrEmpty()] | |
[Alias('TimeStamp', 'DateTimeStamp')] | |
[datetime] | |
$Date = (Get-Date), | |
[Parameter(ValueFromPipeline, | |
ValueFromPipelineByPropertyName, | |
Position = 1)] | |
[Alias('UniFi')] | |
[switch] | |
$Milliseconds = $false | |
) | |
begin | |
{ | |
# Set some defaults | |
$UnixStartTime = '1/1/1970' | |
# Cleanup | |
$Result = $null | |
} | |
process | |
{ | |
try | |
{ | |
if ($Milliseconds) | |
{ | |
$Result = ([long]((New-TimeSpan -Start (Get-Date -Date $UnixStartTime -ErrorAction Stop -WarningAction SilentlyContinue) -End (Get-Date -Date $Date -ErrorAction Stop -WarningAction SilentlyContinue) -ErrorAction Stop -WarningAction SilentlyContinue).TotalMilliseconds)) | |
} | |
else | |
{ | |
$Result = ([long]((New-TimeSpan -Start (Get-Date -Date $UnixStartTime -ErrorAction Stop -WarningAction SilentlyContinue) -End (Get-Date -Date $Date -ErrorAction Stop -WarningAction SilentlyContinue) -ErrorAction Stop -WarningAction SilentlyContinue).TotalSeconds)) | |
} | |
} | |
catch | |
{ | |
# get error record | |
[Management.Automation.ErrorRecord]$e = $_ | |
# retrieve information about runtime error | |
$info = [PSCustomObject]@{ | |
Exception = $e.Exception.Message | |
Reason = $e.CategoryInfo.Reason | |
Target = $e.CategoryInfo.TargetName | |
Script = $e.InvocationInfo.ScriptName | |
Line = $e.InvocationInfo.ScriptLineNumber | |
Column = $e.InvocationInfo.OffsetInLine | |
} | |
Write-Verbose -Message $info | |
Write-Error -Message ($info.Exception) -ErrorAction Stop | |
# Only here to catch a global ErrorAction overwrite | |
break | |
} | |
} | |
end | |
{ | |
$Result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment