Created
October 7, 2015 20:01
-
-
Save ryancurrah/c6481751571d0962de09 to your computer and use it in GitHub Desktop.
Set-DatetimeFromNtp
This file contains hidden or 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
$Server = $args[0] | |
$StartOfEpoch=New-Object DateTime(1900,1,1,0,0,0,[DateTimeKind]::Utc) | |
[Byte[]]$NtpData = ,0 * 48 | |
$NtpData[0] = 0x1B # NTP Request header in first byte | |
$Socket = New-Object Net.Sockets.Socket([Net.Sockets.AddressFamily]::InterNetwork, | |
[Net.Sockets.SocketType]::Dgram, | |
[Net.Sockets.ProtocolType]::Udp) | |
$Socket.Connect($Server,123) | |
$t1 = Get-Date # Start of transaction... the clock is ticking... | |
[Void]$Socket.Send($NtpData) | |
[Void]$Socket.Receive($NtpData) | |
$t4 = Get-Date # End of transaction time | |
$Socket.Close() | |
$IntPart = [BitConverter]::ToUInt32($NtpData[43..40],0) # t3 | |
$FracPart = [BitConverter]::ToUInt32($NtpData[47..44],0) | |
$t3ms = $IntPart * 1000 + ($FracPart * 1000 / 0x100000000) | |
$IntPart = [BitConverter]::ToUInt32($NtpData[35..32],0) # t2 | |
$FracPart = [BitConverter]::ToUInt32($NtpData[39..36],0) | |
$t2ms = $IntPart * 1000 + ($FracPart * 1000 / 0x100000000) | |
$t1ms = ([TimeZoneInfo]::ConvertTimeToUtc($t1) - $StartOfEpoch).TotalMilliseconds | |
$t4ms = ([TimeZoneInfo]::ConvertTimeToUtc($t4) - $StartOfEpoch).TotalMilliseconds | |
$Offset = (($t2ms - $t1ms) + ($t3ms-$t4ms))/2 | |
$datetime = ($StartOfEpoch.AddMilliseconds($t4ms + $Offset).ToLocalTime()).GetDateTimeFormats()[44] | |
Set-Date $datetime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment