WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 211 users


Wrong DateTime on .NET Micro Framework Devices

Blogger : Ajax.NET Professional - AJAX and JSON made easy!
All posts : All posts by Ajax.NET Professional - AJAX and JSON made easy!
Category : AJAX
Blogged date : 2008 Mar 09

Since embeddedworld2008 in Nuremberg I'm playing in my free time with the .NET Micro Framework and the Digi Connect ME network device. I've created a small Web server and ported the Ajax.NET Professional library to the really cool and small .NET framework.

When I reset such a device the current date and time gets wrong, too. This is very bad as I would like to have always the correct time, of course. Especially when you are working with Web servers it is very important to have the correct time because http headers contain date info when files should expire or how long they are allowed to be cached locally in Web browsers cache.

I have created a simple method to query a NTP server and set the device date to the returned value. Here is the code:

public static DateTime GetNetworkTime() { IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry("time-a.nist.gov").AddressList[0], 123); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); s.Connect(ep); byte[] ntpData = new byte[48]; // RFC 2030 ntpData[0] = 0x1B; for (int i = 1; i < 48; i++) ntpData[i] = 0; s.Send(ntpData); s.Receive(ntpData); byte offsetTransmitTime = 40; ulong intpart = 0; ulong fractpart = 0; for (int i = 0; i <= 3; i++) intpart = 256 * intpart + ntpData[offsetTransmitTime + i]; for (int i = 4; i <= 7; i++) fractpart = 256 * fractpart + ntpData[offsetTransmitTime + i]; ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L); s.Close(); TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond); DateTime dateTime = new DateTime(1900, 1, 1); dateTime += timeSpan; TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime); DateTime networkDateTime = (dateTime + offsetAmount); Debug.Print(networkDateTime.ToString()); return networkDateTime; }

To set the returned DateTime as new time on the device use following utility method:

Utility.SetLocalTime(GetNetworkTime());

Note, that the current time zone is often wrong, too. Use following method to set the time zone to the correct value, in my example I use the time zone here in Germany:

Microsoft.SPOT.ExtendedTimeZone.SetTimeZone(TimeZoneId.Berlin);

I put this code in the Main() method of my applications to get always the correct date when I start my device.


Read comments or post a reply to : Wrong DateTime on .NET Micro Framework Devices
Page 640 of 1924
Next | Last

.NET News Categories








Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card