Skip to content

Instantly share code, notes, and snippets.

@igricart
Created August 9, 2020 09:46
Show Gist options
  • Save igricart/efcfb3b402bf91a7b165df8b7a94ca7b to your computer and use it in GitHub Desktop.
Save igricart/efcfb3b402bf91a7b165df8b7a94ca7b to your computer and use it in GitHub Desktop.
Write time to a .txt file
#include <iomanip>
#include <chrono>
#include <ctime>
#include <iostream>
#include <string>
#include <fstream>
int main ()
{
auto now = std::chrono::system_clock::now();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
std::cout << ss.str() << std::endl;
std::ofstream file;
file.open("your_example.txt");
file << ss.str() << std::endl;
file.close();
return 0;
}
// in bash
// date "+%Y-%m-%d %H:%M:%S" > TIME.TXT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment