demo program for the TimeZone library (see http://mbed.org/users/hlipka/notebook/time-zone-handling ).
Dependencies: NetServices mbed
main.cpp@0:255c470c37ca, 2010-12-22 (annotated)
- Committer:
- hlipka
- Date:
- Wed Dec 22 23:21:20 2010 +0000
- Revision:
- 0:255c470c37ca
initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:255c470c37ca | 1 | #include "mbed.h" |
hlipka | 0:255c470c37ca | 2 | |
hlipka | 0:255c470c37ca | 3 | #include "EthernetNetIf.h" |
hlipka | 0:255c470c37ca | 4 | #include "NTPClient.h" |
hlipka | 0:255c470c37ca | 5 | |
hlipka | 0:255c470c37ca | 6 | #include "Time.h" |
hlipka | 0:255c470c37ca | 7 | |
hlipka | 0:255c470c37ca | 8 | LocalFileSystem local("local"); |
hlipka | 0:255c470c37ca | 9 | |
hlipka | 0:255c470c37ca | 10 | void updateTime() |
hlipka | 0:255c470c37ca | 11 | { |
hlipka | 0:255c470c37ca | 12 | time_t ctTime; |
hlipka | 0:255c470c37ca | 13 | time(&ctTime); |
hlipka | 0:255c470c37ca | 14 | printf("Current time is (UTC): %s\n", ctime(&ctTime)); |
hlipka | 0:255c470c37ca | 15 | |
hlipka | 0:255c470c37ca | 16 | NTPClient ntp; |
hlipka | 0:255c470c37ca | 17 | Host server(IpAddr(), 123, "0.de.pool.ntp.org"); |
hlipka | 0:255c470c37ca | 18 | ntp.setTime(server); |
hlipka | 0:255c470c37ca | 19 | |
hlipka | 0:255c470c37ca | 20 | printf("set time ok\n"); |
hlipka | 0:255c470c37ca | 21 | time(&ctTime); |
hlipka | 0:255c470c37ca | 22 | printf("Current time is (UTC): %s\n", ctime(&ctTime)); |
hlipka | 0:255c470c37ca | 23 | |
hlipka | 0:255c470c37ca | 24 | } |
hlipka | 0:255c470c37ca | 25 | |
hlipka | 0:255c470c37ca | 26 | int main() { |
hlipka | 0:255c470c37ca | 27 | void* p1=malloc(16000); // to ensure we have enough heap after NTP setup |
hlipka | 0:255c470c37ca | 28 | |
hlipka | 0:255c470c37ca | 29 | printf("setup\n"); |
hlipka | 0:255c470c37ca | 30 | EthernetNetIf eth; |
hlipka | 0:255c470c37ca | 31 | EthernetErr ethErr; |
hlipka | 0:255c470c37ca | 32 | printf("Setting up...\n"); |
hlipka | 0:255c470c37ca | 33 | do { |
hlipka | 0:255c470c37ca | 34 | ethErr = eth.setup(); |
hlipka | 0:255c470c37ca | 35 | if (ethErr) printf("waiting for network...\n", ethErr); |
hlipka | 0:255c470c37ca | 36 | } while (ethErr != ETH_OK); |
hlipka | 0:255c470c37ca | 37 | |
hlipka | 0:255c470c37ca | 38 | printf("setup ok\n"); |
hlipka | 0:255c470c37ca | 39 | |
hlipka | 0:255c470c37ca | 40 | updateTime(); |
hlipka | 0:255c470c37ca | 41 | |
hlipka | 0:255c470c37ca | 42 | free(p1); // reclaim back memory |
hlipka | 0:255c470c37ca | 43 | |
hlipka | 0:255c470c37ca | 44 | char *buf=new char[32]; |
hlipka | 0:255c470c37ca | 45 | |
hlipka | 0:255c470c37ca | 46 | Time *time=new Time(); |
hlipka | 0:255c470c37ca | 47 | TimeStamp *timestamp=time->getTime(); |
hlipka | 0:255c470c37ca | 48 | |
hlipka | 0:255c470c37ca | 49 | snprintf(buf,32,"%.2d/%.2d %.2d:%.2d:%.2d", |
hlipka | 0:255c470c37ca | 50 | timestamp->getMonth()+1, timestamp->getDay(), |
hlipka | 0:255c470c37ca | 51 | timestamp->getHour(), timestamp->getMinute(), timestamp->getSecond()); |
hlipka | 0:255c470c37ca | 52 | printf("time=%s\n",buf); |
hlipka | 0:255c470c37ca | 53 | |
hlipka | 0:255c470c37ca | 54 | delete [] buf; |
hlipka | 0:255c470c37ca | 55 | delete timestamp; |
hlipka | 0:255c470c37ca | 56 | delete time; |
hlipka | 0:255c470c37ca | 57 | |
hlipka | 0:255c470c37ca | 58 | } |