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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TimeInterface.h Source File

TimeInterface.h

00001 
00002 #ifndef TIMEINTERFACE_H
00003 #define TIMEINTERFACE_H
00004 #include "mbed.h"
00005 #include <ctime>
00006 
00007 #include "NTPClient.h"
00008 
00009 // Special Registers and their usage:
00010 // GPREG0: 32 bits
00011 //      low word: time zone offset (-720 to +720)
00012 //      high word: 2's complement of low word for integrity checking
00013 // GPREG1: 32 bits
00014 //      time_t value when the clock was last set
00015 
00016 
00017 extern "C" {
00018 #include "time.h"       // uses some std::time-functions
00019 }
00020 
00021 /// The tm_ex structure is patterned after the traditional tm struct, however
00022 /// it adds an element - the time zone offset in minutes. From this, it is then
00023 /// readily able to create a "local time" instead of simply a UTC time.
00024 ///
00025 struct tm_ex
00026 {
00027     int   tm_sec;       ///<! seconds, 0 to 59.
00028     int   tm_min;       ///<! minutes, 0 to 59.
00029     int   tm_hour;      ///<! hours,   0 to 23.
00030     int   tm_mday;      ///<! monthday 1 to 31.
00031     int   tm_mon;       ///<! month    0 to 11.
00032     int   tm_year;      ///<! years since 1900.
00033     int   tm_wday;      ///<! days since sunday 0 to 6.
00034     int   tm_yday;      ///<! days since 1 Jan 0 to 365.
00035     int   tm_isdst;     ///<! is daylight savings time.
00036     int   tm_tzo_min;   ///<! localtime zone offset in minutes (_ex element)
00037 };
00038 
00039 /// TimeInterface class is much like the normal c-style time.h interface, but
00040 /// is extended with time-zone support, and clock-adjustment support (which 
00041 /// permits tuning the clock) for more accuracy. 
00042 ///
00043 /// Additionally, strptime was integrated, which can extract the time from
00044 /// a text string. A formatter is used, so it cannot parse an arbitrary string.
00045 ///
00046 /// Within this class are the normal time.h methods, simply
00047 /// exposed here for one consistent interface.
00048 ///
00049 /// @note This class uses the special battery backed registers
00050 ///     GPREG0 and GPREG1 for TimeInterface data.
00051 ///
00052 /// @note In mbed library ver 84, the gmtime method is defective,
00053 ///     and calls to this function return junk data. The 
00054 ///     gmtime method in this library actually uses localtime,
00055 ///     but manages the time-zone offset as it does so.
00056 ///
00057 /// @code
00058 /// // TimeInterface Architecture and APIs
00059 /// //
00060 /// // +--------+
00061 /// // | clock  |----> clock_t clock()
00062 /// // +--------+
00063 /// // 
00064 /// // +--------+
00065 /// // |        |<------------ setTime(char * server, uint16_t port, uint32_t timeout)
00066 /// // | NTP    |<-----------> Ethernet
00067 /// // |        |----+
00068 /// // +--------+    |
00069 /// //               |
00070 /// // +--------+    |
00071 /// // | RTC    |<---+-------- set_time(time_t t, int16_t tzo)
00072 /// // |        |<------------ adjust_sec(int32_t)
00073 /// // |        |<------------ set_cal(int32_t)
00074 /// // |        |------------> int32_t get_cal()
00075 /// // |        |------------> time_t time(time_t *)
00076 /// // |        |------+
00077 /// // +--------+      |
00078 /// //                 |  
00079 /// // +--------+      |
00080 /// // |        |<---- | <---- set_dst(bool dst)
00081 /// // |        |<---- | <---- set_dst(char * start_dst, char * end_dst)
00082 /// // |        |----- | ----> bool get_dst()
00083 /// // |dst_pair|---+  |  +----------+
00084 /// // +--------+   |  |  |          |
00085 /// //              |  +->|          |
00086 /// // +--------+   +---->|time_local|--------> time_t timelocal(time_t *)     
00087 /// // | tzo    |<--------|          |
00088 /// // |        |         +----------+
00089 /// // |        |<------------ set_tzo_min(int16_t)
00090 /// // |        |------------> int16_t get_tzo_min()
00091 /// // +--------+                                
00092 /// //                                           
00093 /// // +--------+                                   +--------------------------+
00094 /// // | time_t | ---> char * ctime(time_t *) ----> | buffer                   |
00095 /// // | value  |                                   | Www Mmm dd hh:mm:ss yyyy |
00096 /// // +--------+     +- char * asctime(tm_ex *) -> +--------------------------+
00097 /// //      ^  |      |
00098 /// //      |  |      |                                 +-----------------+   
00099 /// //      |  |      +-------------------------------- | tm_ex           |   
00100 /// //      |  |                                        |   .tm_sec       |   
00101 /// //      |  +- tm_ex * gmtime(const time_t *) -----> |   .tm_min       |   
00102 /// //      |  |                                        |   .tm_hour      |   
00103 /// //      |  +- tm_ex * localtime(const time_t *) --> |   .tm_mday      |   
00104 /// //      |                                           |   .tm_mon       |   
00105 /// //      +---- time_t mktime(struct tm_ex *) ------- |   .tm_year      |   
00106 /// //                                                  |   .tm_wday      |   
00107 /// //                                                  |   .tm_yday      |   
00108 /// //  +---------------------------------------------> |   .tm_isdst     |   
00109 /// //  | +-------------------------------------------- |   .tm_tzo_min   |               
00110 /// //  | |                                             +-----------------+               
00111 /// //  | |                                         +--------------------------+
00112 /// //  | +- strftime(char * ptr, ..., tm_ex *) --> | buffer                   |
00113 /// //  +----strptime(char * buf, ..., tm_ex *) --- | Www Mmm dd hh:mm:ss yyyy |
00114 /// //                                              +--------------------------+
00115 /// //      double difftime(time_t end, time_t)
00116 /// //
00117 /// @endcode
00118 ///
00119 class TimeInterface
00120     {
00121 public:
00122     /// Constructor for the TimeInterface class, which does minimal initialization.
00123     ///
00124     /// @param[in] net is optional and provides the EthernetInterface which is
00125     ///             used if you want to sync to an NTP server
00126     ///
00127     /// @code
00128     /// EthernetInterface net;
00129     /// TimeInterface ntp(&net);
00130     /// ...
00131     ///     ntp.set_tzo_min(-6 * 60);
00132     ///     if (NTP_OK == ntp.setTime("time.nist.gov", 123, 10)) {
00133     ///         time_t tNow = ntp.timelocal();
00134     ///         printf("time is %s\r\n", ntp.ctime(&tNow));
00135     ///     }
00136     /// ...
00137     /// @endcode
00138     ///
00139     TimeInterface(EthernetInterface *m_net = NULL);
00140     
00141     /// Destructor, normally not used, because it is typically kept for the life
00142     /// of the program.
00143     ///
00144     ~TimeInterface();
00145     
00146     /// Gets the system elapsed time in CLOCKS_PER_SEC tics.
00147     ///
00148     /// Divide the returned value by CLOCKS_PER_SEC to get time in seconds.
00149     ///
00150     /// @code
00151     /// clock_t tstart, tend;
00152     /// ...
00153     ///     tstart = clock();
00154     ///     // do something long
00155     ///     tend = clock();
00156     ///     printf("Elapsed time is %5.3f\r\n", (float)(tend - tstart)/CLOCKS_PER_SEC);
00157     /// ...
00158     /// @endcode
00159     ///
00160     /// @returns elapsed tics.
00161     ///
00162     clock_t clock(void);
00163     
00164     /// Gets the current time as a UTC time value, optionally writing it
00165     /// to a provided buffer.
00166     ///
00167     /// This reads the real time clock and returns the current UTC time.
00168     ///
00169     /// @code
00170     /// time_t t_ref1, t_ref2, t_ref3;
00171     /// t_ref1 = time(NULL); 
00172     /// t_ref2 = t.time(); 
00173     /// (void)t.time(&t_ref3);
00174     /// @endcode
00175     ///
00176     /// @param[out] timer is an optional pointer to a time_t value that will 
00177     ///             be written with the current time. This pointer is ignored 
00178     ///             when NULL.
00179     /// @returns the UTC time value.
00180     ///
00181     time_t time(time_t * timer = NULL);
00182 
00183     /// Gets the current time as a LOCAL time value, optionally writing it
00184     /// to a provided buffer.
00185     ///
00186     /// This reads the real time clock and returns the current time, adjusted
00187     /// for the local time zone and daylight savings time.
00188     ///
00189     /// @code
00190     /// time_t t_ref2, t_ref3;
00191     /// t_ref2 = t.time(); 
00192     /// t.timelocal(&t_ref3);
00193     /// @endcode
00194     ///
00195     /// @param[out] timer is an optional pointer to a time_t value that will 
00196     ///     be written. This pointer is ignored when NULL.
00197     /// @returns the LOCAL time value (UTC adjusted for the LOCAL time zone and dst).
00198     ///
00199     time_t timelocal(time_t * timer = NULL);
00200 
00201     /// Convert a time value structure into an ASCII printable time "Www Mmm dd hh:mm:ss yyyy"
00202     ///
00203     /// @note Watch out for race conditions as this returns a pointer to a
00204     ///         shared buffer.
00205     /// @note Unlike the standard ctime function, this version DOES NOT append 
00206     ///         a newline character to the buffer.
00207     ///
00208     /// @code
00209     /// time_t tNow = timelocal();
00210     /// printf("time is %s\r\n", ctime(tNow));
00211     /// @endcode
00212     ///
00213     /// @param[in] timer is a pointer to a time_t value containing the time to convert.
00214     /// @returns a pointer to a buffer containing the string.
00215     ///
00216     char * ctime(const time_t * timer);
00217 
00218     /// Convert a tm_ex structure into an ASCII printable "time Www Mmm dd hh:mm:ss yyyy"
00219     ///
00220     /// @note Unlike the standard asctime, this takes a pointer to a tm_ex, which 
00221     ///         has a time zone offset element.
00222     ///
00223     /// @note Watch out for race conditions as this returns a pointer to a
00224     ///     shared buffer.
00225     ///
00226     /// @note Unlike the standard ctime function, this version DOES NOT append 
00227     ///     a newline character to the buffer.
00228     ///
00229     /// @code
00230     /// time_t tNow = timelocal();
00231     /// tm_ex * tEx = localtime(&tNow);
00232     /// printf("Time is %s\r\n", asctime(tEx));
00233     /// @endcode
00234     ///
00235     /// @param[in] timeptr is a pointer to a tm_ex structure containing the time to convert.
00236     /// @returns a pointer to a private buffer containing the string.
00237     ///
00238     char * asctime(const struct tm_ex *timeptr);
00239 
00240     /// Compute the difference in seconds between two time values.
00241     ///
00242     /// @code
00243     /// time_t tstart, tend;
00244     /// ...
00245     ///     tstart = time();
00246     ///     // do some long process now
00247     ///     tend = time();
00248     ///     printf("Elapsed time is %5.3f\r\n", tend - tstart);
00249     /// ...
00250     /// @endcode
00251     ///
00252     /// @param[in] end is the end time to compare to the beginning time.
00253     /// @param[in] beginning time is compared to the end time.
00254     /// @return the difference in seconds, as a double.
00255     ///
00256     double difftime(time_t end, time_t beginning);
00257     
00258     /// Convert the referenced time_t value to a tm_ex structure in UTC/GMT format.
00259     ///
00260     /// @note Unlike the standard asctime, this return a pointer to a tm_ex, which 
00261     ///         has a time zone offset element.
00262     ///
00263     /// @note Watch out for race conditions as this returns a pointer to a
00264     ///     shared buffer.
00265     ///
00266     /// @param[in] timer is a pointer to a time_t structure to convert.
00267     /// @returns pointer to a tm_ex structure.
00268     ///
00269     struct tm_ex * gmtime(const time_t * timer);
00270     
00271     
00272     /// Convert the referenced time_t value to a tm structure in local format.
00273     ///
00274     /// This method leverages the time zone offset applied with @see set_tzo()
00275     /// and the daylight savings time flag applied with @see set_dst().
00276     ///
00277     /// @note Watch out for race conditions as this returns a pointer to a
00278     ///     shared buffer.
00279     ///
00280     /// @code
00281     /// time_t tNow = timelocal();
00282     /// tm_ex * tEx = localtime(&tNow);
00283     /// @endcode
00284     ///
00285     /// @param[in] timer is a pointer to a time_t structure to convert.
00286     /// @returns pointer to a tm structure.
00287     ///
00288     struct tm_ex * localtime(const time_t * timer);
00289     
00290     /// Convert a tm_ex structure (an extended time structure) to a time_t
00291     /// value.
00292     ///
00293     /// This function also sets the tzo_min element of the tm_ex structure
00294     /// from the previously set tzo_min.
00295     /// 
00296     /// @param[in] timeptr is a pointer to a tm_ex structure.
00297     /// @returns the computed time_t value.
00298     ///
00299     time_t mktime(struct tm_ex * timeptr);
00300     
00301     /// Presents a time value in a user specified format, into a user specified buffer.
00302     ///
00303     /// @param[out] ptr is a pointer to the user buffer.
00304     /// @param[in] maxsize is the size of the user buffer.
00305     /// @param[in] format is a pointer to the special strftime format specification.
00306     ///             see format options.
00307     /// @param[in] timeptr is a pointer to the tm_ex structure.
00308     /// @returns the total number of characters copied into the buffer.
00309     ///
00310     /// format options:
00311     ///     - %%a  Abbreviated weekday name e.g. Thu
00312     ///     - %%A  Full weekday name e.g. Thursday
00313     ///     - %%b  Abbreviated month name e.g. Aug
00314     ///     - %%B  Full month name e.g. August
00315     ///     - %%c  Date and time representation e.g. Thu Aug 23 14:55:02 2001
00316     ///     - %%C  Year divided by 100 and truncated to integer (00-99) e.g. 20
00317     ///     - %%d  Day of the month, zero-padded (01-31) e.g. 23
00318     ///     - %%D  Short MM/DD/YY date, equivalent to %%m/%%d/%%y e.g. 08/23/01
00319     ///     - %%e  Day of the month, space-padded ( 1-31) e.g. 23
00320     ///     - %%F  Short YYYY-MM-DD date, equivalent to %%Y-%%m-%%d e.g. 2001-08-23
00321     ///     - %%g  Week-based year, last two digits (00-99) e.g. 01
00322     ///     - %%G  Week-based year e.g. 2001
00323     ///     - %%h  Abbreviated month name * (same as %%b) e.g. Aug
00324     ///     - %%H  Hour in 24h format (00-23) e.g. 14
00325     ///     - %%I  Hour in 12h format (01-12) e.g. 02
00326     ///     - %%j  Day of the year (001-366)  e.g. 235
00327     ///     - %%m  Month as a decimal number (01-12) e.g. 08
00328     ///     - %%M  Minute (00-59) e.g. 55
00329     ///     - %%n  New-line character ('\\n')   
00330     ///     - %%p  AM or PM designation e.g. PM
00331     ///     - %%r  12-hour clock time e.g. 02:55:02 pm
00332     ///     - %%R  24-hour HH:MM time, equivalent to %%H:%%M e.g. 14:55
00333     ///     - %%S  Second (00-61) e.g. 02
00334     ///     - %%t  Horizontal-tab character ('\t') 
00335     ///     - %%T  ISO 8601 time format (HH:MM:SS), equivalent to %%H:%%M:%%S e.g. 14:55:02
00336     ///     - %%u  ISO 8601 weekday as number with Monday as 1 (1-7) e.g. 4
00337     ///     - %%U  Week number with the first Sunday as the first day of week one (00-53) e.g. 33
00338     ///     - %%V  ISO 8601 week number (00-53) e.g. 34
00339     ///     - %%w  Weekday as a decimal number with Sunday as 0 (0-6) e.g. 4
00340     ///     - %%W  Week number with the first Monday as the first day of week one (00-53) e.g. 34
00341     ///     - %%x  Date representation e.g. 08/23/01
00342     ///     - %%X  Time representation e.g. 14:55:02
00343     ///     - %%y  Year, last two digits (00-99) e.g. 01
00344     ///     - %%Y  Year e.g. 2001
00345     ///     - %%z  ISO 8601 offset from UTC in timezone (1 minute=1, 1 hour=100) (e.g. +100)
00346     ///           If timezone cannot be determined, no characters 
00347     ///     - %%Z  Timezone name or abbreviation (e.g. CDT)
00348     ///           If timezone cannot be determined, no characters 
00349     ///     - %  A % sign
00350     ///
00351     size_t strftime(char * ptr, size_t maxsize, const char * format, const struct tm_ex * timeptr);
00352     
00353 
00354     /// Convert a string, in a defined format, to a time value in a tm_ex structure.
00355     ///
00356     /// Most format details leveraged from The Open Group Base Specifications Issue 6
00357     /// IEEE Std 1003.1, 2004 Edition
00358     /// Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.
00359     ///
00360     /// Modifications for mbed, and addition of the timezone format option by D. Smart
00361     ///
00362     /// @code
00363     ///     char timesample[] = "Jan 22 2017 01:32:48 UTC";
00364     ///     tm_ex tm;
00365     ///     strptime(timesample, "%b %d %Y %H:%M:%S %Z", &tm);
00366     /// @endcode
00367     /// 
00368     /// @param[in] buf is a pointer to the string to be parsed.
00369     /// @param[in] format is a pointer to a format string. See the format options.
00370     /// @param[out] tm is a pointer to a tm_ex struct.
00371     /// @returns a pointer to the character following the last one parsed, or null on failure
00372     ///
00373     /// format options:
00374     ///     - %%a The day of the week, using the locale's weekday names; either the abbreviated or 
00375     ///         full name may be specified.
00376     ///     - %%A Equivalent to %%a.
00377     ///     - %%b The month, using the locale's month names; either the abbreviated or full name 
00378     ///         may be specified.
00379     ///     - %%B Equivalent to %%b.
00380     ///     - %%c Replaced by the locale's appropriate date and time representation.
00381     ///     - %%C The century number [00,99]; leading zeros are permitted but not required.
00382     ///     - %%d The day of the month [01,31]; leading zeros are permitted but not required.
00383     ///     - %%D The date as %%m / %%d / %%y.
00384     ///     - %%e Equivalent to %%d.
00385     ///     - %%h Equivalent to %%b.
00386     ///     - %%H The hour (24-hour clock) [00,23]; leading zeros are permitted but not required.
00387     ///     - %%I The hour (12-hour clock) [01,12]; leading zeros are permitted but not required.
00388     ///     - %%j The day number of the year [001,366]; leading zeros are permitted but not required.
00389     ///     - %%m The month number [01,12]; leading zeros are permitted but not required.
00390     ///     - %%M The minute [00,59]; leading zeros are permitted but not required.
00391     ///     - %%n Any white space.
00392     ///     - %%p The locale's equivalent of a.m or p.m.
00393     ///     - %%r 12-hour clock time using the AM/PM notation if t_fmt_ampm is not an empty string 
00394     ///         in the LC_TIME portion of the current locale; in the POSIX locale, this shall be 
00395     ///         equivalent to %%I : %%M : %%S %%p.
00396     ///     - %%R The time as %%H : %%M.
00397     ///     - %%S The seconds [00,60]; leading zeros are permitted but not required.
00398     ///     - %%t Any white space.
00399     ///     - %%T The time as %%H : %%M : %%S.
00400     ///     - %%U The week number of the year (Sunday as the first day of the week) as a decimal 
00401     ///         number [00,53]; leading zeros are permitted but not required.
00402     ///     - %%w The weekday as a decimal number [0,6], with 0 representing Sunday; leading zeros 
00403     ///         are permitted but not required.
00404     ///     - %%W The week number of the year (Monday as the first day of the week) as a decimal 
00405     ///         number [00,53]; leading zeros are permitted but not required.
00406     ///     - %%x The date, using the locale's date format.
00407     ///     - %%X The time, using the locale's time format.
00408     ///     - %%y The year within century. When a century is not otherwise specified, values in 
00409     ///         the range [69,99] shall refer to years 1969 to 1999 inclusive, and values in the 
00410     ///         range [00,68] shall refer to years 2000 to 2068 inclusive; leading zeros shall be 
00411     ///         permitted but shall not be required.
00412     ///         Note: It is expected that in a future version of IEEE Std 1003.1-2001 
00413     ///         the default century inferred from a 2-digit year will change. 
00414     ///         (This would apply to all commands accepting a 2-digit year as input.)
00415     ///     - %%Y The year, including the century (for example, 1988).
00416     ///     - %%Z The timezone offset, as a 3-letter sequence. Only a few whole-hour offsets
00417     ///         have been defined.
00418     ///     - %% Replaced by %.
00419     ///
00420     const char * strptime(const char *buf, char *fmt, struct tm_ex *tm);
00421 
00422 
00423     // time zone functions
00424     
00425     /// Set the internal RTC (clock) to the time value. 
00426     ///
00427     /// The time valueshould be UTC time along with an offset of zero,
00428     /// which then permits gmtime and localtime to be used appropriately.
00429     /// Alternately, the time can be in localtime, and the offset is then
00430     /// used to compute UTC to set the clock.
00431     ///
00432     /// @param[in] t should be the UTC time value to set the clock to. If the available 
00433     ///     time value is local time, the optional time zone offset can
00434     ///     be provided so the system clock is UTC.
00435     /// @param[in] tzo is the optional time zone offset in minutes when it is in
00436     ///     the range of -720 to +720 (-12 hours to + 12 hours). Any
00437     ///     other value is illegal and no change will be made.
00438     ///
00439     void set_time(time_t t, int16_t tzo_min = 0);
00440     
00441     /// Set the time zone offset in minutes.
00442     ///
00443     /// This API should be used before any other methods that fetch
00444     /// the RTC info.
00445     ///
00446     /// @param[in] tzo is the time zone offset in minutes when it is in
00447     ///     the range of -720 to +720 (-12 hours to + 12 hours). Any
00448     ///     other value is illegal and no change will be made.
00449     ///
00450     void set_tzo_min(int16_t tzo_min);
00451     
00452     /// Get the time zone offset in minutes.
00453     ///
00454     /// @returns the time zone offset value in minutes. If the tzo was
00455     /// never initialized, this returns zero.
00456     ///
00457     int16_t get_tzo_min(void);
00458     
00459     /// Set the clock for local time to report whether the current
00460     /// mode is standard or daylight savings time.
00461     ///
00462     /// return values for localtime will then be adjusted not only
00463     /// for the time zone offset, but for dst.
00464     ///
00465     /// @param[in] dst is a boolean that should be set when dst is
00466     ///         the active mode.
00467     /// @returns true, always.
00468     ///
00469     bool set_dst(bool dst);
00470     
00471     /// Set the clock for auto-adjust local time based on 
00472     /// changing to standard or daylight savings time.
00473     ///
00474     /// return values for localtime will then be adjusted not only
00475     /// for the time zone offset, but for dst.
00476     ///
00477     /// @param[in] dstStart is a string of the form "mm/dd,hh:mm"
00478     ///                     representing when DST starts.
00479     /// @param[in] dstStop  is a string of the form "mm/dd,hh:mm"
00480     ///                     representing when DST stops.
00481     /// @returns true if the start and stop pair could be successfully
00482     ///               parsed.
00483     ///
00484     bool set_dst(const char * dstStart, const char * dstStop);
00485     
00486     /// Get the current clock mode for daylight savings time.
00487     ///
00488     /// @returns true if clock is in dst mode.
00489     ///
00490     bool get_dst(void);
00491     
00492     /// Get the time value when the clock was last set. This is most
00493     /// often used in calibration of the clock.
00494     ///
00495     /// @returns time last set as a UTC time value.
00496     ///
00497     time_t get_timelastset(void);
00498     
00499     /// get_cal will return the calibration register value
00500     ///
00501     /// This is the raw register value as a signed 32-bit value (even though
00502     /// it is actually a 17-bit unsigned value with an additional 'direction' flag).
00503     ///
00504     /// @returns calibration settings ranging from -131071 to +131071
00505     ///
00506     int32_t get_cal();
00507 
00508     /// set_cal will set the calibration register value
00509     ///
00510     /// This accepts a signed value to be used to set the calibration
00511     /// registers. Setting the calibration value to zero disables the
00512     /// calibration function. 
00513     ///
00514     /// It is important to know the register function in order to use 
00515     /// this command, and this API is normally not used by external
00516     /// application code. @See AdjustBySeconds for a user-friendly
00517     /// API.
00518     ///
00519     /// @param[in] calibration value to use ranging from -131071 to +131071
00520     /// @returns nothing
00521     ///
00522     void set_cal(int32_t calibration);
00523 
00524     /// adjust_sec adjusts both the time and the calibration by seconds
00525     ///
00526     /// This will take a signed value, which is the current adjustment in seconds
00527     /// to put the clock on the correct time. So, if the clock is behind by
00528     /// 3 seconds, the value should be +3 to advance the clock accordingly.
00529     /// It will then adjust the time, and it will attempt to adjust the
00530     /// calibration factor to make the time more accurate.
00531     ///
00532     /// The adjustment can only be made if it has retained when the clock was
00533     /// last set, in order to know by how much to adjust it. It is also most
00534     /// accurate if several days have elapsed since the time was set.
00535     ///
00536     /// @note The current version only works if the calibration value
00537     ///       is zero when this adjustment is made.
00538     /// 
00539     /// @param[in] adjustSeconds is the signed value by which to adjust the time to
00540     ///        correct it to the current actual time.
00541     /// @returns true if the adjustment was made
00542     /// @returns false if the adjustment could not be made
00543     ///
00544     bool adjust_sec(int32_t adjustSeconds);
00545 
00546     /// Set the clock from an internet source (blocking)
00547     ///
00548     /// This function is the interface to NTPClient.
00549     /// Blocks until completion
00550     ///
00551     /// @param[in] host NTP server IPv4 address or hostname (will be resolved via DNS)
00552     /// @param[in] port port to use; defaults to 123
00553     /// @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
00554     /// @returns NTP_OK on success, 
00555     /// @returns NTP_CONN if no network interface
00556     /// @returns other NTP error code (<0) on failure
00557     ///
00558     NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT);
00559 
00560     // ntp interface functions    
00561 private:
00562     EthernetInterface * m_net;
00563 
00564     typedef struct {
00565         uint8_t MM;
00566         uint8_t DD;
00567         uint8_t hh;
00568         uint8_t mm;
00569     } dst_event_t;
00570     typedef struct {
00571         dst_event_t dst_start;
00572         dst_event_t dst_stop;
00573     } dst_event_pair_t;
00574 
00575     bool parseDSTstring(dst_event_t * result, const char * dstr);
00576     
00577     /// Performs a "simple" computation of two dates into minutes.
00578     ///
00579     /// Does not account for leap years or which month it is. Is
00580     /// useful only for comparing which date/time came first, not for
00581     /// computing the difference between them.
00582     ///
00583     /// @return "normalized" minutes since Jan 1 00:00.
00584     ///
00585     uint32_t minutesSinceJan(int mon, int day, int hr, int min);
00586 
00587     dst_event_pair_t dst_pair;
00588     bool dst;           // true in dst mode
00589     char result[30];    // holds the converted to text time string
00590     time_t tresult;     // holds the converted time structure.
00591     struct tm_ex tm_ext;
00592     };
00593 
00594 #endif // TIMEINTERFACE_H