#include <time.h>
struct tm *localtime_r(const time_t *timep, struct tm *result);



----DESCRIPTION
localtime_r 함수는 time_t type의 초단위의 시간을 지역시간(locale정보)의 struct tm type으로 변환하는 함수입니다.
localtime()함수가 변환된 결과를 local static변수에 저장하는것과 달리 localtime_r()함수는 data를 변환한 결과값을
result에 저장하기때문에 multi-thread에서도 안전한 thread-safe함수입니다.





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

struct tm *localtime_r(const time_t *timep, struct tm *result);

int main(int argc, char **argv[])
{
    time_t t;
    struct tm lt;
 
    if((t=time(NULL))==-1) {
        printf("time() call error\n");
        return -1;
    }
    if(localtime_r(&t, &lt)==NULL) {
        printf("localtime_r() call error\n");
        return -1;
    }
    printf("NOW TIME:%04d-%02d-%02d %02d:%02d:%02d\n",
    lt.tm_year + 1900,
    lt.tm_mon + 1,
    lt.tm_mday,
    lt.tm_hour,
    lt.tm_min,
    lt.tm_sec);
 
    return 0;

+ Recent posts