demo program for the TimeZone library (see http://mbed.org/users/hlipka/notebook/time-zone-handling ).

Dependencies:   NetServices mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "EthernetNetIf.h"
00004 #include "NTPClient.h"
00005 
00006 #include "Time.h"
00007 
00008 LocalFileSystem local("local");
00009 
00010 void updateTime()
00011 {
00012     time_t ctTime;
00013     time(&ctTime);
00014     printf("Current time is (UTC): %s\n", ctime(&ctTime));
00015 
00016     NTPClient ntp;
00017     Host server(IpAddr(), 123, "0.de.pool.ntp.org");
00018     ntp.setTime(server);
00019 
00020   printf("set time ok\n");
00021   time(&ctTime);
00022   printf("Current time is (UTC): %s\n", ctime(&ctTime));
00023     
00024 }
00025 
00026 int main() {
00027     void* p1=malloc(16000); // to ensure we have enough heap after NTP setup
00028 
00029     printf("setup\n");
00030     EthernetNetIf eth;
00031     EthernetErr ethErr;
00032     printf("Setting up...\n");
00033     do {
00034         ethErr = eth.setup();
00035         if (ethErr) printf("waiting for network...\n", ethErr);
00036     } while (ethErr != ETH_OK);
00037 
00038     printf("setup ok\n");
00039 
00040     updateTime();
00041     
00042     free(p1); // reclaim back memory
00043 
00044     char *buf=new char[32];
00045 
00046     Time *time=new Time();
00047     TimeStamp *timestamp=time->getTime();
00048 
00049     snprintf(buf,32,"%.2d/%.2d %.2d:%.2d:%.2d",
00050         timestamp->getMonth()+1, timestamp->getDay(), 
00051         timestamp->getHour(), timestamp->getMinute(), timestamp->getSecond());
00052     printf("time=%s\n",buf);
00053 
00054     delete [] buf;
00055     delete timestamp;
00056     delete time;
00057 
00058 }