ConvertTime.psm1

$exportcmdlets = @()

function Convert-UnixTime {
    <#
.SYNOPSIS
Convert UnixTime to PowerShell DateTime
 
.DESCRIPTION
Convert UnixTime to PowerShell DateTime
 
.PARAMETER unixDate
(required) The unix time integer
 
.PARAMETER UTC
(optional) Return datetime relative to localtime based off system timezone
 
.EXAMPLE
Convert-UnixTime 1592001868
 
.EXAMPLE
Convert-UnixTime 1592001868 -UTC
 
.LINK
http://darrenjrobinson.com/convert-windows-and-unix-epoch-times-with-powershell
 
#>

    [cmdletbinding()]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)][int32]$unixDate,
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [switch]$UTC 
    )
    Try {
        $unixEpoch = (Get-Date -Year 1970 -Month 1 -Day 1 -hour 0 -Minute 0 -Second 0 -Millisecond 0)  
        if ($UTC) {
            $dateTime = $unixEpoch.AddSeconds($unixDate)
        }
        else {   
            $timeZone = Get-TimeZone
            $utcTime = $unixEpoch.AddSeconds($unixDate)
            $dateTime = $utcTime.AddMinutes($timeZone.BaseUtcOffset.TotalMinutes)
        }
        return $dateTime
    }
    catch {
        return $_
    }
}

$exportcmdlets += 'Convert-UnixTime'

function Convert-WindowsTime {
    <#
.SYNOPSIS
Convert Convert-WindowsTime to PowerShell DateTime
 
.DESCRIPTION
Convert Convert-WindowsTime to PowerShell DateTime
 
.PARAMETER winDate
(required) The Windows time integer
 
.PARAMETER UTC
(optional) Return datetime relative to localtime based off system timezone
 
.EXAMPLE
Convert-WindowsTime 132947402891099830
 
.EXAMPLE
Convert-WindowsTime 132947402891099830 -UTC
 
.LINK
http://darrenjrobinson.com/convert-windows-and-unix-epoch-times-with-powershell
 
#>

    [cmdletbinding()]
    Param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)][int64]$winDate,
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [switch]$UTC
    )

    try {
        $winEpoch = (Get-Date -Year 1601 -Month 1 -Day 1 -hour 0 -Minute 0 -Second 0 -Millisecond 0)        
        if ($UTC) {
            $dateTime = $winEpoch.AddDays($winDate / 864000000000)
        }
        else {
            $convertedDate = $winEpoch.AddDays($winDate / 864000000000)
            $timeZone = Get-TimeZone
            $dateTime = $convertedDate.AddMinutes($timeZone.BaseUtcOffset.TotalMinutes)
        }
        return $dateTime
    }
    catch {
        return $_
    }
}

$exportcmdlets += 'Convert-WindowsTime'

function Get-UnixTime {
    <#
.SYNOPSIS
Convert PowerShell DateTime to Unix timestamp
 
.DESCRIPTION
Convert PowerShell DateTime to Unix timestamp
 
.PARAMETER datetime
(required) A PowerShell DateTime object
 
.EXAMPLE
$now = Get-Date
Get-UnixTime $now
 
.EXAMPLE
Get-Date | Get-UnixTime
 
.EXAMPLE
Get-Unixtime -datetime 'Sunday, 9 October 2022 2:47:48 PM'
 
.LINK
http://darrenjrobinson.com/convert-windows-and-unix-epoch-times-with-powershell
 
#>


    [cmdletbinding()]
    Param(
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [DateTime]$datetime       
    )
    try {
        if ($datetime) {
            $unixEpoch = [int][double]::Parse((Get-Date $datetime -UFormat %s))
        }
        else {
            $unixEpoch = [int][double]::Parse((Get-Date -UFormat %s))
        }
        return $unixEpoch
    }
    catch {
        return $_
    }
}
$exportcmdlets += 'Get-UnixTime'

function Get-WindowsTime {
    <#
.SYNOPSIS
Convert PowerShell DateTime to Windows timestamp
 
.DESCRIPTION
Convert PowerShell DateTime to Windows timestamp
 
.PARAMETER datetime
(required) A PowerShell DateTime object
 
.EXAMPLE
$now = Get-Date
Get-WindowsTime $now
 
.EXAMPLE
Get-Date | Get-WindowsTime
 
.EXAMPLE
Get-WindowsTime -datetime 'Sunday, 9 October 2022 2:47:48 PM'
 
.LINK
http://darrenjrobinson.com/convert-windows-and-unix-epoch-times-with-powershell
 
#>


    [cmdletbinding()]
    Param(
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [DateTime]$datetime       
    )
    try {
        if ($datetime) {
            $windowsEpoch = (Get-Date $datetime).ToFileTime()
        }
        else {
            $windowsEpoch = (Get-Date).ToFileTime()
        }
        return $windowsEpoch
    }
    catch {
        return $_
    }
}
$exportcmdlets += 'Get-WindowsTime'

Export-ModuleMember -Function $exportcmdlets 

# SIG # Begin signature block
# MIINSwYJKoZIhvcNAQcCoIINPDCCDTgCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUSzgahMU2I+XC3+8G1fszRJgL
# VfugggqNMIIFMDCCBBigAwIBAgIQBAkYG1/Vu2Z1U0O1b5VQCDANBgkqhkiG9w0B
# AQsFADBlMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYD
# VQQLExB3d3cuZGlnaWNlcnQuY29tMSQwIgYDVQQDExtEaWdpQ2VydCBBc3N1cmVk
# IElEIFJvb3QgQ0EwHhcNMTMxMDIyMTIwMDAwWhcNMjgxMDIyMTIwMDAwWjByMQsw
# CQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cu
# ZGlnaWNlcnQuY29tMTEwLwYDVQQDEyhEaWdpQ2VydCBTSEEyIEFzc3VyZWQgSUQg
# Q29kZSBTaWduaW5nIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
# +NOzHH8OEa9ndwfTCzFJGc/Q+0WZsTrbRPV/5aid2zLXcep2nQUut4/6kkPApfmJ
# 1DcZ17aq8JyGpdglrA55KDp+6dFn08b7KSfH03sjlOSRI5aQd4L5oYQjZhJUM1B0
# sSgmuyRpwsJS8hRniolF1C2ho+mILCCVrhxKhwjfDPXiTWAYvqrEsq5wMWYzcT6s
# cKKrzn/pfMuSoeU7MRzP6vIK5Fe7SrXpdOYr/mzLfnQ5Ng2Q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