time formatting 관련 함수 본문

Programming/C

time formatting 관련 함수

halatha 2010. 1. 22. 14:01

  • getdate

http://www.xnet.fi/susv3/functions/getdate.html

http://old.nabble.com/getdate%283%29---format-date-td6441921.html

http://www.kernel.org/doc/man-pages/online/pages/man3/getdate.3.html

http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.bpxbd00/rgdate.htm

#define _GNU_SOURCE 500
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    struct tm *tmp;
    int j;

    for (j = 1; j < argc; j++) {
        tmp = getdate(argv[j]);
        //getdate_r(argv[j], tmp);

        if (tmp == NULL) {
            printf("Call %d failed; getdate_err = %d\n",
                    j, getdate_err);
            continue;
        }

        printf("Call %d (\"%s\") succeeded:\n", j, argv[j]);
        printf("    tm_sec   = %d\n", tmp->tm_sec);
        printf("    tm_min   = %d\n", tmp->tm_min);
        printf("    tm_hour  = %d\n", tmp->tm_hour);
        printf("    tm_mday  = %d\n", tmp->tm_mday);
        printf("    tm_mon   = %d\n", tmp->tm_mon);
        printf("    tm_year  = %d\n", tmp->tm_year);
        printf("    tm_wday  = %d\n", tmp->tm_wday);
        printf("    tm_yday  = %d\n", tmp->tm_yday);
        printf("    tm_isdst = %d\n", tmp->tm_isdst);

    }

    exit(EXIT_SUCCESS);
}

compile시 getdate 사용을 위한 option이 필요한데, 어느 system에서 어떤 것이 정확히 필요한 option인지는 모르겠다. 여기서는 _GNU_SOURCE으로 해결했지만, 웹에서는 _XOPEN_SOURCE, _XOPEN_SOURCE_EXTENDED등의 것도 볼 수 있었다.

Usage:
1. DATEMSK를 위해 사용할 format 파일을 생성(format의 자세한 것은 위의 link나 man page에서 볼 것. 현재 사용한 예는 4자리 연도, 2자리 월, 일, 시간, 분 순서이고 초 부분은 00으로 고정)
$ vi format
%Y%m%d%H%M00
2. DATEMSK 환경 변수 설정(program 내에서 setenv function으로 할 수 있을지는 아직 테스트해보지 않았음)
export DATEMSK=./format
3. compile & execution
gcc test.c -o test; ./test "20100122174000"

  • getdate_r

man page의 설명에 따르면 getdate는 결과를 static variable에 저장하기 때문에 overwritten의 위험성이 존재
getdate와 마찬가지로 DATEMSK 필요
아직 test해보지 않았음

  • strptime

http://kldp.org/node/44771

#define _XOPEN_SOURCE 1

#include
#include
#include

int main()
{
struct tm* tmp = (struct tm*)malloc(sizeof(struct tm) * 1);
char *buffer = "200901201740";

strptime(buffer, "%Y%m%d%H%M", tmp);

printf(" tm_sec = %d\n", tmp->tm_sec);
printf(" tm_min = %d\n", tmp->tm_min);
printf(" tm_hour = %d\n", tmp->tm_hour);
printf(" tm_mday = %d\n", tmp->tm_mday);
printf(" tm_mon = %d\n", tmp->tm_mon);
printf(" tm_year = %d\n", tmp->tm_year);
printf(" tm_wday = %d\n", tmp->tm_wday);
printf(" tm_yday = %d\n", tmp->tm_yday);
printf(" tm_isdst = %d\n", tmp->tm_isdst);

free(tmp);

return 0;
}

Usage:
compile시 option이 필요한데, 어느 system에서 어떤 option이 정확히 필요한지는 모르겠음. 여기서는 _XOPEN_SOURCE 사용
변환을 원하는 string과 두 번째 인자의 format은 getdate에서와 마찬가지

  • example(char* > struct tm > time_t > struct tm > char*)



#define _XOPEN_SOURCE 1

#include 
#include 
#include 
#include 

void printTm(struct tm p_stTm)
{
    printf("\ttm_sec   = %d\n", p_stTm.tm_sec);
    printf("\ttm_min   = %d\n", p_stTm.tm_min);
    printf("\ttm_hour  = %d\n", p_stTm.tm_hour);
    printf("\ttm_mday  = %d\n", p_stTm.tm_mday);
    printf("\ttm_mon   = %d\n", p_stTm.tm_mon);
    printf("\ttm_year  = %d\n", p_stTm.tm_year);
    printf("\ttm_wday  = %d\n", p_stTm.tm_wday);
    printf("\ttm_yday  = %d\n", p_stTm.tm_yday);
    printf("\ttm_isdst = %d\n", p_stTm.tm_isdst);
}

int main()
{
    char const * const  pcFmt   = "%Y%m%d%H%M";
    char*   pcTime  = "200809132230";
    struct tm*  pstTm = (struct tm*)malloc(sizeof(struct tm) * 1);
    time_t  stTime_t;

    printf("string: %s\n", pcTime);
    printf("\n\n\n--- 1. strptime() converts char* into struct tm\n");
    strptime(pcTime, pcFmt, pstTm);
    printTm(*pstTm);

    printf("\n\n\n--- 2. mktime() converts struct tm into time_t\n");
    stTime_t    = mktime(pstTm);
    printf("time_t\t= %ld\t%s", stTime_t, ctime(&stTime_t));
    stTime_t    -= 90 * 60;
    //stTime_t  -= 12 * 60 * 60;
    //stTime_t  -= 15 * 24 * 60 * 60;
    printf("time_t\t= %ld\t%s", stTime_t, ctime(&stTime_t));

    printf("\n\n\n--- 3. gmtime_r() converts time_t into struct tm\n");
    //pstTm = gmtime(&stTime_t);
    //gmtime_r(&stTime_t, pstTm);
    localtime_r(&stTime_t, pstTm);
    printTm(*pstTm);

    printf("\n\n\n--- 4. strftime() converts struct tm into char*\n");
    char    pcTmp[13];
    bzero(pcTmp, sizeof(pcTmp));
    strftime(pcTmp, sizeof(pcTmp), pcFmt, pstTm);
    printf("%s\n", pcTmp);

    free(pstTm);

    return  0;
}
Comments