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.
Dependents: CI-data-logger-server WattEye X10Svr SSDP_Server
Diff: TimeInterface.h
- Revision:
- 20:5ca2c94d46b8
- Parent:
- 17:45dae5a72679
- Child:
- 21:f3818e2e0370
--- 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;