Private/ConvertFrom-UnixDate.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Function ConvertFrom-UnixDate {
    <#
    .SYNOPSIS
        Convert from Unix time to DateTime

    .DESCRIPTION
        Convert from Unix time to DateTime

    .PARAMETER Date
        Date to convert, in Unix / Epoch format

    .PARAMETER Utc
        Default behavior is to convert Date to universal time.

        Set this to false to return local time.

    .EXAMPLE
        ConvertFrom-UnixDate -Date 1441471257

    .FUNCTIONALITY
        General Command
    #>

    Param(
        [int]$Date,
        [bool]$Utc = $true
    )

    # Adapted from http://stackoverflow.com/questions/10781697/convert-unix-time-with-powershell
    $unixEpochStart = new-object DateTime 1970,1,1,0,0,0,([DateTimeKind]::Utc)
    $Output = $unixEpochStart.AddSeconds($Date)

    if(-not $utc)
    {
        $Output = $Output.ToLocalTime()
    }

    $Output
}