Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.96 KB | None | 0 0
  1. Imports System.Net.Sockets
  2. Imports System.Text
  3. Public Class Form1
  4.  
  5.  
  6.    Private Function AtomicTime(ByVal sTimeServer As String) As Date
  7.        ' Standard-Rückgabewert bei Fehler
  8.        Dim vDate As Date = Nothing
  9.  
  10.        Try
  11.            ' Verbindung zum TimerServer herstellen
  12.            Dim oClient As TcpClient = New TcpClient()
  13.            oClient.Connect(sTimeServer, 13)
  14.  
  15.            ' NetworkStream-Objekt für die Antwort
  16.            Dim oStream As NetworkStream = oClient.GetStream()
  17.  
  18.            ' Antwort des TimeServer lesen
  19.            Dim cCh1 As Char
  20.            Dim sResponse As New StringBuilder
  21.            Dim nByte As Integer
  22.  
  23.            ' solange byteweise einlesen, bis keine
  24.            ' Daten mehr vom Server zurückkommen
  25.            Do
  26.                nByte = oStream.ReadByte()
  27.                If nByte = -1 Then Exit Do
  28.                cCh1 = System.Convert.ToChar(nByte)
  29.                sResponse.Append(cCh1.ToString)
  30.            Loop Until nByte = 13
  31.  
  32.            ' Verbindung trennen
  33.            oClient.Close()
  34.  
  35.            If sResponse.Length > 0 Then
  36.                Dim sBuffer As String = sResponse.ToString
  37.  
  38.                ' Atomzeiten werden in folgendem Format übermitteln:
  39.                ' xxxxx yy-mm-dd hh:mm:ss xx x x  xx.x UTC(NIST) *
  40.  
  41.                ' Datum und Uhrzeit aus dem Rückgabestring des
  42.                ' TimeServers extahieren
  43.                If sBuffer.Contains(" ") Then
  44.                    Dim sData() As String = sBuffer.Split(" ")
  45.                    If sData(1).Length = 8 AndAlso sData(2).Length = 8 Then
  46.                        ' Datumsangabe in Einzelteile splitten
  47.                        Dim sDate() As String = Split(sData(1), "-")
  48.  
  49.                        ' Uhrzeitangabe in Einzelteile splitten
  50.                        Dim sTime() As String = Split(sData(2), ":")
  51.  
  52.                        ' einzelne Datumswerte und Uhrzeitwerte zu neuem
  53.                        ' Date-Objekt zusammenstellen
  54.                        vDate = New DateTime(2000 + CInt(sDate(0)), _
  55.                          CInt(sDate(1)), CInt(sDate(2)), _
  56.                          CInt(sTime(0)), CInt(sTime(1)), CInt(sTime(2)))
  57.  
  58.                        ' jetzt noch ggf. Sommer/Winterzeit berücksichtigen
  59.                        Dim nDif As Integer = Val(Now.ToString("zzzz").Substring(0, 3))
  60.                        If nDif <> 0 Then
  61.                            vDate = vDate.AddHours(nDif)
  62.                        End If
  63.                    End If
  64.                End If
  65.            End If
  66.        Catch ex As Exception
  67.            vDate = Nothing
  68.        End Try
  69.  
  70.        Return vDate
  71.    End Function
  72.  
  73.    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  74.        Application.DoEvents()
  75.        Dim vDate As Date = AtomicTime("time.nist.gov")
  76.        If Not vDate = Nothing Then
  77.            Label1.Text = vDate.ToString
  78.        Else
  79.            Label1.Text = "Fehler"
  80.        End If
  81.    End Sub
  82. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement