#define _CRT_SECURE_NO_WARNINGS // needed by msvc to suppress issue with std::localtime which is not safe.
#include <chrono>
#include <iostream>
#include <iomanip>
#include <thread>
#include <string_view>
using namespace std::chrono_literals;
std::string time_to_str(const std::chrono::system_clock::time_point& time, const std::string_view format)
{
std::time_t tt = std::chrono::system_clock::to_time_t(time);
std::tm tm = *std::localtime(&tt); //Locale time-zone, usually UTC by default.
std::stringstream ss;
ss << std::put_time(&tm, format.data());
return ss.str();
}
int main()
{
while (true)
{
// in C++20 with format support you can use std::format
//auto s = std::format("{:%Y/%m/%d %H:%M:%S}", std::chrono::system_clock::now());
auto s = time_to_str(std::chrono::system_clock::now(), "%Y/%m/%d %H:%M:%S");
std::cout << s << "\r";
std::cout << std::flush; // needed for onlinegdb to show output
std::this_thread::sleep_for(1s);
}
}