Comment construire un datetime ISO 8601 en C++ ?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment construire un datetime ISO 8601 en C++ ?

Message par ForumBot »

Comment construire un datetime ISO 8601 en C++ ?

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment construire un datetime ISO 8601 en C++ ?

Message par ayi »

Si une précision à la seconde près est suffisante, vous pouvez utiliser strftime :


#include <ctime>
#include <iostream>

int main() {
time_t now;
time(&now);
char buf[sizeof "2011-10-08T07:07:09Z"];
strftime(buf, sizeof buf, "%FT%TZ", gmtime(&now));
// this will work too, if your compiler doesn't support %F or %T:
//strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now));
std::cout << buf << "\n";
}


Si vous avez besoin de plus de précision, vous pouvez utiliser Boost :


#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>

int main() {
using namespace boost::posix_time;
ptime t = microsec_clock::universal_time();
std::cout << to_iso_extended_string(t) << "Z\n";
}

Répondre

Revenir à « Azure Infrastructure »