Time program - Cpp11
🧩 Syntax:
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <ctime>
namespace chr = std::chrono;
void error(const std::string& function)
{
if (errno)
{
std::string error_str = std::strerror(errno);
std::cerr << function << "(): " << error_str << "\n";
}
else
std::cerr << "Error: " << function << "()\n";
std::exit(EXIT_FAILURE);
}
int main()
{
auto tp = chr::system_clock::now();
std::time_t t = chr::system_clock::to_time_t(tp);
std::tm* p = std::gmtime(&t);
if (!p)
error("std::gmtime");
std::tm utc = *p;
p = std::localtime(&t);
if (!p)
error("localtime");
std::tm local = *p;
std::cout << std::put_time(&utc, "UTC: %F %T\n");
std::cout << std::put_time(&local, "Local (UTC%z): %F %T\n");
}marda
Member