programming/C_C++
                
              gettimeofday를 대체하는 clock_gettime 함수
                공간사랑
                 2017. 12. 20. 14:43
              
              
            
            gettimeofday를 대체하는 clock_gettime 함수
#include <time.h>int clock_gettime(clockid_t clock_id, struct timespec *tp);    struct timespec {            time_t   tv_sec;        /* seconds */            long     tv_nsec;       /* nanoseconds */    };--------------------------------------------------------------#include <sys/time.h>int gettimeofday(struct timeval *restrict tp, void *restrict tzp);    struct timeval {            time_t      tv_sec;     /* seconds */            suseconds_t tv_usec;    /* microseconds */    };
clockid_t 타입
| CLOCK_REALTIME | 시스템 전역의 실제 시계(The UNIX Epoch 시간) | 
| CLOCK_MONOTONIC | 단조 시계 (특정 시점에서 흐른 시간, 대체적으로 부팅후 시간) | 
| CLOCK_PROCESS_CPUTIME_ID | 프로세스 단위 CPU 사용 시간 | 
| CLOCK_THREAD_CPUTIME_ID | 쓰레드 단위 CPU 사용 시간 | 
#define STR_TIME_FORMAT      "%y-%m-%d/%H:%M:%S"char  buf[32];size_t sz_buf = sizeof(buf);struct timespec tspec;struct tm   tm_now;/* 리얼타임 시계로부터 현재 시간을 구한다. */if (clock_gettime(CLOCK_REALTIME, &tspec) == -1) {      /* 에러 처리*/}/* timespec 구조체의 초수 필드(tv_sec)를 tm 구조체로 변환 */localtime_r((time_t *)&tspec.tv_sec, &tm_now); /* 문자열로 저장 */if (strftime(buf, sz_buf, STR_TIME_FORMAT, &tm_now) == 0) {     /* 에러 처리*/}
출처: http://sunyzero.tistory.com/161 [Programmer]