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

Committer:
WiredHome
Date:
Tue Nov 21 17:04:12 2017 +0000
Revision:
21:f3818e2e0370
Parent:
20:5ca2c94d46b8
Child:
24:45a9e7081499
Updates for OS5 and integrate NTPClient for network time sync.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:61112ca9193b 1
WiredHome 0:61112ca9193b 2 #ifndef TIMEINTERFACE_H
WiredHome 0:61112ca9193b 3 #define TIMEINTERFACE_H
WiredHome 0:61112ca9193b 4 #include "mbed.h"
WiredHome 4:9cae2da8215e 5 #include <ctime>
WiredHome 0:61112ca9193b 6
WiredHome 21:f3818e2e0370 7 #include "NTPClient.h"
WiredHome 2:65e0a25c7551 8
WiredHome 0:61112ca9193b 9 // Special Registers and their usage:
WiredHome 0:61112ca9193b 10 // GPREG0: 32 bits
WiredHome 0:61112ca9193b 11 // low word: time zone offset (-720 to +720)
WiredHome 0:61112ca9193b 12 // high word: 2's complement of low word for integrity checking
WiredHome 0:61112ca9193b 13 // GPREG1: 32 bits
WiredHome 0:61112ca9193b 14 // time_t value when the clock was last set
WiredHome 0:61112ca9193b 15
WiredHome 0:61112ca9193b 16
WiredHome 0:61112ca9193b 17 extern "C" {
WiredHome 21:f3818e2e0370 18 #include "time.h" // uses some std::time-functions
WiredHome 0:61112ca9193b 19 }
WiredHome 0:61112ca9193b 20
WiredHome 21:f3818e2e0370 21 /// The tm_ex structure is patterned after the traditional tm struct, however
WiredHome 20:5ca2c94d46b8 22 /// it adds an element - the time zone offset in minutes. From this, it is then
WiredHome 20:5ca2c94d46b8 23 /// readily able to create a "local time" instead of simply a UTC time.
WiredHome 20:5ca2c94d46b8 24 ///
WiredHome 0:61112ca9193b 25 struct tm_ex
WiredHome 0:61112ca9193b 26 {
WiredHome 0:61112ca9193b 27 int tm_sec; ///<! seconds, 0 to 59.
WiredHome 0:61112ca9193b 28 int tm_min; ///<! minutes, 0 to 59.
WiredHome 0:61112ca9193b 29 int tm_hour; ///<! hours, 0 to 23.
WiredHome 0:61112ca9193b 30 int tm_mday; ///<! monthday 1 to 31.
WiredHome 0:61112ca9193b 31 int tm_mon; ///<! month 0 to 11.
WiredHome 0:61112ca9193b 32 int tm_year; ///<! years since 1900.
WiredHome 0:61112ca9193b 33 int tm_wday; ///<! days since sunday 0 to 6.
WiredHome 0:61112ca9193b 34 int tm_yday; ///<! days since 1 Jan 0 to 365.
WiredHome 0:61112ca9193b 35 int tm_isdst; ///<! is daylight savings time.
WiredHome 20:5ca2c94d46b8 36 int tm_tzo_min; ///<! localtime zone offset in minutes (_ex element)
WiredHome 0:61112ca9193b 37 };
WiredHome 0:61112ca9193b 38
WiredHome 11:1d880a50da8a 39 /// TimeInterface class is much like the normal c-style time.h interface, but
WiredHome 11:1d880a50da8a 40 /// is extended with time-zone support, and clock-adjustment support (which
WiredHome 11:1d880a50da8a 41 /// permits tuning the clock) for more accuracy.
WiredHome 11:1d880a50da8a 42 ///
WiredHome 11:1d880a50da8a 43 /// Additionally, strptime was integrated, which can extract the time from
WiredHome 11:1d880a50da8a 44 /// a text string. A formatter is used, so it cannot parse an arbitrary string.
WiredHome 0:61112ca9193b 45 ///
WiredHome 0:61112ca9193b 46 /// Within this class are the normal time.h methods, simply
WiredHome 0:61112ca9193b 47 /// exposed here for one consistent interface.
WiredHome 0:61112ca9193b 48 ///
WiredHome 0:61112ca9193b 49 /// @note This class uses the special battery backed registers
WiredHome 0:61112ca9193b 50 /// GPREG0 and GPREG1 for TimeInterface data.
WiredHome 0:61112ca9193b 51 ///
WiredHome 0:61112ca9193b 52 /// @note In mbed library ver 84, the gmtime method is defective,
WiredHome 0:61112ca9193b 53 /// and calls to this function return junk data. The
WiredHome 0:61112ca9193b 54 /// gmtime method in this library actually uses localtime,
WiredHome 0:61112ca9193b 55 /// but manages the time-zone offset as it does so.
WiredHome 0:61112ca9193b 56 ///
WiredHome 11:1d880a50da8a 57 /// @code
WiredHome 21:f3818e2e0370 58 /// // TimeInterface Architecture and APIs
WiredHome 11:1d880a50da8a 59 /// //
WiredHome 11:1d880a50da8a 60 /// // +--------+
WiredHome 11:1d880a50da8a 61 /// // | clock |----> clock_t clock()
WiredHome 11:1d880a50da8a 62 /// // +--------+
WiredHome 11:1d880a50da8a 63 /// //
WiredHome 11:1d880a50da8a 64 /// // +--------+
WiredHome 11:1d880a50da8a 65 /// // | RTC |<---- setTime(char*, uint16_t, uint32_t)
WiredHome 11:1d880a50da8a 66 /// // | |<---- adjust_sec(int32_t)
WiredHome 11:1d880a50da8a 67 /// // | |<---- set_cal(int32_t)
WiredHome 11:1d880a50da8a 68 /// // | |----> int32_t get_cal()
WiredHome 11:1d880a50da8a 69 /// // | |----> bool get_dst()
WiredHome 17:45dae5a72679 70 /// // | |
WiredHome 11:1d880a50da8a 71 /// // | |<---- set_time(time_t t, int16_t)
WiredHome 11:1d880a50da8a 72 /// // | |----> time_t time(time_t *)
WiredHome 17:45dae5a72679 73 /// // | |------+
WiredHome 17:45dae5a72679 74 /// // +--------+ |
WiredHome 17:45dae5a72679 75 /// // +- | <---- set_dst(char *, char *)
WiredHome 17:45dae5a72679 76 /// // +--------+ | | +----------+
WiredHome 17:45dae5a72679 77 /// // | |<--+ +--|time_local|
WiredHome 17:45dae5a72679 78 /// // |dst_pair|---------| |--------> time_t timelocal(time_t *)
WiredHome 17:45dae5a72679 79 /// // +--------+ +--| |
WiredHome 17:45dae5a72679 80 /// // | +----------+
WiredHome 17:45dae5a72679 81 /// // +--------+ |
WiredHome 17:45dae5a72679 82 /// // | tzo |------+
WiredHome 11:1d880a50da8a 83 /// // | |<---- set_tzo_min(int16_t)
WiredHome 11:1d880a50da8a 84 /// // | |----> int16_t get_tzo_min()
WiredHome 11:1d880a50da8a 85 /// // +--------+
WiredHome 11:1d880a50da8a 86 /// //
WiredHome 11:1d880a50da8a 87 /// // +--------+ +--------------------------+
WiredHome 11:1d880a50da8a 88 /// // | time_t | ---> char * ctime(time_t *) ----> | buffer |
WiredHome 11:1d880a50da8a 89 /// // | value | | Www Mmm dd hh:mm:ss yyyy |
WiredHome 11:1d880a50da8a 90 /// // +--------+ +- char * asctime(tm_ex *) -> +--------------------------+
WiredHome 11:1d880a50da8a 91 /// // ^ | |
WiredHome 11:1d880a50da8a 92 /// // | | | +-----------------+
WiredHome 20:5ca2c94d46b8 93 /// // | | +-------------------------------- | tm_ex |
WiredHome 11:1d880a50da8a 94 /// // | | | .tm_sec |
WiredHome 11:1d880a50da8a 95 /// // | +- tm_ex * gmtime(const time_t *) -----> | .tm_min |
WiredHome 11:1d880a50da8a 96 /// // | | | .tm_hour |
WiredHome 11:1d880a50da8a 97 /// // | +- tm_ex * localtime(const time_t *) --> | .tm_mday |
WiredHome 11:1d880a50da8a 98 /// // | | .tm_mon |
WiredHome 11:1d880a50da8a 99 /// // +---- time_t mktime(struct tm_ex *) ------- | .tm_year |
WiredHome 11:1d880a50da8a 100 /// // | .tm_wday |
WiredHome 11:1d880a50da8a 101 /// // | .tm_yday |
WiredHome 20:5ca2c94d46b8 102 /// // +---------------------------------------------> | .tm_isdst |
WiredHome 20:5ca2c94d46b8 103 /// // | +-------------------------------------------- | .tm_tzo_min |
WiredHome 20:5ca2c94d46b8 104 /// // | | +-----------------+
WiredHome 20:5ca2c94d46b8 105 /// // | | +--------------------------+
WiredHome 20:5ca2c94d46b8 106 /// // | +- strftime(char * ptr, ..., tm_ex *) --> | buffer |
WiredHome 20:5ca2c94d46b8 107 /// // +----strptime(char * buf, ..., tm_ex *) --- | Www Mmm dd hh:mm:ss yyyy |
WiredHome 11:1d880a50da8a 108 /// // +--------------------------+
WiredHome 21:f3818e2e0370 109 /// // double difftime(time_t end, time_t)
WiredHome 21:f3818e2e0370 110 /// //
WiredHome 11:1d880a50da8a 111 /// @endcode
WiredHome 11:1d880a50da8a 112 ///
WiredHome 0:61112ca9193b 113 class TimeInterface
WiredHome 0:61112ca9193b 114 {
WiredHome 0:61112ca9193b 115 public:
WiredHome 11:1d880a50da8a 116 /// Constructor for the TimeInterface class, which does minimal initialization.
WiredHome 11:1d880a50da8a 117 ///
WiredHome 20:5ca2c94d46b8 118 /// @param[in] net is optional and provides the EthernetInterface which is
WiredHome 20:5ca2c94d46b8 119 /// used if you want to sync to an NTP server
WiredHome 20:5ca2c94d46b8 120 ///
WiredHome 21:f3818e2e0370 121 /// @code
WiredHome 21:f3818e2e0370 122 /// EthernetInterface net;
WiredHome 21:f3818e2e0370 123 /// TimeInterface ntp(&net);
WiredHome 21:f3818e2e0370 124 /// ...
WiredHome 21:f3818e2e0370 125 /// ntp.set_tzo_min(-6 * 60);
WiredHome 21:f3818e2e0370 126 /// if (NTP_OK == ntp.setTime("time.nist.gov", 123, 10)) {
WiredHome 21:f3818e2e0370 127 /// time_t tNow = ntp.timelocal();
WiredHome 21:f3818e2e0370 128 /// printf("time is %s\r\n", ntp.ctime(&tNow));
WiredHome 21:f3818e2e0370 129 /// }
WiredHome 21:f3818e2e0370 130 /// ...
WiredHome 21:f3818e2e0370 131 /// @endcode
WiredHome 21:f3818e2e0370 132 ///
WiredHome 20:5ca2c94d46b8 133 TimeInterface(EthernetInterface *m_net = NULL);
WiredHome 0:61112ca9193b 134
WiredHome 11:1d880a50da8a 135 /// Destructor, normally not used, because it is typically kept for the life
WiredHome 11:1d880a50da8a 136 /// of the program.
WiredHome 11:1d880a50da8a 137 ///
WiredHome 0:61112ca9193b 138 ~TimeInterface();
WiredHome 0:61112ca9193b 139
WiredHome 0:61112ca9193b 140 /// Gets the system elapsed time in CLOCKS_PER_SEC tics.
WiredHome 0:61112ca9193b 141 ///
WiredHome 0:61112ca9193b 142 /// Divide the returned value by CLOCKS_PER_SEC to get time in seconds.
WiredHome 0:61112ca9193b 143 ///
WiredHome 0:61112ca9193b 144 /// @code
WiredHome 0:61112ca9193b 145 /// clock_t tstart, tend;
WiredHome 21:f3818e2e0370 146 /// ...
WiredHome 21:f3818e2e0370 147 /// tstart = clock();
WiredHome 21:f3818e2e0370 148 /// // do something long
WiredHome 21:f3818e2e0370 149 /// tend = clock();
WiredHome 21:f3818e2e0370 150 /// printf("Elapsed time is %5.3f\r\n", (float)(tend - tstart)/CLOCKS_PER_SEC);
WiredHome 21:f3818e2e0370 151 /// ...
WiredHome 0:61112ca9193b 152 /// @endcode
WiredHome 0:61112ca9193b 153 ///
WiredHome 0:61112ca9193b 154 /// @returns elapsed tics.
WiredHome 0:61112ca9193b 155 ///
WiredHome 0:61112ca9193b 156 clock_t clock(void);
WiredHome 0:61112ca9193b 157
WiredHome 8:18489e877b0b 158 /// Gets the current time as a UTC time value, optionally writing it
WiredHome 0:61112ca9193b 159 /// to a provided buffer.
WiredHome 0:61112ca9193b 160 ///
WiredHome 8:18489e877b0b 161 /// This reads the real time clock and returns the current UTC time.
WiredHome 0:61112ca9193b 162 ///
WiredHome 0:61112ca9193b 163 /// @code
WiredHome 0:61112ca9193b 164 /// time_t t_ref1, t_ref2, t_ref3;
WiredHome 21:f3818e2e0370 165 /// t_ref1 = time(NULL);
WiredHome 21:f3818e2e0370 166 /// t_ref2 = t.time();
WiredHome 21:f3818e2e0370 167 /// (void)t.time(&t_ref3);
WiredHome 0:61112ca9193b 168 /// @endcode
WiredHome 0:61112ca9193b 169 ///
WiredHome 21:f3818e2e0370 170 /// @param[out] timer is an optional pointer to a time_t value that will
WiredHome 21:f3818e2e0370 171 /// be written with the current time. This pointer is ignored
WiredHome 21:f3818e2e0370 172 /// when NULL.
WiredHome 8:18489e877b0b 173 /// @returns the UTC time value.
WiredHome 0:61112ca9193b 174 ///
WiredHome 0:61112ca9193b 175 time_t time(time_t * timer = NULL);
WiredHome 0:61112ca9193b 176
WiredHome 8:18489e877b0b 177 /// Gets the current time as a LOCAL time value, optionally writing it
WiredHome 1:2ee90f546f54 178 /// to a provided buffer.
WiredHome 1:2ee90f546f54 179 ///
WiredHome 1:2ee90f546f54 180 /// This reads the real time clock and returns the current time, adjusted
WiredHome 21:f3818e2e0370 181 /// for the local time zone and daylight savings time.
WiredHome 1:2ee90f546f54 182 ///
WiredHome 1:2ee90f546f54 183 /// @code
WiredHome 1:2ee90f546f54 184 /// time_t t_ref2, t_ref3;
WiredHome 21:f3818e2e0370 185 /// t_ref2 = t.time();
WiredHome 21:f3818e2e0370 186 /// t.timelocal(&t_ref3);
WiredHome 1:2ee90f546f54 187 /// @endcode
WiredHome 1:2ee90f546f54 188 ///
WiredHome 21:f3818e2e0370 189 /// @param[out] timer is an optional pointer to a time_t value that will
WiredHome 21:f3818e2e0370 190 /// be written. This pointer is ignored when NULL.
WiredHome 21:f3818e2e0370 191 /// @returns the LOCAL time value (UTC adjusted for the LOCAL time zone and dst).
WiredHome 1:2ee90f546f54 192 ///
WiredHome 1:2ee90f546f54 193 time_t timelocal(time_t * timer = NULL);
WiredHome 1:2ee90f546f54 194
WiredHome 21:f3818e2e0370 195 /// Convert a time value structure into an ASCII printable time "Www Mmm dd hh:mm:ss yyyy"
WiredHome 21:f3818e2e0370 196 ///
WiredHome 21:f3818e2e0370 197 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 21:f3818e2e0370 198 /// shared buffer.
WiredHome 21:f3818e2e0370 199 /// @note Unlike the standard ctime function, this version DOES NOT append
WiredHome 21:f3818e2e0370 200 /// a newline character to the buffer.
WiredHome 21:f3818e2e0370 201 ///
WiredHome 21:f3818e2e0370 202 /// @code
WiredHome 21:f3818e2e0370 203 /// time_t tNow = timelocal();
WiredHome 21:f3818e2e0370 204 /// printf("time is %s\r\n", ctime(tNow));
WiredHome 21:f3818e2e0370 205 /// @endcode
WiredHome 21:f3818e2e0370 206 ///
WiredHome 21:f3818e2e0370 207 /// @param[in] timer is a pointer to a time_t value containing the time to convert.
WiredHome 21:f3818e2e0370 208 /// @returns a pointer to a buffer containing the string.
WiredHome 21:f3818e2e0370 209 ///
WiredHome 21:f3818e2e0370 210 char * ctime(const time_t * timer);
WiredHome 21:f3818e2e0370 211
WiredHome 21:f3818e2e0370 212 /// Convert a tm_ex structure into an ASCII printable "time Www Mmm dd hh:mm:ss yyyy"
WiredHome 21:f3818e2e0370 213 ///
WiredHome 21:f3818e2e0370 214 /// @note Unlike the standard asctime, this takes a pointer to a tm_ex, which
WiredHome 21:f3818e2e0370 215 /// has a time zone offset element.
WiredHome 0:61112ca9193b 216 ///
WiredHome 0:61112ca9193b 217 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 0:61112ca9193b 218 /// shared buffer.
WiredHome 0:61112ca9193b 219 ///
WiredHome 0:61112ca9193b 220 /// @note Unlike the standard ctime function, this version DOES NOT append
WiredHome 0:61112ca9193b 221 /// a newline character to the buffer.
WiredHome 0:61112ca9193b 222 ///
WiredHome 14:b5c01a52bff4 223 /// @code
WiredHome 14:b5c01a52bff4 224 /// time_t tNow = timelocal();
WiredHome 14:b5c01a52bff4 225 /// tm_ex * tEx = localtime(&tNow);
WiredHome 14:b5c01a52bff4 226 /// printf("Time is %s\r\n", asctime(tEx));
WiredHome 14:b5c01a52bff4 227 /// @endcode
WiredHome 14:b5c01a52bff4 228 ///
WiredHome 21:f3818e2e0370 229 /// @param[in] timeptr is a pointer to a tm_ex structure containing the time to convert.
WiredHome 0:61112ca9193b 230 /// @returns a pointer to a private buffer containing the string.
WiredHome 0:61112ca9193b 231 ///
WiredHome 0:61112ca9193b 232 char * asctime(const struct tm_ex *timeptr);
WiredHome 0:61112ca9193b 233
WiredHome 0:61112ca9193b 234 /// Compute the difference in seconds between two time values.
WiredHome 0:61112ca9193b 235 ///
WiredHome 21:f3818e2e0370 236 /// @code
WiredHome 21:f3818e2e0370 237 /// time_t tstart, tend;
WiredHome 21:f3818e2e0370 238 /// ...
WiredHome 21:f3818e2e0370 239 /// tstart = time();
WiredHome 21:f3818e2e0370 240 /// // do some long process now
WiredHome 21:f3818e2e0370 241 /// tend = time();
WiredHome 21:f3818e2e0370 242 /// printf("Elapsed time is %5.3f\r\n", tend - tstart);
WiredHome 21:f3818e2e0370 243 /// ...
WiredHome 21:f3818e2e0370 244 /// @endcode
WiredHome 21:f3818e2e0370 245 ///
WiredHome 1:2ee90f546f54 246 /// @param[in] end is the end time to compare to the beginning time.
WiredHome 1:2ee90f546f54 247 /// @param[in] beginning time is compared to the end time.
WiredHome 0:61112ca9193b 248 /// @return the difference in seconds, as a double.
WiredHome 0:61112ca9193b 249 ///
WiredHome 0:61112ca9193b 250 double difftime(time_t end, time_t beginning);
WiredHome 0:61112ca9193b 251
WiredHome 21:f3818e2e0370 252 /// Convert the referenced time_t value to a tm_ex structure in UTC/GMT format.
WiredHome 21:f3818e2e0370 253 ///
WiredHome 21:f3818e2e0370 254 /// @note Unlike the standard asctime, this return a pointer to a tm_ex, which
WiredHome 21:f3818e2e0370 255 /// has a time zone offset element.
WiredHome 0:61112ca9193b 256 ///
WiredHome 0:61112ca9193b 257 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 0:61112ca9193b 258 /// shared buffer.
WiredHome 0:61112ca9193b 259 ///
WiredHome 1:2ee90f546f54 260 /// @param[in] timer is a pointer to a time_t structure to convert.
WiredHome 21:f3818e2e0370 261 /// @returns pointer to a tm_ex structure.
WiredHome 0:61112ca9193b 262 ///
WiredHome 0:61112ca9193b 263 struct tm_ex * gmtime(const time_t * timer);
WiredHome 0:61112ca9193b 264
WiredHome 0:61112ca9193b 265
WiredHome 0:61112ca9193b 266 /// Convert the referenced time_t value to a tm structure in local format.
WiredHome 0:61112ca9193b 267 ///
WiredHome 6:c79cfe750416 268 /// This method leverages the time zone offset applied with @see set_tzo()
WiredHome 6:c79cfe750416 269 /// and the daylight savings time flag applied with @see set_dst().
WiredHome 0:61112ca9193b 270 ///
WiredHome 0:61112ca9193b 271 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 0:61112ca9193b 272 /// shared buffer.
WiredHome 0:61112ca9193b 273 ///
WiredHome 14:b5c01a52bff4 274 /// @code
WiredHome 14:b5c01a52bff4 275 /// time_t tNow = timelocal();
WiredHome 14:b5c01a52bff4 276 /// tm_ex * tEx = localtime(&tNow);
WiredHome 14:b5c01a52bff4 277 /// @endcode
WiredHome 14:b5c01a52bff4 278 ///
WiredHome 1:2ee90f546f54 279 /// @param[in] timer is a pointer to a time_t structure to convert.
WiredHome 0:61112ca9193b 280 /// @returns pointer to a tm structure.
WiredHome 0:61112ca9193b 281 ///
WiredHome 0:61112ca9193b 282 struct tm_ex * localtime(const time_t * timer);
WiredHome 0:61112ca9193b 283
WiredHome 0:61112ca9193b 284 /// Convert a tm_ex structure (an extended time structure) to a time_t
WiredHome 0:61112ca9193b 285 /// value.
WiredHome 0:61112ca9193b 286 ///
WiredHome 1:2ee90f546f54 287 /// @param[in] timeptr is a pointer to a tm_ex structure.
WiredHome 0:61112ca9193b 288 /// @returns the computed time_t value.
WiredHome 0:61112ca9193b 289 ///
WiredHome 0:61112ca9193b 290 time_t mktime(struct tm_ex * timeptr);
WiredHome 0:61112ca9193b 291
WiredHome 0:61112ca9193b 292 /// Presents a time value in a user specified format, into a user specified buffer.
WiredHome 0:61112ca9193b 293 ///
WiredHome 1:2ee90f546f54 294 /// @param[out] ptr is a pointer to the user buffer.
WiredHome 1:2ee90f546f54 295 /// @param[in] maxsize is the size of the user buffer.
WiredHome 1:2ee90f546f54 296 /// @param[in] format is a pointer to the special strftime format specification.
WiredHome 15:82bd8fc6f317 297 /// see format options.
WiredHome 1:2ee90f546f54 298 /// @param[in] timeptr is a pointer to the tm_ex structure.
WiredHome 0:61112ca9193b 299 /// @returns the total number of characters copied into the buffer.
WiredHome 0:61112ca9193b 300 ///
WiredHome 15:82bd8fc6f317 301 /// format options:
WiredHome 15:82bd8fc6f317 302 /// - %%a Abbreviated weekday name e.g. Thu
WiredHome 15:82bd8fc6f317 303 /// - %%A Full weekday name e.g. Thursday
WiredHome 15:82bd8fc6f317 304 /// - %%b Abbreviated month name e.g. Aug
WiredHome 15:82bd8fc6f317 305 /// - %%B Full month name e.g. August
WiredHome 15:82bd8fc6f317 306 /// - %%c Date and time representation e.g. Thu Aug 23 14:55:02 2001
WiredHome 15:82bd8fc6f317 307 /// - %%C Year divided by 100 and truncated to integer (00-99) e.g. 20
WiredHome 15:82bd8fc6f317 308 /// - %%d Day of the month, zero-padded (01-31) e.g. 23
WiredHome 15:82bd8fc6f317 309 /// - %%D Short MM/DD/YY date, equivalent to %%m/%%d/%%y e.g. 08/23/01
WiredHome 15:82bd8fc6f317 310 /// - %%e Day of the month, space-padded ( 1-31) e.g. 23
WiredHome 15:82bd8fc6f317 311 /// - %%F Short YYYY-MM-DD date, equivalent to %%Y-%%m-%%d e.g. 2001-08-23
WiredHome 15:82bd8fc6f317 312 /// - %%g Week-based year, last two digits (00-99) e.g. 01
WiredHome 15:82bd8fc6f317 313 /// - %%G Week-based year e.g. 2001
WiredHome 15:82bd8fc6f317 314 /// - %%h Abbreviated month name * (same as %%b) e.g. Aug
WiredHome 15:82bd8fc6f317 315 /// - %%H Hour in 24h format (00-23) e.g. 14
WiredHome 15:82bd8fc6f317 316 /// - %%I Hour in 12h format (01-12) e.g. 02
WiredHome 15:82bd8fc6f317 317 /// - %%j Day of the year (001-366) e.g. 235
WiredHome 15:82bd8fc6f317 318 /// - %%m Month as a decimal number (01-12) e.g. 08
WiredHome 15:82bd8fc6f317 319 /// - %%M Minute (00-59) e.g. 55
WiredHome 15:82bd8fc6f317 320 /// - %%n New-line character ('\\n')
WiredHome 15:82bd8fc6f317 321 /// - %%p AM or PM designation e.g. PM
WiredHome 15:82bd8fc6f317 322 /// - %%r 12-hour clock time e.g. 02:55:02 pm
WiredHome 15:82bd8fc6f317 323 /// - %%R 24-hour HH:MM time, equivalent to %%H:%%M e.g. 14:55
WiredHome 15:82bd8fc6f317 324 /// - %%S Second (00-61) e.g. 02
WiredHome 15:82bd8fc6f317 325 /// - %%t Horizontal-tab character ('\t')
WiredHome 15:82bd8fc6f317 326 /// - %%T ISO 8601 time format (HH:MM:SS), equivalent to %%H:%%M:%%S e.g. 14:55:02
WiredHome 15:82bd8fc6f317 327 /// - %%u ISO 8601 weekday as number with Monday as 1 (1-7) e.g. 4
WiredHome 15:82bd8fc6f317 328 /// - %%U Week number with the first Sunday as the first day of week one (00-53) e.g. 33
WiredHome 15:82bd8fc6f317 329 /// - %%V ISO 8601 week number (00-53) e.g. 34
WiredHome 15:82bd8fc6f317 330 /// - %%w Weekday as a decimal number with Sunday as 0 (0-6) e.g. 4
WiredHome 15:82bd8fc6f317 331 /// - %%W Week number with the first Monday as the first day of week one (00-53) e.g. 34
WiredHome 15:82bd8fc6f317 332 /// - %%x Date representation e.g. 08/23/01
WiredHome 15:82bd8fc6f317 333 /// - %%X Time representation e.g. 14:55:02
WiredHome 15:82bd8fc6f317 334 /// - %%y Year, last two digits (00-99) e.g. 01
WiredHome 15:82bd8fc6f317 335 /// - %%Y Year e.g. 2001
WiredHome 15:82bd8fc6f317 336 /// - %%z ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100) (e.g. +100)
WiredHome 15:82bd8fc6f317 337 /// If timezone cannot be determined, no characters
WiredHome 15:82bd8fc6f317 338 /// - %%Z Timezone name or abbreviation (e.g. CDT)
WiredHome 15:82bd8fc6f317 339 /// If timezone cannot be determined, no characters
WiredHome 15:82bd8fc6f317 340 /// - % A % sign
WiredHome 15:82bd8fc6f317 341 ///
WiredHome 0:61112ca9193b 342 size_t strftime(char * ptr, size_t maxsize, const char * format, const struct tm_ex * timeptr);
WiredHome 0:61112ca9193b 343
WiredHome 10:5734dbc2f5cc 344
WiredHome 10:5734dbc2f5cc 345 /// Convert a string, in a defined format, to a time value in a tm_ex structure.
WiredHome 10:5734dbc2f5cc 346 ///
WiredHome 10:5734dbc2f5cc 347 /// Most format details leveraged from The Open Group Base Specifications Issue 6
WiredHome 10:5734dbc2f5cc 348 /// IEEE Std 1003.1, 2004 Edition
WiredHome 10:5734dbc2f5cc 349 /// Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.
WiredHome 10:5734dbc2f5cc 350 ///
WiredHome 10:5734dbc2f5cc 351 /// Modifications for mbed, and addition of the timezone format option by D. Smart
WiredHome 10:5734dbc2f5cc 352 ///
WiredHome 10:5734dbc2f5cc 353 /// @code
WiredHome 10:5734dbc2f5cc 354 /// char timesample[] = "Jan 22 2017 01:32:48 UTC";
WiredHome 10:5734dbc2f5cc 355 /// tm_ex tm;
WiredHome 10:5734dbc2f5cc 356 /// strptime(timesample, "%b %d %Y %H:%M:%S %Z", &tm);
WiredHome 10:5734dbc2f5cc 357 /// @endcode
WiredHome 10:5734dbc2f5cc 358 ///
WiredHome 10:5734dbc2f5cc 359 /// @param[in] buf is a pointer to the string to be parsed.
WiredHome 10:5734dbc2f5cc 360 /// @param[in] format is a pointer to a format string. See the format options.
WiredHome 10:5734dbc2f5cc 361 /// @param[out] tm is a pointer to a tm_ex struct.
WiredHome 10:5734dbc2f5cc 362 /// @returns a pointer to the character following the last one parsed, or null on failure
WiredHome 10:5734dbc2f5cc 363 ///
WiredHome 10:5734dbc2f5cc 364 /// format options:
WiredHome 10:5734dbc2f5cc 365 /// - %%a The day of the week, using the locale's weekday names; either the abbreviated or
WiredHome 10:5734dbc2f5cc 366 /// full name may be specified.
WiredHome 10:5734dbc2f5cc 367 /// - %%A Equivalent to %%a.
WiredHome 10:5734dbc2f5cc 368 /// - %%b The month, using the locale's month names; either the abbreviated or full name
WiredHome 10:5734dbc2f5cc 369 /// may be specified.
WiredHome 10:5734dbc2f5cc 370 /// - %%B Equivalent to %%b.
WiredHome 10:5734dbc2f5cc 371 /// - %%c Replaced by the locale's appropriate date and time representation.
WiredHome 10:5734dbc2f5cc 372 /// - %%C The century number [00,99]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 373 /// - %%d The day of the month [01,31]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 374 /// - %%D The date as %%m / %%d / %%y.
WiredHome 10:5734dbc2f5cc 375 /// - %%e Equivalent to %%d.
WiredHome 10:5734dbc2f5cc 376 /// - %%h Equivalent to %%b.
WiredHome 10:5734dbc2f5cc 377 /// - %%H The hour (24-hour clock) [00,23]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 378 /// - %%I The hour (12-hour clock) [01,12]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 379 /// - %%j The day number of the year [001,366]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 380 /// - %%m The month number [01,12]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 381 /// - %%M The minute [00,59]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 382 /// - %%n Any white space.
WiredHome 10:5734dbc2f5cc 383 /// - %%p The locale's equivalent of a.m or p.m.
WiredHome 10:5734dbc2f5cc 384 /// - %%r 12-hour clock time using the AM/PM notation if t_fmt_ampm is not an empty string
WiredHome 10:5734dbc2f5cc 385 /// in the LC_TIME portion of the current locale; in the POSIX locale, this shall be
WiredHome 10:5734dbc2f5cc 386 /// equivalent to %%I : %%M : %%S %%p.
WiredHome 10:5734dbc2f5cc 387 /// - %%R The time as %%H : %%M.
WiredHome 10:5734dbc2f5cc 388 /// - %%S The seconds [00,60]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 389 /// - %%t Any white space.
WiredHome 10:5734dbc2f5cc 390 /// - %%T The time as %%H : %%M : %%S.
WiredHome 10:5734dbc2f5cc 391 /// - %%U The week number of the year (Sunday as the first day of the week) as a decimal
WiredHome 10:5734dbc2f5cc 392 /// number [00,53]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 393 /// - %%w The weekday as a decimal number [0,6], with 0 representing Sunday; leading zeros
WiredHome 10:5734dbc2f5cc 394 /// are permitted but not required.
WiredHome 10:5734dbc2f5cc 395 /// - %%W The week number of the year (Monday as the first day of the week) as a decimal
WiredHome 10:5734dbc2f5cc 396 /// number [00,53]; leading zeros are permitted but not required.
WiredHome 10:5734dbc2f5cc 397 /// - %%x The date, using the locale's date format.
WiredHome 10:5734dbc2f5cc 398 /// - %%X The time, using the locale's time format.
WiredHome 10:5734dbc2f5cc 399 /// - %%y The year within century. When a century is not otherwise specified, values in
WiredHome 10:5734dbc2f5cc 400 /// the range [69,99] shall refer to years 1969 to 1999 inclusive, and values in the
WiredHome 10:5734dbc2f5cc 401 /// range [00,68] shall refer to years 2000 to 2068 inclusive; leading zeros shall be
WiredHome 10:5734dbc2f5cc 402 /// permitted but shall not be required.
WiredHome 10:5734dbc2f5cc 403 /// Note: It is expected that in a future version of IEEE Std 1003.1-2001
WiredHome 10:5734dbc2f5cc 404 /// the default century inferred from a 2-digit year will change.
WiredHome 10:5734dbc2f5cc 405 /// (This would apply to all commands accepting a 2-digit year as input.)
WiredHome 10:5734dbc2f5cc 406 /// - %%Y The year, including the century (for example, 1988).
WiredHome 10:5734dbc2f5cc 407 /// - %%Z The timezone offset, as a 3-letter sequence. Only a few whole-hour offsets
WiredHome 10:5734dbc2f5cc 408 /// have been defined.
WiredHome 11:1d880a50da8a 409 /// - %% Replaced by %.
WiredHome 10:5734dbc2f5cc 410 ///
WiredHome 10:5734dbc2f5cc 411 const char * strptime(const char *buf, char *fmt, struct tm_ex *tm);
WiredHome 10:5734dbc2f5cc 412
WiredHome 10:5734dbc2f5cc 413
WiredHome 0:61112ca9193b 414 // time zone functions
WiredHome 0:61112ca9193b 415
WiredHome 8:18489e877b0b 416 /// Set the internal RTC (clock) to the time value.
WiredHome 8:18489e877b0b 417 ///
WiredHome 8:18489e877b0b 418 /// The time valueshould be UTC time along with an offset of zero,
WiredHome 8:18489e877b0b 419 /// which then permits gmtime and localtime to be used appropriately.
WiredHome 8:18489e877b0b 420 /// Alternately, the time can be in localtime, and the offset is then
WiredHome 8:18489e877b0b 421 /// used to compute UTC to set the clock.
WiredHome 0:61112ca9193b 422 ///
WiredHome 2:65e0a25c7551 423 /// @param[in] t should be the UTC time value to set the clock to. If the available
WiredHome 0:61112ca9193b 424 /// time value is local time, the optional time zone offset can
WiredHome 2:65e0a25c7551 425 /// be provided so the system clock is UTC.
WiredHome 1:2ee90f546f54 426 /// @param[in] tzo is the optional time zone offset in minutes when it is in
WiredHome 0:61112ca9193b 427 /// the range of -720 to +720 (-12 hours to + 12 hours). Any
WiredHome 0:61112ca9193b 428 /// other value is illegal and no change will be made.
WiredHome 0:61112ca9193b 429 ///
WiredHome 0:61112ca9193b 430 void set_time(time_t t, int16_t tzo_min = 0);
WiredHome 0:61112ca9193b 431
WiredHome 0:61112ca9193b 432 /// Set the time zone offset in minutes.
WiredHome 0:61112ca9193b 433 ///
WiredHome 0:61112ca9193b 434 /// This API should be used before any other methods that fetch
WiredHome 0:61112ca9193b 435 /// the RTC info.
WiredHome 0:61112ca9193b 436 ///
WiredHome 1:2ee90f546f54 437 /// @param[in] tzo is the time zone offset in minutes when it is in
WiredHome 0:61112ca9193b 438 /// the range of -720 to +720 (-12 hours to + 12 hours). Any
WiredHome 0:61112ca9193b 439 /// other value is illegal and no change will be made.
WiredHome 0:61112ca9193b 440 ///
WiredHome 0:61112ca9193b 441 void set_tzo_min(int16_t tzo_min);
WiredHome 0:61112ca9193b 442
WiredHome 0:61112ca9193b 443 /// Get the time zone offset in minutes.
WiredHome 0:61112ca9193b 444 ///
WiredHome 0:61112ca9193b 445 /// @returns the time zone offset value in minutes. If the tzo was
WiredHome 0:61112ca9193b 446 /// never initialized, this returns zero.
WiredHome 0:61112ca9193b 447 ///
WiredHome 0:61112ca9193b 448 int16_t get_tzo_min(void);
WiredHome 0:61112ca9193b 449
WiredHome 3:49f36b489b64 450 /// Set the clock for local time to report whether the current
WiredHome 3:49f36b489b64 451 /// mode is standard or daylight savings time.
WiredHome 3:49f36b489b64 452 ///
WiredHome 3:49f36b489b64 453 /// return values for localtime will then be adjusted not only
WiredHome 3:49f36b489b64 454 /// for the time zone offset, but for dst.
WiredHome 3:49f36b489b64 455 ///
WiredHome 3:49f36b489b64 456 /// @param[in] dst is a boolean that should be set when dst is
WiredHome 3:49f36b489b64 457 /// the active mode.
WiredHome 6:c79cfe750416 458 /// @returns true, always.
WiredHome 3:49f36b489b64 459 ///
WiredHome 6:c79cfe750416 460 bool set_dst(bool dst);
WiredHome 6:c79cfe750416 461
WiredHome 6:c79cfe750416 462 /// Set the clock for auto-adjust local time based on
WiredHome 6:c79cfe750416 463 /// changing to standard or daylight savings time.
WiredHome 6:c79cfe750416 464 ///
WiredHome 6:c79cfe750416 465 /// return values for localtime will then be adjusted not only
WiredHome 6:c79cfe750416 466 /// for the time zone offset, but for dst.
WiredHome 6:c79cfe750416 467 ///
WiredHome 6:c79cfe750416 468 /// @param[in] dstStart is a string of the form "mm/dd,hh:mm"
WiredHome 6:c79cfe750416 469 /// representing when DST starts.
WiredHome 6:c79cfe750416 470 /// @param[in] dstStop is a string of the form "mm/dd,hh:mm"
WiredHome 6:c79cfe750416 471 /// representing when DST stops.
WiredHome 6:c79cfe750416 472 /// @returns true if the start and stop pair could be successfully
WiredHome 6:c79cfe750416 473 /// parsed.
WiredHome 6:c79cfe750416 474 ///
WiredHome 6:c79cfe750416 475 bool set_dst(const char * dstStart, const char * dstStop);
WiredHome 3:49f36b489b64 476
WiredHome 3:49f36b489b64 477 /// Get the current clock mode for daylight savings time.
WiredHome 3:49f36b489b64 478 ///
WiredHome 3:49f36b489b64 479 /// @returns true if clock is in dst mode.
WiredHome 3:49f36b489b64 480 ///
WiredHome 3:49f36b489b64 481 bool get_dst(void);
WiredHome 3:49f36b489b64 482
WiredHome 0:61112ca9193b 483 /// Get the time value when the clock was last set. This is most
WiredHome 0:61112ca9193b 484 /// often used in calibration of the clock.
WiredHome 0:61112ca9193b 485 ///
WiredHome 0:61112ca9193b 486 /// @returns time last set as a UTC time value.
WiredHome 0:61112ca9193b 487 ///
WiredHome 0:61112ca9193b 488 time_t get_timelastset(void);
WiredHome 0:61112ca9193b 489
WiredHome 0:61112ca9193b 490 /// get_cal will return the calibration register value
WiredHome 0:61112ca9193b 491 ///
WiredHome 0:61112ca9193b 492 /// This is the raw register value as a signed 32-bit value (even though
WiredHome 0:61112ca9193b 493 /// it is actually a 17-bit unsigned value with an additional 'direction' flag).
WiredHome 0:61112ca9193b 494 ///
WiredHome 0:61112ca9193b 495 /// @returns calibration settings ranging from -131071 to +131071
WiredHome 0:61112ca9193b 496 ///
WiredHome 0:61112ca9193b 497 int32_t get_cal();
WiredHome 0:61112ca9193b 498
WiredHome 0:61112ca9193b 499 /// set_cal will set the calibration register value
WiredHome 0:61112ca9193b 500 ///
WiredHome 0:61112ca9193b 501 /// This accepts a signed value to be used to set the calibration
WiredHome 0:61112ca9193b 502 /// registers. Setting the calibration value to zero disables the
WiredHome 0:61112ca9193b 503 /// calibration function.
WiredHome 0:61112ca9193b 504 ///
WiredHome 0:61112ca9193b 505 /// It is important to know the register function in order to use
WiredHome 0:61112ca9193b 506 /// this command, and this API is normally not used by external
WiredHome 0:61112ca9193b 507 /// application code. @See AdjustBySeconds for a user-friendly
WiredHome 0:61112ca9193b 508 /// API.
WiredHome 0:61112ca9193b 509 ///
WiredHome 1:2ee90f546f54 510 /// @param[in] calibration value to use ranging from -131071 to +131071
WiredHome 0:61112ca9193b 511 /// @returns nothing
WiredHome 0:61112ca9193b 512 ///
WiredHome 0:61112ca9193b 513 void set_cal(int32_t calibration);
WiredHome 0:61112ca9193b 514
WiredHome 0:61112ca9193b 515 /// adjust_sec adjusts both the time and the calibration by seconds
WiredHome 0:61112ca9193b 516 ///
WiredHome 0:61112ca9193b 517 /// This will take a signed value, which is the current adjustment in seconds
WiredHome 0:61112ca9193b 518 /// to put the clock on the correct time. So, if the clock is behind by
WiredHome 0:61112ca9193b 519 /// 3 seconds, the value should be +3 to advance the clock accordingly.
WiredHome 0:61112ca9193b 520 /// It will then adjust the time, and it will attempt to adjust the
WiredHome 0:61112ca9193b 521 /// calibration factor to make the time more accurate.
WiredHome 0:61112ca9193b 522 ///
WiredHome 0:61112ca9193b 523 /// The adjustment can only be made if it has retained when the clock was
WiredHome 0:61112ca9193b 524 /// last set, in order to know by how much to adjust it. It is also most
WiredHome 0:61112ca9193b 525 /// accurate if several days have elapsed since the time was set.
WiredHome 0:61112ca9193b 526 ///
WiredHome 0:61112ca9193b 527 /// @note The current version only works if the calibration value
WiredHome 0:61112ca9193b 528 /// is zero when this adjustment is made.
WiredHome 0:61112ca9193b 529 ///
WiredHome 1:2ee90f546f54 530 /// @param[in] adjustSeconds is the signed value by which to adjust the time to
WiredHome 0:61112ca9193b 531 /// correct it to the current actual time.
WiredHome 0:61112ca9193b 532 /// @returns true if the adjustment was made
WiredHome 0:61112ca9193b 533 /// @returns false if the adjustment could not be made
WiredHome 0:61112ca9193b 534 ///
WiredHome 0:61112ca9193b 535 bool adjust_sec(int32_t adjustSeconds);
WiredHome 0:61112ca9193b 536
WiredHome 2:65e0a25c7551 537 /// Set the clock from an internet source (blocking)
WiredHome 2:65e0a25c7551 538 ///
WiredHome 2:65e0a25c7551 539 /// This function is the interface to NTPClient.
WiredHome 2:65e0a25c7551 540 /// Blocks until completion
WiredHome 2:65e0a25c7551 541 ///
WiredHome 2:65e0a25c7551 542 /// @param[in] host NTP server IPv4 address or hostname (will be resolved via DNS)
WiredHome 2:65e0a25c7551 543 /// @param[in] port port to use; defaults to 123
WiredHome 2:65e0a25c7551 544 /// @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 21:f3818e2e0370 545 /// @returns NTP_OK on success,
WiredHome 21:f3818e2e0370 546 /// @returns NTP_CONN if no network interface
WiredHome 21:f3818e2e0370 547 /// @returns other NTP error code (<0) on failure
WiredHome 2:65e0a25c7551 548 ///
WiredHome 2:65e0a25c7551 549 NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT);
WiredHome 2:65e0a25c7551 550
WiredHome 2:65e0a25c7551 551 // ntp interface functions
WiredHome 0:61112ca9193b 552 private:
WiredHome 20:5ca2c94d46b8 553 EthernetInterface * m_net;
WiredHome 20:5ca2c94d46b8 554
WiredHome 6:c79cfe750416 555 typedef struct {
WiredHome 6:c79cfe750416 556 uint8_t MM;
WiredHome 6:c79cfe750416 557 uint8_t DD;
WiredHome 6:c79cfe750416 558 uint8_t hh;
WiredHome 6:c79cfe750416 559 uint8_t mm;
WiredHome 6:c79cfe750416 560 } dst_event_t;
WiredHome 6:c79cfe750416 561 typedef struct {
WiredHome 6:c79cfe750416 562 dst_event_t dst_start;
WiredHome 6:c79cfe750416 563 dst_event_t dst_stop;
WiredHome 6:c79cfe750416 564 } dst_event_pair_t;
WiredHome 6:c79cfe750416 565
WiredHome 6:c79cfe750416 566 bool parseDSTstring(dst_event_t * result, const char * dstr);
WiredHome 6:c79cfe750416 567
WiredHome 6:c79cfe750416 568 /// Performs a "simple" computation of two dates into minutes.
WiredHome 6:c79cfe750416 569 ///
WiredHome 6:c79cfe750416 570 /// Does not account for leap years or which month it is. Is
WiredHome 6:c79cfe750416 571 /// useful only for comparing which date/time came first, not for
WiredHome 6:c79cfe750416 572 /// computing the difference between them.
WiredHome 6:c79cfe750416 573 ///
WiredHome 6:c79cfe750416 574 /// @return "normalized" minutes since Jan 1 00:00.
WiredHome 6:c79cfe750416 575 ///
WiredHome 6:c79cfe750416 576 uint32_t minutesSinceJan(int mon, int day, int hr, int min);
WiredHome 6:c79cfe750416 577
WiredHome 6:c79cfe750416 578 dst_event_pair_t dst_pair;
WiredHome 6:c79cfe750416 579 bool dst; // true in dst mode
WiredHome 0:61112ca9193b 580 char result[30]; // holds the converted to text time string
WiredHome 0:61112ca9193b 581 time_t tresult; // holds the converted time structure.
WiredHome 0:61112ca9193b 582 struct tm_ex tm_ext;
WiredHome 0:61112ca9193b 583 };
WiredHome 0:61112ca9193b 584
WiredHome 11:1d880a50da8a 585 #endif // TIMEINTERFACE_H