diff --git a/include/Clock.h b/include/Clock.h index 234800b..87ff289 100644 --- a/include/Clock.h +++ b/include/Clock.h @@ -11,14 +11,12 @@ #include void clock_setup(void); +SNTPError clock_sync(NetworkInterface &net); std::chrono::microseconds clock_now(void); -SNTPError clock_sync(NetworkInterface &net); - const char *clock_timestring(std::chrono::microseconds, const char *format); const char *clock_iso8601string(std::chrono::microseconds); - const char *clock_iso8601string_now(void); #endif diff --git a/src/Clock.cpp b/src/Clock.cpp index f1e921c..acc181f 100644 --- a/src/Clock.cpp +++ b/src/Clock.cpp @@ -8,16 +8,12 @@ using namespace std::chrono; +// Initializes the clock, run once at boot void clock_setup(void) { // mbed reads the RTC at boot, do nothing for now } -microseconds clock_now(void) { - time_t now_seconds = time(NULL); - microseconds microsecs = seconds(now_seconds); - return microsecs; -} - +// Syncs the clock using SNTP, run any time SNTPError clock_sync(NetworkInterface &net) { struct timeval time; SNTPError err; @@ -29,6 +25,18 @@ return SNTPSuccess; } +// Gets the current clock time in microseconds since 1970 +microseconds clock_now(void) { + time_t now_seconds = time(NULL); + microseconds microsecs = seconds(now_seconds); + return microsecs; +} + +// Formats a time in microseconds using strftime +// Returned value is a buffer containing the formatted string +// or "(time overflow)" +// Calling this function overwrites the previous buffer contents +// so if you want to preserve it please copy the string const char *clock_timestring(microseconds time, const char *format) { time_t time_seconds = ceil(time).count(); struct tm *local_time = localtime(&time_seconds); @@ -46,6 +54,11 @@ } } +// Formats a time in microseconds according to ISO 8601 +// Returned value is a buffer containing the formatted string +// or "(iso8601 overflow)" +// Calling this function overwrites the previous buffer contents +// so if you want to preserve it please copy the string const char *clock_iso8601string(microseconds time) { const char *timestring = clock_timestring(time, "%Y-%m-%dT%H:%M:%S"); long only_microseconds = time.count() % 1000000; @@ -64,7 +77,9 @@ } } +// Formats the current time in microseconds according to ISO 8601 +// This is a quick wrapper for clock_iso8601string using the current +// clock time. Read its documentation for full usage details const char *clock_iso8601string_now(void) { - microseconds now = clock_now(); return clock_iso8601string(clock_now()); }