Featured image of post [C++20] Aktuelle Zeit mit std::chrono und std::format als UTC im ISO-8601-Format ausgeben

[C++20] Aktuelle Zeit mit std::chrono und std::format als UTC im ISO-8601-Format ausgeben

Ein vollständiges C++20-Beispiel zur Ausgabe der aktuellen UTC-Zeit im ISO-8601-Format mit std::chrono und std::format, einschließlich Hinweisen zur Bibliotheksunterstützung.

Voraussetzungen

  • ISO C++ 20 Standard (/std:c++20)

Code

Das Beispiel gibt UTC mit Sekundengenauigkeit aus, etwa 2026-11-23T00:00:00Z.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <chrono>
#include <format>
#include <iostream>
#include <string>

int main() {
    const auto now = std::chrono::floor<std::chrono::seconds>(
        std::chrono::system_clock::now());
    const std::string datetime = std::format("{:%FT%TZ}", now);
    std::cout << datetime << '\n';
}

%F formatiert das Datum und %T die Uhrzeit. T und Z im Formatstring sind literale Zeichen; der system_clock-Zeitpunkt wird als UTC formatiert. Bei einer Zeitauflösung unter einer Sekunde können Nachkommastellen erscheinen. Die Chrono-Formatierung gehört zur C++20-Bibliothek, wird aber nicht von jeder Standardbibliothek gleich unterstützt. Prüfen Sie Compiler und Bibliothek.

Referenzen

comments powered by Disqus