본문 바로가기

카테고리 없음

[C++] 시간 구조체(time struct)

void main()
{
	time_t t;
	tm *pt;

	time(&t);
	pt = localtime(&t);
	cout << "현재 시간 " << pt->tm_year + 1900 << "년 ";
	cout <<  pt->tm_mon + 1 << "월 ";
	cout <<  pt->tm_mday << "일 ";
	cout <<  pt->tm_hour << "시 ";
	cout <<  pt->tm_min << "분 ";
	cout <<  pt->tm_sec << "초 " << endl;;
}

struct tm 
{
	int tm_sec;	/* seconds after the minute - [0,59] */
	int tm_min;	/* minutes after the hour - [0,59] */
	int tm_hour;	/* hours since midnight - [0,23] */
	int tm_mday;	/* day of the month - [1,31] */
	int tm_mon;	/* months since January - [0,11] */
	int tm_year;	/* years since 1900 */
	int tm_wday;	/* days since Sunday - [0,6] */
	int tm_yday;	/* days since January 1 - [0,365] */
	int tm_isdst;	/* daylight savings time flag */
};

프로그램 실행화면


바로 전에 time()함수를 사용하는데 있어서 불편한 점이 바로 반환 값이 초 단위로 반환된다는 것이었다. 그래서 현재 시간을 알아내기 위해서는 가공을 해 주어야 한다고 했다.


위에 있는 프로그램이 바로 그 가공해 주는 부분이다. 년도, 월, 일, 시, 분, 초 단위로 변환할 수 있다. tm 구조체의 내용도 주석처리로 넣어 놓았다. 참고해서 사용하면 좋을 것 같다.