From 2c1484587fa2df79b042bcd35d0eac8665e6c4d3 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 14 Aug 2021 18:59:59 +0200 Subject: [PATCH] statustime: Only update when the minute changes. --- statustime.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/statustime.cpp b/statustime.cpp index d62fd8b..c2e7e89 100755 --- a/statustime.cpp +++ b/statustime.cpp @@ -13,6 +13,8 @@ int main() { using fmt::format; using std::cout; + using clock = std::chrono::system_clock; + using std::chrono::minutes; using namespace std::chrono_literals; // Set explicitly, because LC_TIME is en_DK.UTF-8. @@ -22,12 +24,15 @@ int main() { while (true) { + const auto now{clock::now()}; cout << format(R"({0:%A}, )" R"({0:%Y-%m-%d} )" R"({0:%H:%M})", - std::chrono::system_clock::now()) + now) << std::endl; // NOTE: Don't forget that we need to flush! 😊 - std::this_thread::sleep_for(1s); + + const auto next_minute{std::chrono::floor(now + 1min)}; + std::this_thread::sleep_until(next_minute); } } catch (const std::exception &e)