Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetNetIf SimpleSocket 1.0 mbed
ntpclient.cpp
- Committer:
- yamaguch
- Date:
- 2011-10-26
- Revision:
- 32:00114e36de90
- Child:
- 33:39d9cdf99de8
File content as of revision 32:00114e36de90:
#include "EthernetNetIf.h"
#include "SimpleSocket.h"
void ntpclient() {
char *NTP_SERVER = "pool.ntp.org";
printf("ntp client - %s\n", NTP_SERVER);
while (true) {
DatagramSocket datagram;
char buf[48] = {0x23};// 00100011 LI(0), Version(4), Mode(3: Client)
datagram.write(buf, sizeof(buf));
datagram.send(NTP_SERVER, 123);
if (datagram.receive() > 0) {
if (datagram.read(buf, sizeof(buf))) {
unsigned long seconds = 0;
for (int i = 40; i <= 43; i++)
seconds = (seconds << 8) | buf[i];
seconds -= 2208988800ULL;
set_time((time_t) seconds);
char buf[16];
time_t jstime = time(NULL) + 9 * 3600;
strftime(buf, sizeof(buf), "%m/%d %X", localtime(&jstime));
printf("Time: %s\n", buf);
break;
}
}
}
}