반응형

ctime(3)

#include <time.h>

char *ctime(const time_t *timep);

초단위 시간 데이터(time_t)를 날짜 및 시간 표시 문자열로 변환합니다. 문자열의 format은 "Wed Jan 25 15:52:09 2017\n"형태로 끝에 new line이 붙습니다. 변환된 문자열은 local static변수로 저장되므로 ctime(3)을 다시 실행하면 내용이 변경됩니다. multi-thread에서는 문제가 발생할 수 있으므로 ctime_r(3)을 사용해야 합니다.

 

 

파라미터

timep
    -  일반적으로 time(2)에서 얻거나 gettimeofday(2)에서 얻은 struct timeval의 tv_sec 값입니다.

 

RETURN

NULL 아님
    - "Wed Jan 25 15:52:09 2017\n" 형태의 문자열을 return 합니다.
    - 문자열은 static local 변수를 사용하므로 다음 ctime(3)호출되면 다른 값으로 변경됩니다.

   특히, multi-thread에서는 문제가 발생할 수 있으므로 ctime_r(3)을 사용할 것을 권고합니다.


NULL
    - 변환 오류가 발생하였습니다.

 


활용 예제

 

Sample

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

int main(int argc, char **argv)
{
    time_t t;

    if((t = time(NULL)) == -1) {
        perror("time() call error");
        return -1;
    }

    /* new line이 자동으로 붙기 때문에 %s 뒤에 \n가 필요없음 */
    printf("ctime() : %s", ctime(&t));

    return 0;
}


결과:
ctime() : Wed Jan 25 17:05:23 2017

 


 

see also : 시간 관련 함수

 

 

 

반응형
블로그 이미지

자연&사람

행복한 개발자 programmer since 1995.

,