Get time from

Posted on 2006-05-03
Last Modified: 2012-06-27

How can I get the exact time from in VB.NET so I can change the system time?

Thanks a lot

Question by:Ruttensoft

    Author Comment

    I have now coded this:

            Dim uri As String = ""
            Dim myHttpWebRequest As HttpWebRequest = WebRequest.Create(uri)
            Dim myHttpWebResponse As HttpWebResponse = myHttpWebRequest.GetResponse()
            Dim sr As Stream = myHttpWebResponse.GetResponseStream()
            Dim reader As StreamReader = New StreamReader(sr, System.Text.Encoding.ASCII)
            Dim srdata As String = reader.ReadToEnd
            Dim g() As String = Split(srdata, "<td align=""center""><font size=""7"" color=""white""><b>")
            Dim d() As String = Split(g(1), "<br>")
            Dim a() As String = Split(srdata, "</b></font><font size=""5"" color=""white"">")
            Dim s() As String = Split(a(1), "<br>")
            MsgBox("The time is: " & d(0) & " " & s(0))

    Is there an easier method?
    LVL 96

    Accepted Solution

    I don't have an easier one, just a more complex one:

    Imports System.Runtime

    Public Class InternetTimeServer

      ' Number of seconds that Windows clock can deviate from
      ' NIST and still be okay
      Private Const THRESHOLD_SECONDS As Integer = 15

      'Server IP addresses from
      Private Shared Servers() As String = { _
       "", "", "", "", _
       "", "", "", "", _
       "", "", "", "", _

      Private Shared m_lastHost As String = String.Empty
      Private Shared m_lastSysTime As DateTime

      Public Shared ReadOnly Property LastHost() As String
          Return m_lastHost
        End Get
      End Property     'LastHost

      Public Shared ReadOnly Property LastSystemTime() As String
          Return m_lastSysTime
        End Get
      End Property     'LastSystemTime

      Public Shared Function GetTimeFromServer() As DateTime

        ' Returns UTC/GMT using an NIST server if possible,
        ' degrading to simply returning the system clock.

        ' If we are successful in getting NIST time, then
        '   LastHost indicates which server was used and
        '   LastSystemTime contains the system time of the call.
        ' If LastSystemTime is not within 15 seconds of NIST time,
        '  the system clock may need to be reset.
        ' If LastHost is "", time is equal to system clock.

        Dim host As String
        Dim result As DateTime

        m_lastHost = String.Empty

        ' Scan through all the servers in the list to get
        ' the first server that returns a valid date/time.
        For Each host In Servers

          result = GetNISTTime(host)

          If result > DateTime.MinValue Then
            m_lastHost = host
            Exit For
          End If

        Next host

        If LastHost.Length = 0 Then
          ' No server in list was successful so use system time
          result = DateTime.UtcNow()
        End If

        Return result

      End Function     'GetTimeFromServer

      Public Shared Function SecondsDifference(ByVal dt1 As DateTime, ByVal dt2 As DateTime) As Integer

        Dim span As TimeSpan = dt1.Subtract(dt2)

        Return span.TotalSeconds

      End Function     'SecondsDifference

      Public Shared Function IsWindowsClockIncorrect() As Boolean

        Dim nist As DateTime = GetTimeFromServer()

        If (Math.Abs(SecondsDifference(nist, LastSystemTime)) > THRESHOLD_SECONDS) Then
          Return True
          Return False
        End If

      End Function     'IsWindowsClockIncorrect

      Private Shared Function GetNISTTime(ByVal host As String) As DateTime

        ' Returns DateTime.MinValue if host unreachable or does not
        ' produce a date/time value.

        Dim result As DateTime = DateTime.MinValue
        Dim timeStr As String = String.Empty


          Dim reader As New IO.StreamReader(New Net.Sockets.TcpClient(host, 13).GetStream)

          m_lastSysTime = DateTime.UtcNow()

          timeStr = reader.ReadToEnd()


          ' Couldn't connect to server, transmission error.
          Trace.WriteLine("Socket Exception [" & host & "]")

        Catch ex As Exception
          Trace.WriteLine("Exception [" & ex.Message & "]")
        End Try

        If timeStr.Length > 0 Then

          ' Look server signature and optimum status (time not off by  
          ' more than 5 seconds).
          If timeStr.Substring(38, 9) = "UTC(NIST)" AndAlso _
           timeStr.Substring(30, 1) = "0" Then

            Dim jd As Integer = Val(timeStr.Substring(1, 5))
            Dim yr As Integer = Val(timeStr.Substring(7, 2))
            Dim mo As Integer = Val(timeStr.Substring(10, 2))
            Dim dy As Integer = Val(timeStr.Substring(13, 2))
            Dim hr As Integer = Val(timeStr.Substring(16, 2))
            Dim mm As Integer = Val(timeStr.Substring(19, 2))
            Dim sc As Integer = Val(timeStr.Substring(22, 2))

            ' Date is before 1900.
            If (jd < 15020) Then

              If (jd > 51544) Then
                yr += 2000
                yr += 1900
              End If

              result = New DateTime(yr, mo, dy, hr, mm, sc)

            End If

          End If

        End If

        Return result

      End Function     'GetNISTTime

      <InteropServices.StructLayout(InteropServices.LayoutKind.Sequential)> _
      Private Structure SYSTEMTIME
        Public wYear As Int16
        Public wMonth As Int16
        Public wDayOfWeek As Int16
        Public wDay As Int16
        Public wHour As Int16
        Public wMinute As Int16
        Public wSecond As Int16
        Public wMilliseconds As Int16
      End Structure     'SYSTEMTIME

      Private Declare Function SetSystemTime Lib "kernel32.dll" _
        (ByRef time As SYSTEMTIME) As Int32

      Public Shared Function SetWindowsClock(ByVal time As DateTime) As Int32

        ' Sets system time, using UTC time. Windows will apply the
        ' time zone.

        Dim timeStru As New SYSTEMTIME

        timeStru.wYear = CType(time.Year, Int16)
        timeStru.wMonth = CType(time.Month, Int16)
        timeStru.wDay = CType(time.Day, Int16)
        timeStru.wDayOfWeek = CType(time.DayOfWeek, Int16)
        timeStru.wHour = CType(time.Hour, Int16)
        timeStru.wMinute = CType(time.Minute, Int16)
        timeStru.wSecond = CType(time.Second, Int16)
        timeStru.wMilliseconds = CType(time.Millisecond, Int16)

        Dim result As Int32 = SetSystemTime(timeStru)

        Return result

      End Function     'SetWindowsClock

    End Class


    Author Comment

    Thanks a lot, a small fault is in the code but otherwise it's perfect!

    (jd < 15020) must be (jd > 15020)

    Author Comment

    Btw. how does the code know that I am UTC +2? Its working perfectly but I dont know how the code knows it?
    LVL 96

    Expert Comment

    by:Bob Learned
    Thanks, I have a lot of code that sits without much debugging until I need it.


    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now