'programming/C_C++'에 해당되는 글 279건

  1. 2016.05.03 clock_gettime localtime_r

반응형

/*** 컴파일시에 -lrt 추가 ***/


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

 

int main(int argc, char *argv[])
{

    struct timespec tspec;
    struct tm       tm_now;
    char   sDateTime[128]={'\0'};

 

    clock_gettime(CLOCK_REALTIME, &tspec);

    localtime_r((time_t *)&tspec.tv_sec, &tm_now);

 

    memset(sDateTime, 0x00, sizeof(sDateTime));
    strftime(sDateTime, sizeof(sDateTime), "%Y%m%d%H%M%S", &tm_now);

    fprintf(stdout, "%s\n", sDateTime);

 

    memset(sDateTime, 0x00, sizeof(sDateTime));

    sprintf(sDateTime, "%04d%02d%02d%02d%02d%02d", tm_now.tm_year+1900, tm_now.tm_mon+1, tm_now.tm_mday,tm_now.tm_hour, tm_now.tm_min, tm_now.tm_sec);

    fprintf(stdout, "%s\n", sDateTime);

}

반응형
Posted by 공간사랑
,