반응형
ctime_r(3)
#include <time.h>
char *ctime_r(const time_t *timep, char *buf);
초단위의 시간(time_t)을 날짜 및 시간 표시 문자열로 변환합니다. 문자열의 format은 "Wed Jan 25 15:52:09 2017\n"형태로 끝에 new line이 붙습니다. 변환된 문자열은 buf에 저장되므로 buf의 크기는 null-terminate값까지 최소 26바이트 이상이 되어야 합니다. 이 함수는 ctime(3)과는 달리 multi-thread에서도 안전한 thread safe 함수입니다.
파라미터
timep
- 일반적으로 time(2)에서 얻거나 gettimeofday(2)에서 얻은 struct timeval의 tv_sec 값입니다.
buf
- 초단위의 시간(time_t)을 문자열 format("Wed Jan 25 15:52:09 2017\n")으로
변환된 데이터가 저장할 buffer입니다.
- buf는 최소 26바이트 이상되어야 합니다.
RETURN
NULL 아님
- buf
NULL
- 변환 오류가 발생하였습니다.
활용 예제
Sample
#include <time.h>
#include <stdio.h>
int main(int argc, char **argv)
{
time_t t;
char time_str[26];
if((t = time(NULL)) == -1) {
perror("time() call error");
return -1;
}
/* new line이 자동으로 붙기 때문에 %s 뒤에 \n가 필요없음 */
if(ctime_r(&t, time_str) != NULL) {
printf("ctime_r() : %s", time_str);
}
return 0;
}
결과:
ctime_r() : Wed Jan 25 17:18:22 2017
see also : 시간 관련 함수
반응형
'C언어 header > time.h' 카테고리의 다른 글
asctime_r(3) - struct tm 구조체를 날짜 및 시간 표시 문자열로 변환(thread-safe) (0) | 2019.09.30 |
---|---|
asctime(3) - struct tm 구조체를 날짜 및 시간 표시 문자열로 변환 (1) | 2019.09.30 |
ctime(3) - 초단위 시간을 문자열로 변환 (0) | 2019.09.30 |
gmtime_r(3) - 초단위의 시간을 국제표준시(UTC) struct tm으로 변환(thread-safe) (0) | 2019.09.30 |
gmtime(3) - 초단위의 시간을 국제표준시(UTC) struct tm으로 변환 (0) | 2019.09.30 |