Main Site Documentation

Snippet - NTP with auto detection of timezone and DST


#1

[title]NTP with auto detection of timezone and DST[/title]
http://code.tinyclr.com/project/468/ntp-with-auto-detection-of-timezone-and-dst/
[line]
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 geobytes.com and askGeo.com I am finding the actual time-zone of the device:
Through geobytes.com I am finding my location in the world
Through askGeo.com I am finding the timezone data of this location

Limitations:

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 http://www.askgeo.com 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 http://www.askgeo.com Terms of Use and legal disclaimer
Please read http://www.geobytes.com Terms and Conditions

A slightly modified version of this code is used in this instructable: http://www.instructables.com/id/Weather-Underground-on-graphical-display-with-Netd/ AskGeo service changed their interface a bit, new code to support that change in the instructable or the ZIP file here.

#2

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?


#3

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.


#4

Very cool! Thank you.


#5

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.
.


#6

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();             
myLocation.queryLocation();            
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);        
}

#7

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.


#8

Zakie!
Bro!!

HowYouBeNStuff?!?

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…

Jim


#9

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


#10

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