블로그는 나의 힘!
[ Programing ]/C++2011. 6. 29. 23:14

///////////////////////////////////////////////////////////////////////
     #include <sys/time.h>
     #include <unistd.h>

     int gettimeofday( struct timeval *tv, struct timezone *tz );
///////////////////////////////////////////////////////////////////////

현재의 시간을 가져 온다.
해당 시간을 timeval 에 저장한다.

struct timeval {
     long tv_sec;       // 초
     long tv_usec;     // 마이크로 초
};

1970. 01. 01. 부터 현재 까지 시간을 구해 준다.
성공시 0을 실패시 -1을 리턴한다.
윈도우에서 비슷한 것으로 struct time_t 를 이용한 clock_t clock(void) 함수가 있다.

struct tm* localtime( const time_t* )를 응용하여 현재 시간 및 날짜를 구할 수 있다.
계산 딜레이 시간은 대략 178ms 정도 콜링타임을 가진다.
비슷한 시간 출력 함수로 clock_gettime()은 870ms정도 콜링 타임을 가진다.



**************************************************************
   ( 현재 날짜와 시간 출력하는 예제 소스 )

   #include <stdio.h>
   #include <time.h>
   #include <sys/time.h>
   #include <unistd.h>

   int main(int argc, char *argv[])
   { 
        tm *tm;
        timeval tv;
 
        gettimeofday( &tv, NULL );
        tm = localtime( &tv.tv_sec );
 
        printf( "year    : (%d) \n", tm->tm_year +1900 );     // 년
        printf( "month  : (%d) \n", tm->tm_mon +1 );         // 월
        printf( "day     : (%d) \n", tm->tm_mday );           // 일
        printf( "hour    : (%d) \n", tm->tm_hour );             // 시
        printf( "minute : (%d) \n", tm->tm_min );             // 분
        printf( "scond : (%d) \n", tm->tm_sec );              // 초
 
        return EXIT_SUCCESS;
   }
**************************************************************


참고 자료 :
joinc.co.kr/modules/moniwiki/wiki.php/man/2/gettimeofday
charsyam.blog.me/20009290688

기타 자료 ( clock_gettime() ) :
juliusdavies.ca/posix_clocks/clock_realtime_linux_faq.html

Posted by Mister_Q