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