Snippet - NTP with auto detection of timezone and DST

[title]NTP with auto detection of timezone and DST[/title]
In this code an implementation of NTP client and auto-detection classes for actual timezone. To avoid too large assemblies, I did not use any XML or JSON parsing classes. If your application is using XML or JSON parsing, the use of those here could reduce code size.

The code refreshes local time with NTP server in a configurable number of hours. I noticed that on my device with 10 hours the clock gets drifted in couple of seconds. Use 5-6 hours for refresh. This is the main reason I wrote this code… I did not implement a refresh when DST turns on or off. If anyone adds that, please share.

The code here works on NetduinoPlus device, and should easily deployed on any FEZ device.

How it Works:

By quering and I am finding the actual time-zone of the device:
Through I am finding my location in the world
Through I am finding the timezone data of this location


This auto-detection will work if the device is connected to the internet through standard NAT configuration, but with remote proxies the auto-detection will be
wrong as it will detect the timezone of the proxy itself. For most applications however it is good enough.

Don’t expect a GPS accuracy for your location. It will probably get you the name of city/town where your ISP is, and not your actual location, still for timezone purposes this good enough.

Please read Terms of Use and legal disclaimer

Special NOTE:

In this code an API Key that is registered to the author of this code.

Please modify it to your registered key <<<<
Search below for <<<< to location of where you need to put your key

You may use the code, modify or include in any project you have, posted in the hope it will be useful.
Please read Terms of Use and legal disclaimer
Please read Terms and Conditions

A slightly modified version of this code is used in this instructable: AskGeo service changed their interface a bit, new code to support that change in the instructable or the ZIP file here.

Thanks for your contribution Zakie.

I plan on using your AskGeo interface in my current home automatiuon project.
If I understand it correctly, this will return the current offset including adjustments for DST?
Is that correct?

The GeoBytes service returns the right TimeZone but does not tell you about DST.
The AskGeo service returns the TimeZone including the offset by DST.

Very cool! Thank you.

I realy like the code - thx for sharing.
As to the DST problem:
I usually prefere to maintain UTC on my clock, and then make the adjustment of timezone and DST when reading the time (a small function calculating DST at the actual time)…
I do’nt se how to determine DST-rule at a particular location, however.

It’s relying on the geo-location service. Here’s the extract from the example usage.

public static void InitLocation()        
GeoLoactionByIP myLocation = new GeoLoactionByIP();            
AskGeoService askGeo = new AskGeoService();             
Debug.Print("Location:\n\t" + myLocation.ToString() );             
askGeo.queryLocation(myLocation.latitude, myLocation.longitude);                        
int minutes = (int)(askGeo.currentOffserMS / 1000 / 60);            
Debug.Print("\tCurrent timezone offset=" +  minutes + " minutes");            NetworkTimeProtocolThread.SetTimeZoneOffset(minutes);        

Indeed, in many OSes and places people keep the Hardware based RTC and System clock in UTC, while simply calculating difference when user reads clock (and then it can be even per user rather than machine based).

Will be pretty straight forward to use NTP server to update the system clock (and it will be UTC), while the geo services and IP locator will only be used to save the delta from your TZ to UTC inside your DateTime wrapper.



Ain’t NetMF and [italic]especially[/italic] Gadgeteer some kewl toys?!?

You should check out my Gadgeteer WPAD tester - I think you’ll be amused…


Indeed, saw it when you posted it, VERY cool. Got any asks from our friends at CSS for assembled ones :D?

ooo; a $$-making opportunity! :think: