본문 바로가기

프로그래밍 언어

[C] C struct timeval

※ 이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.




헤더파일 설명

 - struct timeval은 bit/time.h 헤더파일에 정의 되어있다. 
 - bit/time.h 헤더파일은 sys/time.h 헤더파일에 선언되어 있다. 
 
 형식
strcut timeval
{
    __time_t tv_sec;                /* Seconds. */
    __suseconds_t tv_usec;    /* Microseconds. */
}
예제
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main(void){
    struct timeval tv;
    struct tm *ptm;

    gettimeofday(&tv, NULL);
    ptm = localtime(&tv.tv_sec);

    printf("%d.%d\n", tv.tv_sec, tv.tv_usec);
    printf("%d05d-%02d-%02d %d02d:%02d:%02d.%06ld\n"
            , ptm->tm_year + 1900
            , ptm->tm_mon + 1
            , ptm->tm_mday
            , ptm->tm_hour
            , ptm->tm_min
            , ptm->tm_sec
            , tv.tv_usec);

    return 0;
}
type 확인
__time_t 타임은 8byte로 long 타입이다. 
__suseconds 타입은 8byte로 long 타입이다.
#include <stdio.h>
#include <sys/time.h>

int main(void){
    sturct timeval tv;

    printf("%d\n", sizeof(tv.tv_sec));
    printf("%d\n", sizeof(tv.tv_usec));

   return 0;
}