diff --git a/include/Clock.h b/include/Clock.h index 7fc28e2..55c97b3 100644 --- a/include/Clock.h +++ b/include/Clock.h @@ -25,6 +25,7 @@ SNTPError clock_sync(NetworkInterface &net); const char *clock_timestring(struct timeval time, const char *format); -const char *clock_iso8601string(void); +const char *clock_iso8601string(struct timeval time); +const char *clock_iso8601string_now(); #endif diff --git a/src/Clock.cpp b/src/Clock.cpp index 3dfc0bb..adbfb9a 100644 --- a/src/Clock.cpp +++ b/src/Clock.cpp @@ -48,7 +48,7 @@ microseconds curr_offset = boot_start; clock_lock.unlock(); microseconds boot_offset = clock_time - curr_offset; - milliseconds offset_ms = duration_cast(boot_offset); + milliseconds offset_ms = std::chrono::ceil(boot_offset); return kernel_timepoint(offset_ms); } @@ -89,16 +89,14 @@ } } -const char *clock_iso8601string(void) { - microseconds time = clock_now(); - struct timeval val = microseconds_to_timeval(time); - const char *timestring = clock_timestring(val, "%Y-%m-%dT%H:%M:%S"); +const char *clock_iso8601string(struct timeval time) { + const char *timestring = clock_timestring(time, "%Y-%m-%dT%H:%M:%S"); static char buffer[128]; int buffer_size = sizeof(buffer); int written; written = snprintf( - buffer, buffer_size, "%s.%05li", timestring, val.tv_usec); + buffer, buffer_size, "%s.%06li", timestring, time.tv_usec); if (written >= buffer_size) { // It overflowed, so return something useful @@ -107,3 +105,8 @@ return buffer; } } + +const char *clock_iso8601string_now(void) { + struct timeval time = microseconds_to_timeval(clock_now()); + return clock_iso8601string(time); +} diff --git a/src/main.cpp b/src/main.cpp index 6bf547b..94a5039 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,7 +49,7 @@ tr_info("Project Tardis"); clock_setup(); - tr_info("The time is %s", clock_iso8601string()); + tr_info("The time is %s", clock_iso8601string_now()); mountFilesystem(mainBD, mainFS); @@ -74,32 +74,43 @@ } if (presses == 3) { +#if 0 if (!network_is_connected()) { tr_err("Can't sync clock without network " "connection!"); continue; } +#endif NetworkInterface *net = network_interface(); tr_info("Syncing clock..."); tr_info("The current time is %s", - clock_iso8601string()); + clock_iso8601string_now()); SNTPError err = clock_sync(*net); if (err != SNTPSuccess) { tr_err("Failed to sync clock, error %i", err); } else { tr_info("The new time is %s", - clock_iso8601string()); + clock_iso8601string_now()); } + std::chrono::microseconds now_us = clock_now(); + std::chrono::milliseconds wait = + std::chrono::floor( + now_us); + wait += std::chrono::milliseconds(10); + wait -= std::chrono::milliseconds(wait.count() % 10); for (int i = 0; i < 5; ++i) { - std::chrono::microseconds now = clock_now(); - now += std::chrono::seconds(1); - now = std::chrono::floor( - now); - kernel_timepoint wait_time = kernel_time(now); + kernel_timepoint wait_time = kernel_time(wait); ThisThread::sleep_until(wait_time); - tr_info("Slept to nearest second. The new time " - "is %s", - clock_iso8601string()); + now_us = clock_now(); + struct timeval time = + microseconds_to_timeval(now_us); + struct timeval time2 = + microseconds_to_timeval(wait); + tr_info("Slept to %s", + clock_iso8601string(time)); + tr_info("Tried for %s ", + clock_iso8601string(time2)); + wait += std::chrono::milliseconds(10); } } }