2021-08-05 16:08:40 +02:00
|
|
|
// Print date and time for i3 status bar.
|
2021-08-23 13:18:29 +02:00
|
|
|
/* i3blocks config:
|
|
|
|
* [time]
|
|
|
|
* command=statustime
|
|
|
|
* interval=persist
|
|
|
|
* markup=pango
|
|
|
|
*/
|
2021-08-05 16:08:40 +02:00
|
|
|
|
|
|
|
#include <fmt/chrono.h>
|
|
|
|
#include <fmt/core.h>
|
|
|
|
|
|
|
|
#include <chrono>
|
2021-08-14 21:28:06 +02:00
|
|
|
#include <csignal>
|
2021-08-05 16:08:40 +02:00
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
|
|
|
#include <locale>
|
|
|
|
#include <thread>
|
|
|
|
|
2021-08-14 21:28:06 +02:00
|
|
|
void signal_handler(int signum)
|
|
|
|
{
|
|
|
|
switch (signum)
|
|
|
|
{
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGINT:
|
|
|
|
{
|
2021-08-15 15:40:38 +02:00
|
|
|
std::cout << R"(<span color="red">Time script terminated.</span>)"
|
|
|
|
<< std::endl;
|
2021-08-14 21:28:06 +02:00
|
|
|
std::exit(signum); // NOLINT(concurrency-mt-unsafe)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 16:08:40 +02:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
using fmt::format;
|
|
|
|
using std::cout;
|
2021-08-15 15:40:38 +02:00
|
|
|
using std::endl;
|
2021-08-14 18:59:59 +02:00
|
|
|
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"));
|
|
|
|
|
2021-08-14 21:28:06 +02:00
|
|
|
std::signal(SIGINT, signal_handler);
|
|
|
|
std::signal(SIGTERM, signal_handler);
|
|
|
|
|
2021-08-05 16:08:40 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
2021-08-14 18:59:59 +02:00
|
|
|
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>)",
|
2021-08-14 18:59:59 +02:00
|
|
|
now)
|
2021-08-15 15:40:38 +02:00
|
|
|
<< endl; // NOTE: Don't forget that we need to flush! 😊
|
2021-08-14 18:59:59 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-08-06 12:42:32 +02:00
|
|
|
catch (const std::exception &e)
|
2021-08-05 16:08:40 +02:00
|
|
|
{
|
2021-08-15 15:40:38 +02:00
|
|
|
cout << R"(<span color="red">Time script crashed.</span>)" << endl;
|
2021-08-06 12:42:32 +02:00
|
|
|
std::cerr << e.what() << '\n';
|
2021-08-05 16:08:40 +02:00
|
|
|
return 33;
|
|
|
|
}
|
|
|
|
}
|