cppscripts/statustime.cpp

45 lines
1.2 KiB
C++
Raw Normal View History

2021-08-05 16:08:40 +02:00
// Print date and time for i3 status bar.
#include <fmt/chrono.h>
#include <fmt/core.h>
#include <chrono>
#include <exception>
#include <iostream>
#include <locale>
#include <thread>
int main()
{
using fmt::format;
using std::cout;
using clock = std::chrono::system_clock;
using std::chrono::minutes;
2021-08-05 16:08:40 +02:00
using namespace std::chrono_literals;
// Set explicitly, because LC_TIME is en_DK.UTF-8.
std::locale::global(std::locale("de_DE.UTF-8"));
try
{
while (true)
{
const auto now{clock::now()};
2021-08-05 16:08:40 +02:00
cout << format(R"(<b><span color="orange">{0:%A}</span></b>, )"
R"({0:%Y-%m-%d} )"
R"(<span color="lightcyan">{0:%H:%M}</span>)",
now)
2021-08-05 16:08:40 +02:00
<< std::endl; // NOTE: Don't forget that we need to flush! 😊
const auto next_minute{std::chrono::floor<minutes>(now + 1min)};
std::this_thread::sleep_until(next_minute);
2021-08-05 16:08:40 +02:00
}
}
catch (const std::exception &e)
2021-08-05 16:08:40 +02:00
{
cout << "\n<span color='red'>Error: Time script crashed.</span>\n";
std::cerr << e.what() << '\n';
2021-08-05 16:08:40 +02:00
return 33;
}
}