Getting the Time from an Atomic clock

welchb

Active member
Joined
Jul 4, 2005
Messages
25
Location
UK
Programming Experience
Beginner
Hi All,

I'd like to write a small app to get the time from an "atomic" clock -- I believe the UTC time and then compare this against the system clock.

So far I've been able to get the system time details with the code below but just cannot figure out where to start on getting the atomic clock(s) time.

I did Google this for examples and have not found anything useful, I did find a list of atomic time servers @ the following URL and realise I have to connect to something but can someone give me some pointers pls.

NIST Internet Time Service clock


Dim localDirect As Date = Date.Now
Dim utcDirect As Date = Date.UtcNow
Dim localFromUtc As Date = utcDirect.ToLocalTime()
Dim utcFromLocal As Date = localDirect.ToUniversalTime()
Dim localMinusOffset As Date = localDirect.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(localDirect))

MessageBox.Show(localDirect.ToString("o"), localDirect.Kind.ToString())
MessageBox.Show(utcDirect.ToString("o"), utcDirect.Kind.ToString())
MessageBox.Show(localFromUtc.ToString("o"), localFromUtc.Kind.ToString())
MessageBox.Show(utcFromLocal.ToString("o"), utcFromLocal.Kind.ToString())
MessageBox.Show(localMinusOffset.ToString("o"), localMinusOffset.Kind.ToString())
 
Thanks for the reply -- i think it was terminalogy in my Google search I was missing "ntp" and was using things like atomic clock etc....

I'll have a look through these and see what I come up with.

Many Thanks
 
OK so I've looked via the web and yes there are many items returend in the search but I cannot find anything that does the following -->

1) Provides a source code example in VB.NET
2) Does not use another .DLL (ASocket.dll is mentioned alot but this is a commerical library)

I found an example in C# but this is a bit beyond me so I would be grateful if anyone does have a more direct reference or URL I can look @

Tkx

Brian
 
OK guys,

please bear with me on this -- good article above (from jwpjr765), however this seems to be for VB6 rather than .NET and uses Winsock as the control.

Ideally what I'd like to do is update this and I believe I need to use "system.net.sockets" as the .NET equivilant or improved way of doing things over winsock.

I searched MS and found --> (System.Net.Sockets Namespace ())

Now as you might have guessed I'm still having troubles with this.

I'm working on the assumption of trying to repace all of the "Winsock1." lines with a system.net equivilant and don't know if I'm going the right direction or not.... ?

If there's a kind person who can give me some (more) pointers I hope I can work this out.

Thanks again for continued help.

Brian
 
@ jwpjr765

many, many thanks for this 2nd example --- it's very helpful and I acutally understand most of what's being done here -- at least at a high level :)

I am afraid I'm still a little bit stuck -- what I was trying to do just to test this and also my understanding --- have a simple form with one button (to run the code) and a label where I'd update results etc....

I took the code in the example/cut pasted to my Windows App, created the form and button. At this point checked it built and no errors -- all cool so far.

I then added two lines of my code on the Button.Click event as -->

Dim tmen As String
tmen = GetTime()

After doing this I get an Error in the build --> 'GetTime' is not declared. It may be inaccessible due to it's protection level (File Form1.vb)

I've tried to google an answer, and tried various other changes and still cannot get this to work

Obviously what I'm not understanding is how each piece of the code interacts and I'm only a hobbiest developer so probably do not know enough about the protection levels etc....

I am trying to learn and if someone could suggest some more pointers to help me

Tkx

Brian
 
Brian, have you 'imported' the class? You should have something like 'Imports GetDateFromInternet.DateTimeFromInternet' at the top of your form VB file before the class declaration 'Public Class {class_name}'.
Also, with your two lines of code you posted these will not work if you have 'Option Strict' set to 'On'. GetTime function returns a DateTime data type, and you are attempting to assign it to a 'String' data type. It's better 'practice' to explicitly assignment a variable the same data type. You should change 'tmen = GetTime()' to 'tmen = GetTime().ToString'.
Hope this helps.
 
Hi jwpjr765,

thanks for sticking with me on this and your help -- i do appreciate this.

You reply has re-kindled my dampend spirits as I was getting stuck and frustrated --- unfortunatly I still am.

I tried all that you suggested -- it did not like "Imports GetDateFromInternet.DateTimeFromInternet" and the "tmen = GetTime().ToString"
was OK however I'm still getting may be inaccessible due to it's protection level.

I know you should not do this but I did try "Option Strict Off" just to see if this would help and it did not.

I think I'm learning bits albeit slowely but do not know really how to proceed ?

Regards,

Brian
 
Well, at this point I'm a little lost in what may be your problem. I created a VB.NET 3.5 (with Visual Studio 2008) Windows application project and added the GetDateFromInternet class and it works fine for me. I was thinking to zip up my project and post it but it might not help you in learning your issue with your project. If you would like you're welcome to zip your project and post and let me review it for possible issue(s). There are different ways in including the class code into your project. Maybe you could simply explain to me how you incorporated the code into your project, whether as a separate class file, copied the code into your only .vb code file, etc. Let me know what you think. I hope we both learn from this experience!!!
 
Hi jwpjr765 -- yes I really don't know what I'm doing wrong.

I am using VS 2010 and don't know if that makes any difference -- perhaps if we could exchange .ZIP files as I can look at your's and compare what's different and I'll send mine to see if that worked on your version ?

I'll send you a PM with my email address.

Many Thanks,

Brian
 
This has now been resolved and many thanks to the poster who helped me.

Special mention to John for his help and the main part of the answer on post #7 -- see link -- this was very useful.
John was also kind enough to point me on the errors of my ways. -- many thanks
 
Back
Top