none
getting internet time RRS feed

  • Question

  • I've searched the web trying to find code to access the internet time.  I currently have a powershell script that  accesses time.windows.com to return the time.  I would like to convert it to visual basic.

    When I do find samples they seem to be incopmplete as some of the variables are undefined when I cut and paste the code.

    Here is what I think is supposed to work. 

    Public Class Form1

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim varHostName As String
            Dim Timer As Timer
            varHostName = System.Net.Dns.GetHostEntry("time.windows.com").ToString()
            MsgBox(varHostName)
            Dim lAddressList() As System.Net.IPAddress = System.Net.Dns.GetHostAddresses("time.windows.com")
            'MsgBox(lAddressList)
            Dim IpEndPoint As New IPEndPoint(lAddressList(0), 123)
            Dim _ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            _ClientSocket.Connect(IpEndPoint)
            'Dim Buffer() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(1000)
            Buffer(0) = &H1B
            _ClientSocket.Send(Buffer, 0, Buffer.Length, SocketFlags.None)
            _ClientSocket.Receive(Buffer, 0, Buffer.Length, SocketFlags.None)
            Timer.Interval = 1000
            ' Start the timer.
             Timer.Start()
            _ClientSocket.Shutdown(SocketShutdown.Both)
            _ClientSocket.Close()
        End Sub
    End Class

    Thursday, February 21, 2013 4:37 PM

Answers

  • This seems to work.

    Imports System.IO
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Runtime.InteropServices
    
    ' http://support.ntp.org/bin/view/Servers/ provides lists of Network Time Protocol (NTP) public time servers
    
    Public Class Form1
    
        Public Shared LastSysTime As DateTime
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
            Label1.Text = GetNISTTime("utcnist.colorado.edu").ToString
        End Sub
    
        Public Function GetNISTTime(ByVal host As String) As DateTime
    
            Dim timeStr As String
    
            Try
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
                LastSysTime = DateTime.UtcNow()
                timeStr = reader.ReadToEnd()
                reader.Close()
            Catch ex As SocketException
                Label1.Text = ex.Message
            Catch ex As Exception
                Label1.Text = ex.Message
            End Try
    
            Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
            Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
            Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
            Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
            Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
            Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
            Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))
            Dim Temp As Integer = CInt(AscW(timeStr(7)))
    
            Return New DateTime(yr + 2000, mo, dy, hr, mm, sc)
    
        End Function
    
    
    End Class


    You've taught me everything I know but not everything you know.

    • Marked as answer by rac8006 Thursday, February 21, 2013 9:15 PM
    Thursday, February 21, 2013 7:40 PM

All replies

  • This seems to work.

    Imports System.IO
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Runtime.InteropServices
    
    ' http://support.ntp.org/bin/view/Servers/ provides lists of Network Time Protocol (NTP) public time servers
    
    Public Class Form1
    
        Public Shared LastSysTime As DateTime
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
            Label1.Text = GetNISTTime("utcnist.colorado.edu").ToString
        End Sub
    
        Public Function GetNISTTime(ByVal host As String) As DateTime
    
            Dim timeStr As String
    
            Try
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
                LastSysTime = DateTime.UtcNow()
                timeStr = reader.ReadToEnd()
                reader.Close()
            Catch ex As SocketException
                Label1.Text = ex.Message
            Catch ex As Exception
                Label1.Text = ex.Message
            End Try
    
            Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
            Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
            Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
            Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
            Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
            Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
            Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))
            Dim Temp As Integer = CInt(AscW(timeStr(7)))
    
            Return New DateTime(yr + 2000, mo, dy, hr, mm, sc)
    
        End Function
    
    
    End Class


    You've taught me everything I know but not everything you know.

    • Marked as answer by rac8006 Thursday, February 21, 2013 9:15 PM
    Thursday, February 21, 2013 7:40 PM