Date and time utilities
cppreference.com
< cpp
C++ 은 2종류의 시간 조작 방식을 지원합니다:
chrono라이브러리, 다양한 정밀도로 시간을 다룰 수 있는 유연한 타입 모음 (e.g. std::chrono::time_point).- C언어 방식의 날짜와 시간 라이브러리 (e.g. std::time)
std::chrono library
chrono 라이브러리는 3가지 주요 타입과 도구 함수들 및 범용적인 typedef 들을 정의하고 있습니다.
- 클럭(clocks)
- 시점(time points)
- 기간(durations)
클럭(Clocks)
클럭은 시작 지점(혹은 1970년 1월 1일)과 tick rate 로 구성됩니다. 예를 들어, 클럭은 1970년 1월 1일의 시작점과 매초 값이 바뀔 수 있습니다. C++ 은 몇가지 클럭 타입을 정의하고 있습니다.:
<chrono> 헤더에 정의됨. | |
Defined in namespace
std::chrono | |
(C++11) |
wall clock time from the system-wide realtime clock (class) |
(C++11) |
monotonic clock that will never be adjusted (class) |
(C++11) |
the clock with the shortest tick period available (class) |
(C++20) |
determines if a type is a Clock (class template) (variable template) |
(C++20) |
Clock for Coordinated Universal Time (UTC) (class) |
(C++20) |
Clock for International Atomic Time (TAI) (class) |
(C++20) |
Clock for GPS time (class) |
(C++20) |
Clock used for file time (typedef) |
(C++20) |
pseudo-clock representing local time (class) |
시점(Time point)
시점(time point)는 특정 클럭으로부터 흘러온 시간 간격입니다.
<chrono> 헤더에 정의됨. | |
Defined in namespace
std::chrono | |
(C++11) |
a point in time (class template) |
(C++20) |
traits class defining how to convert time points of one clock to another (class template) |
(C++20) |
convert time points of one clock to another (function template) |
기간(Duration)
기간(duration)은 어떤 시간 단위가 몇 tick 이었는지로 정의된 시간의 간격으로 구성됩니다. 예를 들어, "42초" 는 1초 단위의 42 tick 으로 구성된 시간 간격으로 나타낼 수 있습니다.
<chrono> 헤더에 정의됨. | |
Defined in namespace
std::chrono | |
(C++11) |
a time interval (class template) |
Time of day
hh_mm_ss 는 자정 이후 경과된 시각을 시간,분,초,분수 초로 나눕니다. 주로 형식 지정 도구입니다.
<chrono> 헤더에 정의됨. | |
Defined in namespace
std::chrono | |
(C++20) |
represents a time of day (class template) |
(C++20) |
translates between a 12h/24h format time of day (function) |
달력(Calendar)
<chrono> 헤더에 정의됨. | |
Defined in namespace
std::chrono | |
(C++20) |
tag class indicating the last day or weekday in a month (class) |
(C++20) |
represents a day of a month (class) |
(C++20) |
represents a month of a year (class) |
(C++20) |
represents a year in the Gregorian calendar (class) |
(C++20) |
represents a day of the week in the Gregorian calendar (class) |
(C++20) |
represents the n-th weekday of a month (class) |
(C++20) |
represents the last weekday of a month (class) |
(C++20) |
represents a specific day of a specific month (class) |
(C++20) |
represents the last day of a specific month (class) |
(C++20) |
represents the n-th weekday of a specific month (class) |
(C++20) |
represents the last weekday of a specific month (class) |
(C++20) |
represents a specific month of a specific year (class) |
(C++20) |
represents a specific year, month, and day (class) |
(C++20) |
represents the last day of a specific year and month (class) |
(C++20) |
represents the n-th weekday of a specific year and month (class) |
(C++20) |
represents the last weekday of a specific year and month (class) |
(C++20) |
conventional syntax for Gregorian calendar date creation (function) |
시간대(Time zone)
<chrono> 헤더에 정의됨. | |
Defined in namespace
std::chrono | |
(C++20) |
describes a copy of the IANA time zone database (class) |
(C++20) |
represents a linked list of tzdb (class) |
| accesses and controls the global time zone database information (function) | |
(C++20) |
locates a time_zone based on its name (function) |
(C++20) |
returns the current time_zone (function) |
(C++20) |
represents a time zone (class) |
(C++20) |
represents information about a time zone at a particular time point (class) |
(C++20) |
represents information about a local time to UNIX time conversion (class) |
(C++20) |
selects how an ambiguous local time should be resolved (enum) |
(C++20) |
traits class for time zone pointers used by zoned_time (class template) |
(C++20) |
represents a time zone and a time point (class) |
(C++20) |
contains information about a leap second insertion (class) |
(C++20) |
leap second insertion information (class) |
(C++20) |
obtains leap second insertion information from a utc_time object (function template) |
(C++20) |
represents an alternative name for a time zone (class) |
(C++20) |
exception thrown to report that a local time is nonexistent (class) |
(C++20) |
exception thrown to report that a local time is ambiguous (class) |
chrono I/O
<chrono> 헤더에 정의됨. | |
Defined in namespace
std::chrono | |
(C++20) |
parses a chrono object from a stream (function template) |
C-style date and time library
std::time_t, std::difftime, CLOCKS_PER_SEC와 같은 C언어 형식의 날짜 시간 함수도 제공합니다.
예제
함수의 실행 시간 정보를 보여주는 예제입니다:
코드 실행
#include <iostream>
#include <chrono>
long fibonacci(unsigned n)
{
if (n < 2) return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main()
{
auto start = std::chrono::steady_clock::now();
std::cout << "f(42) = " << fibonacci(42) << '\n';
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n";
}
Possible output:
f(42) = 267914296
elapsed time: 1.88232s