Este utilidad para nuestro Netduino toma el dato fecha-hora(datetime) de un servidor NTP
El código se basa en http://weblogs.asp.net/mschwarz/archive/2008/03/09/wrong-datetime-on-net-micro-framework-devices.aspx
Código para Netduino-Plus:
|
using System.Net.Sockets; |
|
using Microsoft.SPOT.Hardware; |
|
using SecretLabs.NETMF.Hardware; |
|
using SecretLabs.NETMF.Hardware.Netduino; |
|
using Microsoft.SPOT.Time; |
|
namespace NetduinoPlusWebServer |
|
public static DateTime NTPTime(String TimeServer) |
|
// Find endpoint for timeserver |
|
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123); |
|
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); |
|
// Make send/receive buffer |
|
byte [] ntpData = new byte [48]; |
|
Array.Clear(ntpData, 0, 48); |
|
byte offsetTransmitTime = 40; |
|
for ( int i = 0; i <= 3; i++) |
|
intpart = (intpart << 8 ) | ntpData[offsetTransmitTime + i]; |
|
for ( int i = 4; i <= 7; i++) |
|
fractpart = (fractpart << 8 ) | ntpData[offsetTransmitTime + i]; |
|
ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L); |
|
TimeSpan timeSpan = TimeSpan.FromTicks(( long )milliseconds * TimeSpan.TicksPerMillisecond); |
|
DateTime dateTime = new DateTime(1900, 1, 1); |
|
TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime); |
|
DateTime networkDateTime = (dateTime + offsetAmount); |
Before writing to disk, data is encoded in the "8 in 14" standard and stored in the form of land…
After exploring a handful of the articles on your blog, I truly appreciate your way of writing a blog. I…
I'm not sure wһy but thіѕ blog iis loading extremely slow fоr me. Ιs anyone eⅼse having thіs isesue oг…
Ԍood blog youu have got һere.. Ӏt'ѕ difficult tto fіnd ցood quality writing ⅼike yoᥙrs these ɗays. I reallʏ apprеciate…
Si eres un extranjero en este país y te sientes agobiado o inseguro acerca de cómo proceder, te recomiendo fuertemente…