A time interface class. This class replicates the normal time functions, but goes a couple of steps further. mbed library 82 and prior has a defective gmtime function. Also, this class enables access to setting the time, and adjusting the accuracy of the RTC.

Dependencies:   CalendarPage

Dependents:   CI-data-logger-server WattEye X10Svr SSDP_Server

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Mon Nov 20 17:09:48 2017 +0000
Parent:
19:ccdf8b6f6aa1
Child:
21:f3818e2e0370
Commit message:
Updates to work with OS 5

Changed in this revision

NTPClient.lib Show annotated file Show diff for this revision Revisions of this file
TimeInterface.cpp Show annotated file Show diff for this revision Revisions of this file
TimeInterface.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.lib	Mon Nov 20 17:09:48 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/WiredHome/code/NTPClient/#0a9c48cb2b10
--- a/TimeInterface.cpp	Sat Jul 29 14:06:45 2017 +0000
+++ b/TimeInterface.cpp	Mon Nov 20 17:09:48 2017 +0000
@@ -31,8 +31,9 @@
 #endif
 
 
-TimeInterface::TimeInterface()
+TimeInterface::TimeInterface(EthernetInterface *net)
 {
+    m_net = net;
     dst = false;
     memset(&dst_pair, 0, sizeof(dst_pair));  // that's enough to keep it from running
 }
@@ -43,7 +44,7 @@
 
 NTPResult TimeInterface::setTime(const char* host, uint16_t port, uint32_t timeout)
 {
-    NTPClient ntp;
+    NTPClient ntp(m_net);
     NTPResult res;
     // int16_t tzomin = get_tzo_min();
     INFO("setTime(%s, %d, %d)\r\n", host, port, timeout);
@@ -450,7 +451,7 @@
 
 
 #ifndef isprint
-#define in_range(c, lo, up)  ((u8_t)c >= lo && (u8_t)c <= up)
+#define in_range(c, lo, up)  ((uint8_t)c >= lo && (uint8_t)c <= up)
 #define isprint(c)           in_range(c, 0x20, 0x7f)
 #define isdigit(c)           in_range(c, '0', '9')
 #define isxdigit(c)          (isdigit(c) || in_range(c, 'a', 'f') || in_range(c, 'A', 'F'))
--- a/TimeInterface.h	Sat Jul 29 14:06:45 2017 +0000
+++ b/TimeInterface.h	Mon Nov 20 17:09:48 2017 +0000
@@ -18,6 +18,10 @@
 #include "time.h"
 }
 
+/// The tm_ex structure is patterened after the traditional tm struct, however
+/// it adds an element - the time zone offset in minutes. From this, it is then
+/// readily able to create a "local time" instead of simply a UTC time.
+///
 struct tm_ex
 {
     int   tm_sec;       ///<! seconds, 0 to 59.
@@ -29,7 +33,7 @@
     int   tm_wday;      ///<! days since sunday 0 to 6.
     int   tm_yday;      ///<! days since 1 Jan 0 to 365.
     int   tm_isdst;     ///<! is daylight savings time.
-    int   tm_tzo_min;   ///<! localtime zone offset in minutes
+    int   tm_tzo_min;   ///<! localtime zone offset in minutes (_ex element)
 };
 
 /// TimeInterface class is much like the normal c-style time.h interface, but
@@ -86,7 +90,7 @@
 /// // +--------+     +- char * asctime(tm_ex *) -> +--------------------------+
 /// //      ^  |      |
 /// //      |  |      |                                 +-----------------+   
-/// //      |  |      +-------------------------------> | tm_ex           |   
+/// //      |  |      +-------------------------------- | tm_ex           |   
 /// //      |  |                                        |   .tm_sec       |   
 /// //      |  +- tm_ex * gmtime(const time_t *) -----> |   .tm_min       |   
 /// //      |  |                                        |   .tm_hour      |   
@@ -95,11 +99,12 @@
 /// //      +---- time_t mktime(struct tm_ex *) ------- |   .tm_year      |   
 /// //                                                  |   .tm_wday      |   
 /// //                                                  |   .tm_yday      |   
-/// //                                                  |   .tm_isdst     |   
-/// //    +-------------------------------------------- |   .tm_tzo_min   |               
-/// //    |                                             +-----------------+               
-/// //    |                                         +--------------------------+
-/// //    +- strftime(char * ptr, ..., tm_ex *) --> | buffer                   |
+/// //  +---------------------------------------------> |   .tm_isdst     |   
+/// //  | +-------------------------------------------- |   .tm_tzo_min   |               
+/// //  | |                                             +-----------------+               
+/// //  | |                                         +--------------------------+
+/// //  | +- strftime(char * ptr, ..., tm_ex *) --> | buffer                   |
+/// //  +----strptime(char * buf, ..., tm_ex *) --- | Www Mmm dd hh:mm:ss yyyy |
 /// //                                              +--------------------------+
 /// //  double difftime(time_t end, time_t)
 /// @endcode
@@ -109,7 +114,10 @@
 public:
     /// Constructor for the TimeInterface class, which does minimal initialization.
     ///
-    TimeInterface();
+    /// @param[in] net is optional and provides the EthernetInterface which is
+    ///             used if you want to sync to an NTP server
+    ///
+    TimeInterface(EthernetInterface *m_net = NULL);
     
     /// Destructor, normally not used, because it is typically kept for the life
     /// of the program.
@@ -172,6 +180,11 @@
     /// @note Unlike the standard ctime function, this version DOES NOT append 
     ///     a newline character to the buffer.
     ///
+    /// @code
+    /// time_t tsample = timelocal();
+    /// printf("time is %s\r\n", ctime(tsample));
+    /// @endcode
+    ///
     /// @param[in] timeptr is a pointer to a tm structure containing the time to convert.
     /// @returns a pointer to a private buffer containing the string.
     ///
@@ -499,6 +512,8 @@
 
     // ntp interface functions    
 private:
+    EthernetInterface * m_net;
+
     typedef struct {
         uint8_t MM;
         uint8_t DD;