C – time, difftime, localtime, gmtime, gettimeofday

By | 2022년 2월 21일
Table of Contents

C – time, difftime, localtime, gmtime, gettimeofday

. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . .

time

time() 함수는 현재 시간을 1970-01-01 00:00:00 UTC 로부터 경과된 초(second) 를 반환한다.

리턴값으로도 반환하고, 동시에 참조로 전달한 파라미터에도 입력해 준다.

#include <stdio.h>
#include <time.h>

int main() {
    time_t seconds;

    printf("since 1970-01-01 00:00:00 : %ld (sec)\n", time(&seconds));
    printf("seconds: %ld\n", seconds);

    return 0;
}

difftime

두 시간의 차이값을 반환한다.(seconds)
window 에는 Sleep() 함수를 써야한다.

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

int main() {
    time_t start, end;

    time(&start);
    sleep(3);
    time(&end);

    printf("time diff%.2lf (sec)\n", difftime(end, start));

    return 0;
}

localtime, gmtime, localtime_r

struct tm
{
    int tm_sec;   // seconds after the minute - [0, 60] including leap second
    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
};

함수명 localtime() 에 맞게 UTC 가 아닌 지역시간(KST) 로 시간을 반환한다.
UTC 가 필요한 경우 gmtime() 을 사용한다.

연도는 1900년도부터 경과된 년도를 반환하므로 1900 을 더해 주어야 한다.
월 또한 0월부터 시작하므로 1을 더해 주어야 한다.

멀티스레드 환경에서는 localtime_r() 을 사용한다.

#include <stdio.h>
#include <time.h>

int main() {
    time_t now;
    struct tm *t_now, t_now_multithread;

    time(&now);
    t_now = (struct tm *) localtime(&now);

    printf("year : %d, month : %d, day : %d, hour : %d\n", (t_now->tm_year + 1900), (t_now->tm_mon + 1), t_now->tm_mday, t_now->tm_hour);

    localtime_r(&now, &t_now_multithread);
    printf("year : %d, month : %d, day : %d, hour : %d\n", (t_now_multithread.tm_year + 1900), (t_now_multithread.tm_mon + 1), t_now_multithread.tm_mday, t_now_multithread.tm_hour);

    return 0;
}

gettimeofday (milisecond 구하기)

window 에는 gettimeofday() 함수가 없다.

gettimeofday() 의 두번째 파라미터는 무시된다.
즉, NULL 이외의 다른 값이 입력되어도 작동하지 않는다.

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

int main() {
    struct timeval now;
    struct tm *t_now;

    gettimeofday(&now, NULL);
    t_now = localtime(&now.tv_sec);
    float miliseconds = t_now->tm_sec + now.tv_usec / 1000000.0;

    printf("miliseconds : %0.3f\n", miliseconds);

    return 0;
}

strptime (문자열에서 시간 구하기)

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

int main() {
    struct tm api_tm;
    strptime("2022-03-25 14:42:24", "%Y-%m-%d %H:%M:%S", &api_tm);

    return 0;
}

struct tm to time_t

초기화를 안해주면 오작동할 수 있다.
값을 설정하지 않은 모든 필드는 랜덤값이 할당되고,
그로인해 mktime() 이 -1 을 return 할 수 있다.

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

int main() {
    struct tm api_tm = {0}; // 초가화하는 것이 중요
    strptime("2022-03-25 14:42:24", "%Y-%m-%d %H:%M:%S", &api_tm);

    // localtime 을 기준으로 변환합니다.
    time_t api_t = mktime(&api_tm);

    return 0;
}

5 thoughts on “C – time, difftime, localtime, gmtime, gettimeofday

답글 남기기