[펌] unix 에서 file의 시간 초단위로 얻기

그냥 명령어가 있을듯도 하나.. 몰라서.

## 실행예
$ ./a.out a.out 
argc=2
### a.out ###
        Time Creation     : 2008-08-14 13:50:24
        Time Last Written : 2008-08-14 13:50:24
        Time Last Access  : 2008-08-14 13:51:07

## 소스

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>

char* timeToString(struct tm *t);


int main(int argc, char *argv[])
{
    struct stat buf;
    char fname[1024];


    fprintf(stdout, "argc=%d\n", argc);
    if (argc == 2) {
        strcpy(fname, argv[1]);
    }

    if ( stat(fname, &buf) != 0 ) {
        switch (errno) {
        case ENOENT:
            fprintf(stdout, "File %s not found.\n", fname); break;
        case EINVAL:
            fprintf(stdout, "Invalid parameter to _stat.\n"); break;
        default:
            fprintf(stdout, "Unexpected error in _stat.\n");
        }
    }
    else {
        printf("### %s ### \n", fname);
        printf( "\tTime Creation     : %s\n", timeToString(localtime(&buf.st_ctime)) );
        printf( "\tTime Last Written : %s\n", timeToString(localtime(&buf.st_mtime)) );
        printf( "\tTime Last Access  : %s\n", timeToString(localtime(&buf.st_atime)) );
    }

    return 0;
}

char* timeToString(struct tm *t)
{
    static char s[20];

    sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
            t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
            t->tm_hour, t->tm_min, t->tm_sec
           );

    return s;
}

by 오서비네 | 2008/08/14 15:29 | C++ | 트랙백

<< 이전 페이지     다음 페이지 >>