std::formatter

std::formatter (std::chrono::zoned_time 특수화)

std::chrono::zoned_time 값을 포맷할 수 있게 해주는 std::formatter 특수화예요. std::formatstd::print에서 시간대가 적용된 시각을 출력할 수 있게 해줘요. C++20부터 있어요.

출처: cppreference

본문

<chrono> 헤더에 정의돼 있고, std::formatter<std::chrono::zoned_time<Duration, TimeZonePtr>> 특수화예요. std::format·std::print 계열에서 zoned_time을 chrono 형식 지정 문법에 따라 포맷해요. 이 값은 위키의 시간대 규칙에 따라 출력되며, chrono 포맷 지정자들의 완전한 집합(%Y, %m, %d, %H, %M, %S, %z(오프셋), %Z(약어) 등)을 지원해요.

#include <chrono>
#include <format>
#include <iostream>

int main()
{
    using namespace std::chrono;
    zoned_time zt{"Asia/Seoul", system_clock::now()};
    std::cout << std::format("{:%Y-%m-%d %H:%M:%S %Z}", zt) << '\n';
}

가능한 출력:

2023-06-30 04:58:34 KST

이렇게 형식 문자열로 zoned_time의 날짜·시간·시간대 약어를 유연하게 출력할 수 있어요.

더 알아보기 (Learn more)

cppreference