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 Jun 22 21:00:01 2014 +0000
Revision:
2:65e0a25c7551
Parent:
1:2ee90f546f54
Child:
3:49f36b489b64
Revised the TimeInterface class to use NTP as is, not to modify it.

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 0:61112ca9193b 5
WiredHome 2:65e0a25c7551 6 #include "NTPClient.h" // ver 4 Donatien Garnier
WiredHome 2:65e0a25c7551 7
WiredHome 0:61112ca9193b 8 // Special Registers and their usage:
WiredHome 0:61112ca9193b 9 // GPREG0: 32 bits
WiredHome 0:61112ca9193b 10 // low word: time zone offset (-720 to +720)
WiredHome 0:61112ca9193b 11 // high word: 2's complement of low word for integrity checking
WiredHome 0:61112ca9193b 12 // GPREG1: 32 bits
WiredHome 0:61112ca9193b 13 // time_t value when the clock was last set
WiredHome 0:61112ca9193b 14
WiredHome 0:61112ca9193b 15
WiredHome 0:61112ca9193b 16 extern "C" {
WiredHome 0:61112ca9193b 17 #include "time.h"
WiredHome 0:61112ca9193b 18 }
WiredHome 0:61112ca9193b 19
WiredHome 0:61112ca9193b 20 struct tm_ex
WiredHome 0:61112ca9193b 21 {
WiredHome 0:61112ca9193b 22 int tm_sec; ///<! seconds, 0 to 59.
WiredHome 0:61112ca9193b 23 int tm_min; ///<! minutes, 0 to 59.
WiredHome 0:61112ca9193b 24 int tm_hour; ///<! hours, 0 to 23.
WiredHome 0:61112ca9193b 25 int tm_mday; ///<! monthday 1 to 31.
WiredHome 0:61112ca9193b 26 int tm_mon; ///<! month 0 to 11.
WiredHome 0:61112ca9193b 27 int tm_year; ///<! years since 1900.
WiredHome 0:61112ca9193b 28 int tm_wday; ///<! days since sunday 0 to 6.
WiredHome 0:61112ca9193b 29 int tm_yday; ///<! days since 1 Jan 0 to 365.
WiredHome 0:61112ca9193b 30 int tm_isdst; ///<! is daylight savings time.
WiredHome 0:61112ca9193b 31 int tm_tzo_min; ///<! localtime zone offset in minutes
WiredHome 0:61112ca9193b 32 };
WiredHome 0:61112ca9193b 33
WiredHome 0:61112ca9193b 34 /// TimeInterface class is much like the normal c-style time.h
WiredHome 0:61112ca9193b 35 /// interface, but is extended with time-zone support, and
WiredHome 0:61112ca9193b 36 /// clock-adjustment support (which permits tuning the clock)
WiredHome 0:61112ca9193b 37 /// for more accuracy.
WiredHome 0:61112ca9193b 38 ///
WiredHome 0:61112ca9193b 39 /// Within this class are the normal time.h methods, simply
WiredHome 0:61112ca9193b 40 /// exposed here for one consistent interface.
WiredHome 0:61112ca9193b 41 ///
WiredHome 0:61112ca9193b 42 /// @note This class uses the special battery backed registers
WiredHome 0:61112ca9193b 43 /// GPREG0 and GPREG1 for TimeInterface data.
WiredHome 0:61112ca9193b 44 ///
WiredHome 0:61112ca9193b 45 /// @note In mbed library ver 84, the gmtime method is defective,
WiredHome 0:61112ca9193b 46 /// and calls to this function return junk data. The
WiredHome 0:61112ca9193b 47 /// gmtime method in this library actually uses localtime,
WiredHome 0:61112ca9193b 48 /// but manages the time-zone offset as it does so.
WiredHome 0:61112ca9193b 49 ///
WiredHome 0:61112ca9193b 50 class TimeInterface
WiredHome 0:61112ca9193b 51 {
WiredHome 0:61112ca9193b 52 public:
WiredHome 0:61112ca9193b 53 TimeInterface();
WiredHome 0:61112ca9193b 54
WiredHome 0:61112ca9193b 55 ~TimeInterface();
WiredHome 0:61112ca9193b 56
WiredHome 0:61112ca9193b 57 /// Gets the system elapsed time in CLOCKS_PER_SEC tics.
WiredHome 0:61112ca9193b 58 ///
WiredHome 0:61112ca9193b 59 /// Divide the returned value by CLOCKS_PER_SEC to get time in seconds.
WiredHome 0:61112ca9193b 60 ///
WiredHome 0:61112ca9193b 61 /// @code
WiredHome 0:61112ca9193b 62 /// clock_t tstart, tend;
WiredHome 0:61112ca9193b 63 /// tstart = clock();
WiredHome 0:61112ca9193b 64 /// // do something long
WiredHome 0:61112ca9193b 65 /// tend = clock();
WiredHome 0:61112ca9193b 66 /// printf("Elapsed time is %5.3f\r\n", (float)(tend - tstart)/CLOCKS_PER_SEC);
WiredHome 0:61112ca9193b 67 /// @endcode
WiredHome 0:61112ca9193b 68 ///
WiredHome 0:61112ca9193b 69 /// @returns elapsed tics.
WiredHome 0:61112ca9193b 70 ///
WiredHome 0:61112ca9193b 71 clock_t clock(void);
WiredHome 0:61112ca9193b 72
WiredHome 0:61112ca9193b 73 /// Gets the current time as a time value, optionally writing it
WiredHome 0:61112ca9193b 74 /// to a provided buffer.
WiredHome 0:61112ca9193b 75 ///
WiredHome 0:61112ca9193b 76 /// This reads the real time clock and returns the current time.
WiredHome 0:61112ca9193b 77 ///
WiredHome 0:61112ca9193b 78 /// @code
WiredHome 0:61112ca9193b 79 /// time_t t_ref1, t_ref2, t_ref3;
WiredHome 0:61112ca9193b 80 /// t_ref1 = time(NULL); t_ref2 = t.time(); t.time(&t_ref3);
WiredHome 0:61112ca9193b 81 /// @endcode
WiredHome 0:61112ca9193b 82 ///
WiredHome 1:2ee90f546f54 83 /// @param[inout] timer is an optional pointer to a time_t value that will be written.
WiredHome 0:61112ca9193b 84 /// This pointer is ignored when NULL.
WiredHome 0:61112ca9193b 85 /// @returns time value.
WiredHome 0:61112ca9193b 86 ///
WiredHome 0:61112ca9193b 87 time_t time(time_t * timer = NULL);
WiredHome 0:61112ca9193b 88
WiredHome 1:2ee90f546f54 89 /// Gets the current local time as a time value, optionally writing it
WiredHome 1:2ee90f546f54 90 /// to a provided buffer.
WiredHome 1:2ee90f546f54 91 ///
WiredHome 1:2ee90f546f54 92 /// This reads the real time clock and returns the current time, adjusted
WiredHome 1:2ee90f546f54 93 /// for the local timezone.
WiredHome 1:2ee90f546f54 94 ///
WiredHome 1:2ee90f546f54 95 /// @code
WiredHome 1:2ee90f546f54 96 /// time_t t_ref2, t_ref3;
WiredHome 1:2ee90f546f54 97 /// t_ref2 = t.time(); t.timelocal(&t_ref3);
WiredHome 1:2ee90f546f54 98 /// @endcode
WiredHome 1:2ee90f546f54 99 ///
WiredHome 1:2ee90f546f54 100 /// @param[inout] timer is an optional pointer to a time_t value that will be written.
WiredHome 1:2ee90f546f54 101 /// This pointer is ignored when NULL.
WiredHome 1:2ee90f546f54 102 /// @returns the time value adjusted for the local time zone.
WiredHome 1:2ee90f546f54 103 ///
WiredHome 1:2ee90f546f54 104 time_t timelocal(time_t * timer = NULL);
WiredHome 1:2ee90f546f54 105
WiredHome 0:61112ca9193b 106 /// Convert a time value structure into an ASCII printable time Www Mmm dd hh:mm:ss yyyy
WiredHome 0:61112ca9193b 107 ///
WiredHome 0:61112ca9193b 108 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 0:61112ca9193b 109 /// shared buffer.
WiredHome 0:61112ca9193b 110 /// @note Unlike the standard ctime function, this version DOES NOT append
WiredHome 0:61112ca9193b 111 /// a newline character to the buffer.
WiredHome 0:61112ca9193b 112 ///
WiredHome 1:2ee90f546f54 113 /// @param[in] timeptr is a pointer to a tm structure containing the time to convert.
WiredHome 0:61112ca9193b 114 /// @returns a pointer to a private buffer containing the string.
WiredHome 0:61112ca9193b 115 ///
WiredHome 0:61112ca9193b 116 char * ctime(const time_t * timer);
WiredHome 0:61112ca9193b 117
WiredHome 0:61112ca9193b 118 /// Convert a tm structure into an ASCII printable time Www Mmm dd hh:mm:ss yyyy
WiredHome 0:61112ca9193b 119 ///
WiredHome 0:61112ca9193b 120 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 0:61112ca9193b 121 /// shared buffer.
WiredHome 0:61112ca9193b 122 /// @note Unlike the standard ctime function, this version DOES NOT append
WiredHome 0:61112ca9193b 123 /// a newline character to the buffer.
WiredHome 0:61112ca9193b 124 ///
WiredHome 1:2ee90f546f54 125 /// @param[in] timeptr is a pointer to a tm structure containing the time to convert.
WiredHome 0:61112ca9193b 126 /// @returns a pointer to a private buffer containing the string.
WiredHome 0:61112ca9193b 127 ///
WiredHome 0:61112ca9193b 128 char * asctime(const struct tm_ex *timeptr);
WiredHome 0:61112ca9193b 129
WiredHome 0:61112ca9193b 130 /// Compute the difference in seconds between two time values.
WiredHome 0:61112ca9193b 131 ///
WiredHome 1:2ee90f546f54 132 /// @param[in] end is the end time to compare to the beginning time.
WiredHome 1:2ee90f546f54 133 /// @param[in] beginning time is compared to the end time.
WiredHome 0:61112ca9193b 134 /// @return the difference in seconds, as a double.
WiredHome 0:61112ca9193b 135 ///
WiredHome 0:61112ca9193b 136 double difftime(time_t end, time_t beginning);
WiredHome 0:61112ca9193b 137
WiredHome 0:61112ca9193b 138 /// Convert the referenced time_t value to a tm structure in UTC/GMT format.
WiredHome 0:61112ca9193b 139 ///
WiredHome 0:61112ca9193b 140 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 0:61112ca9193b 141 /// shared buffer.
WiredHome 0:61112ca9193b 142 ///
WiredHome 1:2ee90f546f54 143 /// @param[in] timer is a pointer to a time_t structure to convert.
WiredHome 0:61112ca9193b 144 /// @returns pointer to a tm structure.
WiredHome 0:61112ca9193b 145 ///
WiredHome 0:61112ca9193b 146 struct tm_ex * gmtime(const time_t * timer);
WiredHome 0:61112ca9193b 147
WiredHome 0:61112ca9193b 148
WiredHome 0:61112ca9193b 149 /// Convert the referenced time_t value to a tm structure in local format.
WiredHome 0:61112ca9193b 150 ///
WiredHome 0:61112ca9193b 151 /// This method leverages the time zone offset applied with @see set_tzo().
WiredHome 0:61112ca9193b 152 ///
WiredHome 0:61112ca9193b 153 /// @note Watch out for race conditions as this returns a pointer to a
WiredHome 0:61112ca9193b 154 /// shared buffer.
WiredHome 0:61112ca9193b 155 ///
WiredHome 1:2ee90f546f54 156 /// @param[in] timer is a pointer to a time_t structure to convert.
WiredHome 0:61112ca9193b 157 /// @returns pointer to a tm structure.
WiredHome 0:61112ca9193b 158 ///
WiredHome 0:61112ca9193b 159 struct tm_ex * localtime(const time_t * timer);
WiredHome 0:61112ca9193b 160
WiredHome 0:61112ca9193b 161 /// Convert a tm_ex structure (an extended time structure) to a time_t
WiredHome 0:61112ca9193b 162 /// value.
WiredHome 0:61112ca9193b 163 ///
WiredHome 1:2ee90f546f54 164 /// @param[in] timeptr is a pointer to a tm_ex structure.
WiredHome 0:61112ca9193b 165 /// @returns the computed time_t value.
WiredHome 0:61112ca9193b 166 ///
WiredHome 0:61112ca9193b 167 time_t mktime(struct tm_ex * timeptr);
WiredHome 0:61112ca9193b 168
WiredHome 0:61112ca9193b 169 /// Presents a time value in a user specified format, into a user specified buffer.
WiredHome 0:61112ca9193b 170 ///
WiredHome 1:2ee90f546f54 171 /// @param[out] ptr is a pointer to the user buffer.
WiredHome 1:2ee90f546f54 172 /// @param[in] maxsize is the size of the user buffer.
WiredHome 1:2ee90f546f54 173 /// @param[in] format is a pointer to the special strftime format specification.
WiredHome 1:2ee90f546f54 174 /// @param[in] timeptr is a pointer to the tm_ex structure.
WiredHome 0:61112ca9193b 175 /// @returns the total number of characters copied into the buffer.
WiredHome 0:61112ca9193b 176 ///
WiredHome 0:61112ca9193b 177 size_t strftime(char * ptr, size_t maxsize, const char * format, const struct tm_ex * timeptr);
WiredHome 0:61112ca9193b 178
WiredHome 0:61112ca9193b 179 // time zone functions
WiredHome 0:61112ca9193b 180
WiredHome 0:61112ca9193b 181 /// Set the internal RTC (clock) to the time value. The time value
WiredHome 0:61112ca9193b 182 /// should be the UTC time, which then permits gmtime and
WiredHome 0:61112ca9193b 183 /// localtime to be used appropriately.
WiredHome 0:61112ca9193b 184 ///
WiredHome 2:65e0a25c7551 185 /// @param[in] t should be the UTC time value to set the clock to. If the available
WiredHome 0:61112ca9193b 186 /// time value is local time, the optional time zone offset can
WiredHome 2:65e0a25c7551 187 /// be provided so the system clock is UTC.
WiredHome 1:2ee90f546f54 188 /// @param[in] tzo is the optional time zone offset in minutes when it is in
WiredHome 0:61112ca9193b 189 /// the range of -720 to +720 (-12 hours to + 12 hours). Any
WiredHome 0:61112ca9193b 190 /// other value is illegal and no change will be made.
WiredHome 0:61112ca9193b 191 ///
WiredHome 0:61112ca9193b 192 void set_time(time_t t, int16_t tzo_min = 0);
WiredHome 0:61112ca9193b 193
WiredHome 0:61112ca9193b 194 /// Set the time zone offset in minutes.
WiredHome 0:61112ca9193b 195 ///
WiredHome 0:61112ca9193b 196 /// This API should be used before any other methods that fetch
WiredHome 0:61112ca9193b 197 /// the RTC info.
WiredHome 0:61112ca9193b 198 ///
WiredHome 1:2ee90f546f54 199 /// @param[in] tzo is the time zone offset in minutes when it is in
WiredHome 0:61112ca9193b 200 /// the range of -720 to +720 (-12 hours to + 12 hours). Any
WiredHome 0:61112ca9193b 201 /// other value is illegal and no change will be made.
WiredHome 0:61112ca9193b 202 ///
WiredHome 0:61112ca9193b 203 void set_tzo_min(int16_t tzo_min);
WiredHome 0:61112ca9193b 204
WiredHome 0:61112ca9193b 205 /// Get the time zone offset in minutes.
WiredHome 0:61112ca9193b 206 ///
WiredHome 0:61112ca9193b 207 /// @returns the time zone offset value in minutes. If the tzo was
WiredHome 0:61112ca9193b 208 /// never initialized, this returns zero.
WiredHome 0:61112ca9193b 209 ///
WiredHome 0:61112ca9193b 210 int16_t get_tzo_min(void);
WiredHome 0:61112ca9193b 211
WiredHome 0:61112ca9193b 212 /// Get the time value when the clock was last set. This is most
WiredHome 0:61112ca9193b 213 /// often used in calibration of the clock.
WiredHome 0:61112ca9193b 214 ///
WiredHome 0:61112ca9193b 215 /// @returns time last set as a UTC time value.
WiredHome 0:61112ca9193b 216 ///
WiredHome 0:61112ca9193b 217 time_t get_timelastset(void);
WiredHome 0:61112ca9193b 218
WiredHome 0:61112ca9193b 219 /// get_cal will return the calibration register value
WiredHome 0:61112ca9193b 220 ///
WiredHome 0:61112ca9193b 221 /// This is the raw register value as a signed 32-bit value (even though
WiredHome 0:61112ca9193b 222 /// it is actually a 17-bit unsigned value with an additional 'direction' flag).
WiredHome 0:61112ca9193b 223 ///
WiredHome 0:61112ca9193b 224 /// @returns calibration settings ranging from -131071 to +131071
WiredHome 0:61112ca9193b 225 ///
WiredHome 0:61112ca9193b 226 int32_t get_cal();
WiredHome 0:61112ca9193b 227
WiredHome 0:61112ca9193b 228 /// set_cal will set the calibration register value
WiredHome 0:61112ca9193b 229 ///
WiredHome 0:61112ca9193b 230 /// This accepts a signed value to be used to set the calibration
WiredHome 0:61112ca9193b 231 /// registers. Setting the calibration value to zero disables the
WiredHome 0:61112ca9193b 232 /// calibration function.
WiredHome 0:61112ca9193b 233 ///
WiredHome 0:61112ca9193b 234 /// It is important to know the register function in order to use
WiredHome 0:61112ca9193b 235 /// this command, and this API is normally not used by external
WiredHome 0:61112ca9193b 236 /// application code. @See AdjustBySeconds for a user-friendly
WiredHome 0:61112ca9193b 237 /// API.
WiredHome 0:61112ca9193b 238 ///
WiredHome 1:2ee90f546f54 239 /// @param[in] calibration value to use ranging from -131071 to +131071
WiredHome 0:61112ca9193b 240 /// @returns nothing
WiredHome 0:61112ca9193b 241 ///
WiredHome 0:61112ca9193b 242 void set_cal(int32_t calibration);
WiredHome 0:61112ca9193b 243
WiredHome 0:61112ca9193b 244 /// adjust_sec adjusts both the time and the calibration by seconds
WiredHome 0:61112ca9193b 245 ///
WiredHome 0:61112ca9193b 246 /// This will take a signed value, which is the current adjustment in seconds
WiredHome 0:61112ca9193b 247 /// to put the clock on the correct time. So, if the clock is behind by
WiredHome 0:61112ca9193b 248 /// 3 seconds, the value should be +3 to advance the clock accordingly.
WiredHome 0:61112ca9193b 249 /// It will then adjust the time, and it will attempt to adjust the
WiredHome 0:61112ca9193b 250 /// calibration factor to make the time more accurate.
WiredHome 0:61112ca9193b 251 ///
WiredHome 0:61112ca9193b 252 /// The adjustment can only be made if it has retained when the clock was
WiredHome 0:61112ca9193b 253 /// last set, in order to know by how much to adjust it. It is also most
WiredHome 0:61112ca9193b 254 /// accurate if several days have elapsed since the time was set.
WiredHome 0:61112ca9193b 255 ///
WiredHome 0:61112ca9193b 256 /// @note The current version only works if the calibration value
WiredHome 0:61112ca9193b 257 /// is zero when this adjustment is made.
WiredHome 0:61112ca9193b 258 ///
WiredHome 1:2ee90f546f54 259 /// @param[in] adjustSeconds is the signed value by which to adjust the time to
WiredHome 0:61112ca9193b 260 /// correct it to the current actual time.
WiredHome 0:61112ca9193b 261 /// @returns true if the adjustment was made
WiredHome 0:61112ca9193b 262 /// @returns false if the adjustment could not be made
WiredHome 0:61112ca9193b 263 ///
WiredHome 0:61112ca9193b 264 bool adjust_sec(int32_t adjustSeconds);
WiredHome 0:61112ca9193b 265
WiredHome 2:65e0a25c7551 266 /// Set the clock from an internet source (blocking)
WiredHome 2:65e0a25c7551 267 ///
WiredHome 2:65e0a25c7551 268 /// This function is the interface to NTPClient.
WiredHome 2:65e0a25c7551 269 /// Blocks until completion
WiredHome 2:65e0a25c7551 270 ///
WiredHome 2:65e0a25c7551 271 /// @param[in] host NTP server IPv4 address or hostname (will be resolved via DNS)
WiredHome 2:65e0a25c7551 272 /// @param[in] port port to use; defaults to 123
WiredHome 2:65e0a25c7551 273 /// @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
WiredHome 2:65e0a25c7551 274 /// @return 0 on success, NTP error code (<0) on failure
WiredHome 2:65e0a25c7551 275 ///
WiredHome 2:65e0a25c7551 276 NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT);
WiredHome 2:65e0a25c7551 277
WiredHome 2:65e0a25c7551 278 // ntp interface functions
WiredHome 0:61112ca9193b 279 private:
WiredHome 0:61112ca9193b 280 char result[30]; // holds the converted to text time string
WiredHome 0:61112ca9193b 281 time_t tresult; // holds the converted time structure.
WiredHome 0:61112ca9193b 282 struct tm_ex tm_ext;
WiredHome 0:61112ca9193b 283 };
WiredHome 0:61112ca9193b 284
WiredHome 0:61112ca9193b 285 #endif // TIMEINTERFACE_H