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
Revision 11:1d880a50da8a, committed 2017-01-22
- Comitter:
- WiredHome
- Date:
- Sun Jan 22 19:31:44 2017 +0000
- Parent:
- 10:5734dbc2f5cc
- Child:
- 14:b5c01a52bff4
- Child:
- 15:82bd8fc6f317
- Commit message:
- Documentation update and added a few more timezones
Changed in this revision
TimeInterface.cpp | Show annotated file Show diff for this revision Revisions of this file |
TimeInterface.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/TimeInterface.cpp Sun Jan 22 04:06:16 2017 +0000 +++ b/TimeInterface.cpp Sun Jan 22 19:31:44 2017 +0000 @@ -101,7 +101,7 @@ dst_event_pair_t test_pair; if (parseDSTstring(&test_pair.dst_start, dstStart) - && parseDSTstring(&test_pair.dst_stop, dstStop)) { + && parseDSTstring(&test_pair.dst_stop, dstStop)) { memcpy(&dst_pair, &test_pair, sizeof(dst_event_pair_t)); INFO("set_dst from (%s,%s)", dstStart, dstStop); return true; @@ -215,7 +215,8 @@ return result; } -struct tm_ex * TimeInterface::gmtime(const time_t * timer) { +struct tm_ex * TimeInterface::gmtime(const time_t * timer) +{ time_t priv = *timer + get_tzo_min() * 60 + dst * 3600; struct tm * tmp = std::localtime(&priv); @@ -232,7 +233,8 @@ return &tm_ext; } -struct tm_ex * TimeInterface::localtime(const time_t * timer) { +struct tm_ex * TimeInterface::localtime(const time_t * timer) +{ struct tm * tmp = std::localtime(timer); tm_ext.tm_sec = tmp->tm_sec; @@ -417,8 +419,8 @@ char *am_string; char *pm_string; char *ldate_format; - char *zone_names[9]; - int8_t zone_offsets[9]; + char *zone_names[10]; + int8_t zone_offsets[10]; }; static struct dtconv En_US = { @@ -442,10 +444,11 @@ "AM", "PM", "%A, %B, %e, %Y", - { "UTC", "EST", "CST", "MST", "PST" }, - { 0, -5, -6, -7, -8 }, + { "UTC", "EST", "CST", "MST", "PST", "YST", "CAT", "HST", "CET", "EET", }, + { 0, -5, -6, -7, -8, -9, -10, -10, +1, +2, }, }; + #ifndef isprint #define in_range(c, lo, up) ((u8_t)c >= lo && (u8_t)c <= up) #define isprint(c) in_range(c, 0x20, 0x7f) @@ -734,13 +737,10 @@ } if (i == asizeof(En_US.zone_names)) return 0; -printf("z: %s => %d\r\n", En_US.zone_names[i], En_US.zone_offsets[i]); tm->tm_tzo_min = En_US.zone_offsets[i] * 60; buf += len; break; } } - return buf; } -
--- a/TimeInterface.h Sun Jan 22 04:06:16 2017 +0000 +++ b/TimeInterface.h Sun Jan 22 19:31:44 2017 +0000 @@ -4,7 +4,7 @@ #include "mbed.h" #include <ctime> -#include "NTPClient.h" // ver 7 Wiredhome from ver 5 Donatien Garnier +#include "NTPClient.h" // ver 9 // Special Registers and their usage: // GPREG0: 32 bits @@ -32,10 +32,12 @@ int tm_tzo_min; ///<! localtime zone offset in minutes }; -/// TimeInterface class is much like the normal c-style time.h -/// interface, but is extended with time-zone support, and -/// clock-adjustment support (which permits tuning the clock) -/// for more accuracy. +/// TimeInterface class is much like the normal c-style time.h interface, but +/// is extended with time-zone support, and clock-adjustment support (which +/// permits tuning the clock) for more accuracy. +/// +/// Additionally, strptime was integrated, which can extract the time from +/// a text string. A formatter is used, so it cannot parse an arbitrary string. /// /// Within this class are the normal time.h methods, simply /// exposed here for one consistent interface. @@ -48,11 +50,66 @@ /// gmtime method in this library actually uses localtime, /// but manages the time-zone offset as it does so. /// +/// @code +/// // TimeInterface Architecture +/// // +/// // +--------+ +/// // | clock |----> clock_t clock() +/// // +--------+ +/// // +/// // +--------+ +/// // | RTC |<---- setTime(char*, uint16_t, uint32_t) +/// // | |<---- adjust_sec(int32_t) +/// // | |<---- set_cal(int32_t) +/// // | |----> int32_t get_cal() +/// // | |----> bool get_dst() +/// // | |<---- set_dst(char *, char *) +/// // | |<---- set_time(time_t t, int16_t) +/// // | |----> time_t time(time_t *) +/// // | |--+-> time_t timelocal(time_t *) +/// // +--------+ | +/// // | +/// // +--------+ | +/// // | tzo |--+ +/// // | |<---- set_tzo_min(int16_t) +/// // | |----> int16_t get_tzo_min() +/// // +--------+ +/// // +/// // +--------+ +--------------------------+ +/// // | time_t | ---> char * ctime(time_t *) ----> | buffer | +/// // | value | | Www Mmm dd hh:mm:ss yyyy | +/// // +--------+ +- char * asctime(tm_ex *) -> +--------------------------+ +/// // ^ | | +/// // | | | +-----------------+ +/// // | | +-------------------------------> | tm_ex | +/// // | | | .tm_sec | +/// // | +- tm_ex * gmtime(const time_t *) -----> | .tm_min | +/// // | | | .tm_hour | +/// // | +- tm_ex * localtime(const time_t *) --> | .tm_mday | +/// // | | .tm_mon | +/// // +---- time_t mktime(struct tm_ex *) ------- | .tm_year | +/// // | .tm_wday | +/// // | .tm_yday | +/// // | .tm_isdst | +/// // +-------------------------------------------- | .tm_tzo_min | +/// // | +-----------------+ +/// // | +--------------------------+ +/// // +- strftime(char * ptr, ..., tm_ex *) --> | buffer | +/// // +--------------------------+ +/// // double difftime(time_t end, time_t) +/// +/// @endcode +/// class TimeInterface { public: + /// Constructor for the TimeInterface class, which does minimal initialization. + /// TimeInterface(); + /// Destructor, normally not used, because it is typically kept for the life + /// of the program. + /// ~TimeInterface(); /// Gets the system elapsed time in CLOCKS_PER_SEC tics. @@ -243,7 +300,7 @@ /// - %%Y The year, including the century (for example, 1988). /// - %%Z The timezone offset, as a 3-letter sequence. Only a few whole-hour offsets /// have been defined. - /// - %%%% Replaced by %%. + /// - %% Replaced by %. /// const char * strptime(const char *buf, char *fmt, struct tm_ex *tm); @@ -416,4 +473,4 @@ struct tm_ex tm_ext; }; -#endif // TIMEINTERFACE_H \ No newline at end of file +#endif // TIMEINTERFACE_H