C++17 - 시간 반올림 연산

밀리 세컨드를 세컨드 단위로 변경할 때 딱 맞아 떨어지지 않는 경우 반올림 혹은 내림을 할 수 있는 floor(내림), ceil(올림), round(가장 근접한 수로)가 생겼다.

#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    milliseconds ms(2500);
    
    seconds sec1 = round<seconds>(ms); // 가장 근접한 수로
    std::cout << sec1.count() << std::endl; // 2
    
    seconds sec2 = floor<seconds>(ms); // 내림
    std::cout << sec2.count() << std::endl; // 2
    
    seconds sec3 = ceil<seconds>(ms); // 올림
    std::cout << sec3.count() << std::endl; // 3
}

이 글은 2020-05-21에 작성되었습니다.