Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Net.Sockets
- Imports System.Text
- Public Class Form1
- Private Function AtomicTime(ByVal sTimeServer As String) As Date
- ' Standard-Rückgabewert bei Fehler
- Dim vDate As Date = Nothing
- Try
- ' Verbindung zum TimerServer herstellen
- Dim oClient As TcpClient = New TcpClient()
- oClient.Connect(sTimeServer, 13)
- ' NetworkStream-Objekt für die Antwort
- Dim oStream As NetworkStream = oClient.GetStream()
- ' Antwort des TimeServer lesen
- Dim cCh1 As Char
- Dim sResponse As New StringBuilder
- Dim nByte As Integer
- ' solange byteweise einlesen, bis keine
- ' Daten mehr vom Server zurückkommen
- Do
- nByte = oStream.ReadByte()
- If nByte = -1 Then Exit Do
- cCh1 = System.Convert.ToChar(nByte)
- sResponse.Append(cCh1.ToString)
- Loop Until nByte = 13
- ' Verbindung trennen
- oClient.Close()
- If sResponse.Length > 0 Then
- Dim sBuffer As String = sResponse.ToString
- ' Atomzeiten werden in folgendem Format übermitteln:
- ' xxxxx yy-mm-dd hh:mm:ss xx x x xx.x UTC(NIST) *
- ' Datum und Uhrzeit aus dem Rückgabestring des
- ' TimeServers extahieren
- If sBuffer.Contains(" ") Then
- Dim sData() As String = sBuffer.Split(" ")
- If sData(1).Length = 8 AndAlso sData(2).Length = 8 Then
- ' Datumsangabe in Einzelteile splitten
- Dim sDate() As String = Split(sData(1), "-")
- ' Uhrzeitangabe in Einzelteile splitten
- Dim sTime() As String = Split(sData(2), ":")
- ' einzelne Datumswerte und Uhrzeitwerte zu neuem
- ' Date-Objekt zusammenstellen
- vDate = New DateTime(2000 + CInt(sDate(0)), _
- CInt(sDate(1)), CInt(sDate(2)), _
- CInt(sTime(0)), CInt(sTime(1)), CInt(sTime(2)))
- ' jetzt noch ggf. Sommer/Winterzeit berücksichtigen
- Dim nDif As Integer = Val(Now.ToString("zzzz").Substring(0, 3))
- If nDif <> 0 Then
- vDate = vDate.AddHours(nDif)
- End If
- End If
- End If
- End If
- Catch ex As Exception
- vDate = Nothing
- End Try
- Return vDate
- End Function
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- Application.DoEvents()
- Dim vDate As Date = AtomicTime("time.nist.gov")
- If Not vDate = Nothing Then
- Label1.Text = vDate.ToString
- Else
- Label1.Text = "Fehler"
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement