Setting system time

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I give up. Ive been working on this program for several hours. It should be very simple. All Im trying to do is get the internet time and add 25 seconds and set the system time. The setsystemtime(SysTime) or setLocaltime(SysTime) Neither function works even while running as adminsitrator.
Second problem is that the NewDate = DateAdd(DateInterval.Second, 25, CurrentDate) statement adds 25 seconds does not work. If time is 6:01:35 the results is 6:1:0. It should be 6:2:0.
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.InteropServices
Module Module1
Private Structure SYSTEMTIME
Public year As Short
Public month As Short
Public dayOfWeek As Short
Public day As Short
Public hour As Short
Public minute As Short
Public second As Short
Public milliseconds As Short
End Structure
Dim SysTime As SYSTEMTIME
Private Declare Sub GetSystemTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME)
Private Declare Function SetSystemTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME) As Boolean
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Declare Function SetLocalTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME) As Boolean
http://support.ntp.org/bin/view/Servers/ provides lists of Network Time Protocol (NTP) public time servers

Public LastSysTime As DateTime
Public Function GetNISTTime(ByVal host As String) As DateTime
Dim timeStr As String
Try
Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
LastSysTime = DateTime.UtcNow()
timeStr = reader.ReadToEnd()
reader.Close()
Catch ex As SocketException
Console.WriteLine(ex.Message())
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

Dim localZone As TimeZone = TimeZone.CurrentTimeZone
Dim currentOffset As TimeSpan = localZone.GetUtcOffset(DateTime.Now)
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))
SysTime.year = yr
SysTime.month = mo
SysTime.day = dy
SysTime.hour = hr
SysTime.minute = mm
SysTime.second = sc
Return New DateTime(yr + 2000, mo, dy, hr, mm, sc)
End Function
Sub Main()
Dim CurrentDate As DateTime
Dim NewDate As DateTime
Dim Result As Boolean
GetSystemTime(SysTime)
Console.WriteLine("PC Time: " & SysTime.hour & ":" & SysTime.minute & ":" & SysTime.second)
CurrentDate = GetNISTTime("utcnist.colorado.edu").ToString
Console.WriteLine("UTC Time: " & CurrentDate)
NewDate = DateAdd(DateInterval.Second, 25, CurrentDate)
SysTime.second = Second(NewDate)
Console.WriteLine("Adjusted Time: " & SysTime.hour & ":" & SysTime.minute & ":" & SysTime.second)
Result = SetLocalTime(SysTime)
Console.WriteLine("Results: " & Result)
GetSystemTime(SysTime)
Console.WriteLine("New Time: " & Format(SysTime.hour, "00") & ":" & Format(SysTime.minute, "00") & ":" & SysTime.second)
Sleep(15000)
End Sub

End Module

View the full article
 
Back
Top