tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
2495
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    curzon curzon ist offline Mitglied
    Registriert seit
    Jan 2007
    Beiträge
    22
    Hallo, bin wiedermal auf ein Hindernis gestoßen, hoffe es kann mir jemand helfen.

    Ich will mit VB .NET die Uhrzeit und Datum eines NTP Servers abfragen, wie geht das ?

    mfg curzon
    Geändert von curzon (17.05.08 um 23:30 Uhr)
     
    VB . Net 2005 Professional mit .Net 2.0 auf Win XP

  2. #2
    curzon curzon ist offline Mitglied
    Registriert seit
    Jan 2007
    Beiträge
    22
    Oder gibt es eine andere möglichkeit die aktuelle Uhrzeit und das Datum herauszufinden ohne auf die Systemuhrzeit zuzugreifen ?
     
    VB . Net 2005 Professional mit .Net 2.0 auf Win XP

  3. #3
    Avatar von JensG
    JensG JensG ist offline Mitglied Platin
    Registriert seit
    Jun 2004
    Ort
    Gera (Thüringen)
    Beiträge
    517
    Hallo,

    das geht z.B. mit folgender Klasse

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    
     
    Public Class Daytime
        '
        Private Const THRESHOLD_SECONDS As Integer = 15 'Number of seconds
        ' that Windows clock can deviate from NIST and still be okay
     
        'Server IP addresses from 
        'http://www.boulder.nist.gov/timefreq/service/time-servers.html
        Private Shared Servers() As String = { _
              "129.6.15.28" _
            , "129.6.15.29" _
            , "132.163.4.101" _
            , "132.163.4.102" _
            , "132.163.4.103" _
            , "128.138.140.44" _
            , "192.43.244.18" _
            , "131.107.1.10" _
            , "66.243.43.21" _
            , "216.200.93.8" _
            , "208.184.49.9" _
            , "207.126.98.204" _
            , "205.188.185.33" _
        }
     
        Public Shared LastHost As String = ""
        Public Shared LastSysTime As DateTime
     
        Public Shared Function GetTime() 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
            ' LastSysTime contains the system time of the call
            ' If LastSysTime 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
     
            LastHost = ""
            For Each host In Servers
                result = GetNISTTime(host)
                If result > DateTime.MinValue Then
                    LastHost = host
                    Exit For
                End If
            Next
     
            If LastHost = "" Then
                'No server in list was successful so use system time
                result = DateTime.UtcNow()
            End If
     
            Return result
        End Function
        Public Shared Function SecondsDifference(ByVal dt1 As DateTime, ByVal dt2 As DateTime) As Integer
            Dim span As TimeSpan = dt1.Subtract(dt2)
            Return span.Seconds + (span.Minutes * 60) + (span.Hours * 360)
        End Function
     
        Public Shared Function WindowsClockIncorrect() As Boolean
            Dim nist As DateTime = GetTime()
            If (Math.Abs(SecondsDifference(nist, LastSysTime)) > THRESHOLD_SECONDS) Then
                Return True
            End If
            Return False
        End Function
        Private Shared Function GetNISTTime(ByVal host As String) As DateTime
     
            'Returns DateTime.MinValue if host unreachable or does not produce time
            Dim timeStr As String
     
            Try
                Dim reader As New StreamReader(New TcpClient(host, 13).GetStream, System.Text.Encoding.Default)
                LastSysTime = DateTime.UtcNow()
                timeStr = reader.ReadToEnd()
                reader.Close()
            Catch ex As SocketException
                'Couldn't connect to server, transmission error
                'Debug.WriteLine("Socket Exception [" & host & "]")
                Return DateTime.MinValue
            Catch ex As Exception
                'Some other error, such as Stream under/overflow
                Return DateTime.MinValue
            End Try
     
            Try
     
                'Parse timeStr
                If (timeStr.Substring(38, 9) <> "UTC(NIST)") Then
                    'This signature should be there
                    Return DateTime.MinValue
                End If
                If (timeStr.Substring(30, 1) <> "0") Then
                    'Server reports non-optimum status, time off by as much as 5 seconds
                    Return DateTime.MinValue    'Try a different server
                End If
     
                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))
     
                If (jd < 15020) Then
                    'Date is before 1900
                    Return DateTime.MinValue
                End If
                If (jd > 51544) Then yr += 2000 Else yr += 1900
     
                Return New DateTime(yr, mo, dy, hr, mm, sc)
     
            Catch ex As Exception
                Return Date.Now
            End Try
     
        End Function
     
        <StructLayout(LayoutKind.Sequential)> _
            Public 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
     
        Private Declare Function GetSystemTime Lib "kernel32.dll" (ByRef stru As SYSTEMTIME) As Int32
        Private Declare Function SetSystemTime Lib "kernel32.dll" (ByRef stru As SYSTEMTIME) As Int32
     
        Public Shared Sub SetWindowsClock(ByVal dt As DateTime)
            'Sets system time. Note: Use UTC time; Windows will apply time zone
     
            Dim timeStru As SYSTEMTIME
            Dim result As Int32
     
            timeStru.wYear = CType(dt.Year, Int16)
            timeStru.wMonth = CType(dt.Month, Int16)
            timeStru.wDay = CType(dt.Day, Int16)
            timeStru.wDayOfWeek = CType(dt.DayOfWeek, Int16)
            timeStru.wHour = CType(dt.Hour, Int16)
            timeStru.wMinute = CType(dt.Minute, Int16)
            timeStru.wSecond = CType(dt.Second, Int16)
            timeStru.wMilliseconds = CType(dt.Millisecond, Int16)
     
            result = SetSystemTime(timeStru)
     
        End Sub
    End Class

    Gruß
    Jens
     

Ähnliche Themen

  1. Wie bekomme ich value von einem Textfield?
    Von Code46 im Forum Swing, Java2D/3D, SWT, JFace
    Antworten: 7
    Letzter Beitrag: 08.02.10, 22:31
  2. Wie bekomme ich einen Boostereffekt bei einem Triebwerk hin?
    Von MDS_LIMITY im Forum 3D Studio Max
    Antworten: 38
    Letzter Beitrag: 19.06.07, 09:59
  3. Wie bekomme ich den LEXBCE - Server
    Von lordofscotland im Forum Microsoft Windows
    Antworten: 0
    Letzter Beitrag: 18.04.07, 19:36
  4. Antworten: 4
    Letzter Beitrag: 02.10.05, 14:50
  5. Antworten: 9
    Letzter Beitrag: 25.03.05, 18:44