// Print date and time for i3 status bar. /* i3blocks config: * [time] * command=statustime * interval=persist * markup=pango */ #include #include #include #include #include #include #include #include void signal_handler(int signum) { switch (signum) { case SIGTERM: case SIGINT: { std::cout << R"(Time script terminated.)" << std::endl; std::exit(signum); // NOLINT(concurrency-mt-unsafe) break; } default: { break; } } } int main() { using fmt::format; using std::cout; using std::endl; 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. std::locale::global(std::locale("de_DE.UTF-8")); std::signal(SIGINT, signal_handler); std::signal(SIGTERM, signal_handler); try { while (true) { const auto now{clock::now()}; cout << format(R"({0:%A}, )" R"({0:%Y-%m-%d} )" R"({0:%H:%M})", now) << endl; // NOTE: Don't forget that we need to flush! 😊 const auto next_minute{std::chrono::floor(now + 1min)}; std::this_thread::sleep_until(next_minute); } } catch (const std::exception &e) { cout << R"(Time script crashed.)" << endl; std::cerr << e.what() << '\n'; return 1; } } // Local Variables: // eval: (rainbow-mode 1) // End: