Featured image of post [C++20] 使用 std::chrono 和 std::format 將目前時間輸出為 UTC ISO 8601 格式

[C++20] 使用 std::chrono 和 std::format 將目前時間輸出為 UTC ISO 8601 格式

完整的 C++20 範例:使用 std::chrono 和 std::format 將目前時間輸出為 UTC ISO 8601 格式,並說明標準程式庫支援情況。

先決條件

  • ISO C++ 20 標準 (/std:c++20)

程式碼

此範例以秒為精度輸出UTC,例如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用來格式化日期,%T用來格式化時間。格式字串中的T和Z是字面字元;system_clock時間點會以UTC格式化。如果時間點精度高於秒,輸出可能包含小數秒。chrono格式化是C++20標準程式庫功能,但各標準程式庫的實作進度不同。請確認所用編譯器與標準程式庫的支援情況。

參考資料

comments powered by Disqus