From dePoPo.net

Getting the current date/time from an internet NIST server

Posted in: General vb.NET
By dePoPo
Mar 23, 2009 - 4:01:10 PM

        ' --- Get the current time/date from an internet nist server
        ' more timeserver here: http://tf.nist.gov/service/time-servers.html

        Dim s_checkserver As String = "time.nist.gov"
        Dim myreader As StreamReader
        Dim s_timestring As String
        Try
            myreader = New StreamReader(New Net.Sockets.TcpClient(s_checkserver, 13).GetStream)
            s_timestring = myreader.ReadToEnd
            s_timestring = Trim(LTrim(s_timestring))
        Catch ex As Exception
            ' --- unable to contact timeserver
            End
        End Try

        ' --- Process the response from the server
        Dim n_work As Integer
        n_work = Val(Strings.Mid(s_timestring, 1, 6))
        Dim years As Integer = Val(Strings.Mid(s_timestring, 8, 2))
        Dim months As Integer = Val(Strings.Mid(s_timestring, 11, 2))
        Dim days As Integer = Val(Strings.Mid(s_timestring, 14, 2))
        Dim hours As Integer = Val(Strings.Mid(s_timestring, 17, 2))
        Dim minutes As Integer = Val(Strings.Mid(s_timestring, 20, 2))
        Dim seconds As Integer = Val(Strings.Mid(s_timestring, 23, 2))

        If n_work > 51544 Then
            years = years + 2000
        Else
            years = years + 1900
        End If

        Dim result As DateTime
        result = New DateTime(years, months, days, hours, minutes, seconds)

        MsgBox(result)

© Copyright 2010 by dePoPo.net