본문 바로가기

C, C++

시간 함수

time 함수를 이용해서 현재 시간을 반환할 수 있습니다.

#include <stdio.h>
#include <time.h>//이 헤더가 포함되야 합니다.

int main() {
	time_t now = time(nullptr);//time(&now);도 가능합니다. 인수와 반환이 제공하는 값이 똑같습니다.
	__time32_t  now32 = _time32(nullptr);//time의 32비트 버전
	__time64_t  now64 = _time64(nullptr);//time의 64비트 버전


	clock_t cl = clock();
	printf("프로그램 시작 후 경과 시간(초) : %f\n", (double)cl / CLOCKS_PER_SEC);


	time_t now2 = time(nullptr);
	__time32_t  now2_32 = _time32(nullptr);
	__time64_t  now2_64 = _time64(nullptr);

	double diff = difftime(now, now2);//두 시간의 차이를 반환
	double diff32 = _difftime32(now2_32, now32);//32비트 버전
	double diff64 = _difftime64(now2_64, now64);//64비트 버전


	char ctimeBuf[64];
	ctime_s(ctimeBuf,64,&now);
	printf(ctimeBuf);//문자열에 개행 문자가 포함되있습니다.

	_ctime32_s(ctimeBuf, 64, &now32);//ctime_s의 32비트 버전
	//ctimeBuf가 배열일 경우 _ctime32_s(ctimeBuf, &now32)도 가능합니다.
	_ctime64_s(ctimeBuf, 64, &now64);//ctime_s의 64비트 버전
	//ctimeBuf가 배열일 경우 _ctime64_s(ctimeBuf, &now64)도 가능합니다.

	wchar_t wctimeBuf[64];
	_wctime_s(wctimeBuf,64, &now);//ctime_s의 유니코드 버전

	_wctime32_s(wctimeBuf,64, &now32);//_wctime_s의 32비트 버전
	//wctimeBuf가 배열일 경우 _wctime32_s(wctimeBuf, &now32)도 가능합니다.
	_wctime64_s(wctimeBuf,64, &now64);//_wctime_s의 64비트 버전
	//wctimeBuf가 배열일 경우 _wctime64_s(wctimeBuf, &now64)도 가능합니다.


	char strDate[16];
	char strTime[16];

	_strdate_s(strDate,16);//strDate가 배열일 경우 _strdate_s(strDate)도 가능합니다.
	_strtime_s(strTime, 16);//strTime이 배열일 경우 _strtime_s(strTime)도 가능합니다.

	printf("현재 날짜:%s, 시간:%s\n", strDate,strTime);

	wchar_t wstrDate[16];
	wchar_t wstrTime[16];

	_wstrdate_s(wstrDate, 16);//_strdate_s의 유니코드 버전
	//wstrDate가 배열일 경우 _wstrdate_s(wstrDate)도 가능합니다.
	_wstrtime_s(wstrTime, 16);//_strtime_s의 유니코드 버전
	//wstrTime이 배열일 경우 _wstrtime_s(wstrTime)도 가능합니다.


	tm timeStruct;
	gmtime_s(&timeStruct,&now);

	printf("세계 표준시 %d년 %d월 %d일 %d시 %d분 %d초\n", timeStruct.tm_year+1900,timeStruct.tm_mon+1,timeStruct.tm_mday,timeStruct.tm_hour,timeStruct.tm_min,timeStruct.tm_sec);

	_gmtime32_s(&timeStruct, &now32);//gmtime_s의 32비트 버전
	_gmtime64_s(&timeStruct, &now64);//gmtime_s의 64비트 버전


	localtime_s(&timeStruct, &now);

	printf("지역 시(현재 시간) %d년 %d월 %d일 %d시 %d분 %d초\n", timeStruct.tm_year + 1900, timeStruct.tm_mon + 1, timeStruct.tm_mday, timeStruct.tm_hour, timeStruct.tm_min, timeStruct.tm_sec);

	_localtime32_s(&timeStruct, &now32);//localtime_s의 32비트 버전
	_localtime64_s(&timeStruct, &now64);//localtime_s의 64비트 버전


	char asctimeBuf[64];
	asctime_s(asctimeBuf, 64, &timeStruct);//tm 구조체를 문자열로 변환합니다.
	//asctimeBuf이 배열일 경우 asctime_s(asctimeBuf, &timeStruct)도 가능합니다.
	printf(asctimeBuf);//문자열에 개행 문자가 포함되있습니다.

	wchar_t wasctimeBuf[64];
	_wasctime_s(wasctimeBuf, 64, &timeStruct);//asctime_s의 유니코드 버전
	//wasctimeBuf이 배열일 경우 _wasctime_s(wasctimeBuf, &timeStruct)도 가능합니다.


	char strftimeBuf[64];
	strftime(strftimeBuf, 64, "%Y년 %m월 %d일 %H시 %M분 %S초\n", &timeStruct);//세 번째 인수로 format을 주고 네 번째 인수로 tm구조체를 주고 tm과 format에 맞추어 문자열을 만들어서 strftimeBuf에 복사합니다.
	printf(strftimeBuf);
    
    return 0;
}

출력 결과 :

Tue Jan 15 15:04:48 2019

현재 날짜:01/15/19, 시간:15:04:48

세계 표준시 2019년 1월 15일 6시 4분 48초

지역 시(현재 시간) 2019년 1월 15일 15시 4분 48초

Tue Jan 15 15:04:48 2019

2019년 01월 15일 15시 04분 48초

프로그램 시작 후 경과 시간(초) : 0.003000

32비트 버전과 64비트 버전은

원래 함수가 운영체제가 32비트일 경우 내부에서 32비트 버전 함수를 출력하고 64비트 버전일 경우 64비트 버전의 함수를 출력합니다.

tm 구조체 멤버 목록은 다음과 같습니다.

tm_sec 48
tm_min 4
tm_hour 시(24시) 15
tm_mday 일(1-31) 15
tm_mon 월(0-11) 1
tm_year 년(1900년 이후 경과 년) 119
tm_wday 요일(0-6, 0이 일요일) 2
tm_yday 연도의 일 165
tm_isdst 서머 타임

strftime의 포맷 목록은 다음과 같습니다.

%Y 2019
%m 월(01-12, 앞에 0이 붙음) 01
%B 영어 월(풀네임) January
%b, %h 영어 월(줄임말) Jan
%A 영어 요일(풀네임) Tuesday
%a 영어 요일(줄임말) Tue
%d 일(앞에 0이 붙음) 01 또는 15
%e 일(앞에 0이 안 붙음) 1 또는 15
%H 시(24시, 앞에 0이 붙음) 14 또는 03
%I(대문자 i) 시(12시, 앞에 0이 붙음) 02 또는 10
%M 분(앞에 0이 붙음) 53 또는 02
%S 초(앞에 0이 붙음) 30 또는 09
%p AM 또는 PM PM
%j 연도의 일(앞에 0이 붙음) 165 또는 005
%y 연도 마지막 2글자(앞에 0이 붙음) 19 또는 01

참고 :

http://www.cplusplus.com/reference/ctime/tm/

http://www.cplusplus.com/reference/ctime/strftime/

'C, C++' 카테고리의 다른 글

랜덤 함수  (0) 2020.02.04
수학 함수  (0) 2020.01.09
nullptr(NULL)  (0) 2020.01.07
동적 할당  (0) 2020.01.06
goto  (0) 2019.12.27