We help IT Professionals succeed at work.

VB.Net Getting time from government time server

PNRT
PNRT asked
on
Hi Experts.  Im writing a .Net4 VB program where accurate real time is important.   I can set Windows to update the time automatically from the internet but then I'm not sure if the workstation always has access to the internet.   I've tried pinging the time servers with no response so figure it would be easier to get the time from the servers myself.  Does anyone know how to get the time from time servers and then update the system clock.  That way if I cant get the time I can at least advise the user.  Thanks in advance.
Comment
Watch Question

I think this is what you're looking for: http://www.codeproject.com/KB/vb/daytime.aspx
Most Valuable Expert 2012
Top Expert 2014

Commented:
>but then I'm not sure if the workstation always has access to the internet.
> so figure it would be easier to get the time from the servers myself.

How would you connect to the servers without the internet?

Author

Commented:
Thank you both for the replies.

Quizwedge - I tried this very project and couldnt get it to work

Codecruiser - My problem is that if I just allow the OS to update the time but the workstation cant get to the time server, the time may go out of sync and no one will know.   I thought that if I pinged the time servers that may give an indication that they were contactable and therefore were probably updating the time correctly, but got no response to the pings.  So I thought that getting the time directly would allow me to test whether it was working or not.and be able to advise the user

Thanks
Most Valuable Expert 2012
Top Expert 2014

Commented:
Sorry, I had a family emergency so I haven't been able to test the project yet. Are you getting an error message? If so, what is the error message?

Author

Commented:
Hi quizwedge, many thanks for the reply.  Im not getting any error message other than the "Cant connect to server"  Its trying a bunch of IP's tho and its quite dated.  Maybe thats the probelm.
I tried the second example from CodeCruiser, the one from dotnet-snippets.  It works great but its in C# which is beyond me Im afraid.  It has a VB module included but but the main program is C#.
Hope the family emergency wasnt to serious.
Most Valuable Expert 2012
Top Expert 2014

Commented:
I dont see the C# code. Convert it here

http://converter.telerik.com/

Author

Commented:
Sorry,  I didnt make myself clear - your second suggestion was a download of a VB class that I couldnt get working - but in the code was a link to the latest download.  This was the one that worked very well, but was in c#.   It included a VB module in it so that you could choose, but it was beyond me to change everythign to VB
Most Valuable Expert 2012
Top Expert 2014

Commented:
Did you try the converter?
Sorry, I'm back. With the example I gave, if clear out some of the IP addresses so that 128.138.140.44 is the first one, it works for me.

A list of servers and their status can be found at http://tf.nist.gov/tf-cgi/servers.cgi# I'd update the list of servers with what you see there.

In addition, I'd change lines 97 - 104 to the following:

        If timeStr.Length < 47 OrElse (timeStr.Substring(38, 9) <> "UTC(NIST)") Then
            'This signature should be there
            Return DateTime.MinValue
        End If
        If timeStr.Length < 31 OrElse (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

Open in new window



That seems to be working for me. Let me know if it works for you.

Author

Commented:
With your changes, worked first time, many thanks for the help