스샷을 저장할 일이 생겨서, 스샷을 저장하려다 보니.. 각 찍힌 시간을 파일 명으로 만들고 싶어졌다....

생각보다 api 함수를 이용하면 시간을 얻기는 간단하다.

일단..

<time.h> 를 선언한다. 아래의 함수들은 각 시간을 얻는 함수들 이다.

_strtime : 현재 시간을 문자열로 만들어주는 함수이다. 함수원형은..

char* _strtime( char *time );

이다.

char time[9];

_strtime( time );
// 11:23:24 (시:분:초)형식이며, 24시간으로 표시된다.

_strdate : 오늘 날짜를 문자열로 만들어 주는 함수이다. 함수 원형은..

char* _strdate( char *date );

이다..

char data [9];

_strdate( data );
// 08/05/11 (월/일/년)형식으로 표시된다.

strftime : 이 함수는 사용자가 지정한 형식대로 현재시간을 문자열로 출력하는 함수이다. 함수 원형은..

size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );

이다..

time_t cur;
struct tm* ptm;
char buf[100] = {0};

cur = time(NULL);
ptm = localtime(&cur);

strftime(buf, sizeof(buf), "%c", ptm);
//08/05/11 11:30:05
strftime(buf, sizeof(buf), "%m/%d/%y %H:%M:%S", ptm);
//08/05/11 11:30:05
strftime(buf, sizeof(buf), "%Y년 %#m월 %#d일 %#I시 %#M분 %#S초", ptm);
//2011년 08월 05일 11시 30분 5초
strftime(buf, sizeof(buf), "%I:%M %p", ptm);
//11:30 AM

// 위 함수를 사용하기 위해서는 localtime(...); 함수로 시간을 우선 얻어와야 한다.

Posted by 바람처럼..
|