chrono는 시간을 다루는 C++ 표준 라이브러리이다.
<chrono> 헤더
std::chrono namespace 사용
1. chrono의 alias
- chrono에는 시간 관련한 타입이 존재
#include <chrono>
int main () {
std::chrono::hours h(10);
std::chrono::miuntes m(10);
std::chrono::seconds s1(10);
std::chrono::milliseconds s2(10);
std::chrono::nanoseconds s3(10);
// 위의 시간 관련한 type은 모두 duration<>의 alias이다.
// std::chrono::duration<long long> d1(10); // s1(10)과 동일한 의미
}
using nanoseconds = duration<long long, nano>;
using microseconds = duration<long long, micro>;
using milliseconds = duration<long long, milli>;
using seconds = duration<long long>;
using minutes = duration<int, ratio<60>>;
using hours = duration<int, ratio<3600>>;
2. chrono의 user define literal
- 10s, 10ms, 1ns, 10min, 10h 등
- using namespace std::literals
#include <thread>
#include <chrono>
using namespace std::literals;
int main () {
// 모두 다 같은 표현
std::this_thread::sleep_for(std::chrono::duration<long long>(10));
std::this_thread::sleep_for(std::chrono::seconds(10));
std::this_thread::sleep_for(10s);
}
3. chrono의 time_point
#include <iostream>
#include <chrono>
int main () {
std::chrono::time_point tp1 = std::chrono::system_clock::now();
// 1970년 1월 1일 이후에 몇시간이 지났는지
// epoch time은 1970년 1월 1일을 말함.
std::chrono::hours h = std::chrono::duration_cast<std::chrono::hours>(tp1.time_since_epoch());
std::cout << h.count() << std::endl;
}
728x90
'Language > C++' 카테고리의 다른 글
[C++] std::ref, std::reference_wrapper (1) | 2023.09.01 |
---|---|
[C++] std::thread (0) | 2023.09.01 |
[C++] std::this_thread (0) | 2023.09.01 |
[C++] 난수 생성하기 (0) | 2023.07.21 |
[C++] vector<pair<int, vector>> 일때 push 하는 방법. (0) | 2022.06.08 |
댓글