duration_formatter

duration_formatter (duration 포맷터)

std::formatter<std::chrono::duration> 특수화가 std::chrono::duration의 포맷 규칙을 정의해요. std::format과 함께 사용해요.

출처: cppreference

본문

std::formatter<std::chrono::duration> 특수화는 <chrono> 헤더에 정의되어 있어요. C++20부터 사용 가능해요.

template< class Rep, class Period, class CharT >
struct std::formatter<std::chrono::duration<Rep, Period>, CharT>;

std::chrono::duration의 포맷 규칙을 정의해요. duration은 자정 이후의 시간으로 해석돼요. 이 std::formatter 특수화는 보통 직접 접근하지 않고 std::format 같은 포맷 함수를 통해 사용돼요.

포맷 지정 (Format specification)

%H, %M, %S 같은 시간 포맷 스펙을 지원해요. 예를 들어 "{:%H:%M}"은 시:분으로 포맷해요.

예제 (Example)

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

int main()
{
    std::chrono::seconds d{3661};
    std::cout << std::format("{:%H:%M:%S}", d) << '\n'; // 01:01:01
}

더 알아보기 (Learn more)

cppreference