C++ 표준 라이브러리는 적절한 날짜 유형을 제공하지 않습니다. C++는 C로부터 날짜 및 시간 조작을 위한 구조 및 함수를 상속합니다. 날짜 및 시간 관련 함수 및 구조에 액세스하려면 C++ 프로그램에
네 가지 시간 관련 유형이 있습니다:clock_t, time_t, size_t 및 tm. clock_t, size_t 및 time_t 유형은 시스템 시간과 날짜를 일종의 정수로 나타낼 수 있습니다.
구조 유형 tm은 다음 요소를 갖는 C 구조의 형태로 날짜와 시간을 보유합니다. -
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
} 현재 시스템 날짜 및 시간을 현지 시간 또는 UTC(협정 세계시)로 검색한다고 가정합니다. 다음은 동일한 것을 달성하는 예입니다 –
예시
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
char* dt = ctime(&now); // convert now to string form
cout << "The local date and time is: " << dt << endl;
// convert now to tm struct for UTC
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "The UTC date and time is:"<< dt << endl;
} 출력
The local date and time is: Fri Mar 22 13:07:39 2019 The UTC date and time is:Fri Mar 22 07:37:39 2019