Created
March 26, 2020 20:03
-
-
Save insooth/db1fbe9d5bf45f598df29b8adcaa3c10 to your computer and use it in GitHub Desktop.
Stringify chrono::time_point
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <ctime> // localtime, time_t, mktime, tm | |
#include <chrono> | |
#include <iomanip> // put_time | |
using namespace std::chrono_literals; | |
int main() | |
{ | |
std::chrono::milliseconds x{1s}; | |
std::cout << x.count() << '\n'; | |
// 1918/11/18 00:00:01 | |
std::tm t; | |
t.tm_sec = 1; | |
t.tm_min = 0; | |
t.tm_hour = 0; | |
t.tm_hour = 0; | |
t.tm_mday = 18; | |
t.tm_mon = 10; | |
t.tm_year = 18; | |
t.tm_wday = 1; | |
t.tm_yday = 322; | |
t.tm_isdst = 1; | |
auto y = std::chrono::system_clock::from_time_t(std::mktime(&t)); | |
auto z = std::chrono::system_clock::to_time_t(y); | |
// ISO 8601 | |
std::time_t tt = std::mktime(&t); | |
std::cout << std::put_time(std::localtime(&tt), "%FT%T%z" ) << '\n'; | |
std::cout << std::put_time(std::localtime(&z), "%FT%T%z" ) << '\n'; | |
return 0; | |
} | |
/* | |
http://coliru.stacked-crooked.com/a/b481641ae46a803c | |
1000 | |
1918-11-18T00:00:01+0000 | |
1918-11-18T00:00:01+0000 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment