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:
Sun Jan 22 04:06:16 2017 +0000
Revision:
10:5734dbc2f5cc
Parent:
8:18489e877b0b
Child:
11:1d880a50da8a
Added strptime, with a custom tzo enhancement

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