반응형
asctime(3)
#include <time.h>
char *asctime(const struct tm *tm);
struct tm 구조체를 날짜 및 시간 표시 문자열로 변환합니다. 문자열의 format은 "Wed Jan 25 15:52:09 2017\n"형태로 끝에 new line이 붙습니다. 변환된 문자열은 local static변수로 저장되므로 asctime(3)을 다시 실행하면 내용이 변경됩니다. multi-thread에서는 문제가 발생할 수 있으므로 asctime_r(3)을 사용해야 합니다.
파라미터
tm
- struct tm 의 변수로 localtime(3), gmtime(3) 등을 통하여 구조체를 얻을 수 있습니다.
struct tm의 구조는 아래와 같습니다.
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
RETURN
NULL 아님
- "Wed Jan 25 15:52:09 2017\n" 형태의 문자열로 static local 변수를 사용하므로
다음 asctime(3)호출되면 다른 값으로 변경됩니다.
- 특히, multi-thread에서는 문제가 발생할 수 있으므로 asctime_r(3)을 사용할 것을 권고합니다.
NULL
- 오류가 발생하였습니다.
활용 예제
Sample
#include <time.h>
#include <stdio.h>
int main(int argc, char **argv)
{
time_t t;
struct tm *lt;
if((t = time(NULL)) == -1) {
perror("time() call error");
return -1;
}
if((lt = localtime(&t)) == NULL) {
perror("localtime() call error");
return -1;
}
printf("현재시각: %04d-%02d-%02d %02d:%02d:%02d\n",
lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday,
lt->tm_hour, lt->tm_min, lt->tm_sec);
/* new line이 자동으로 붙기 때문에 %s 뒤에 \n가 필요없음 */
printf("asctime() : %s", asctime(lt));
return 0;
}
결과:
현재시각: 2017-01-25 17:05:23
asctime() : Wed Jan 25 17:05:23 2017
see also : 시간 관련 함수
반응형
'C언어 header > time.h' 카테고리의 다른 글
mktime(3) - struct tm를 time_t로 변환 (0) | 2019.09.30 |
---|---|
asctime_r(3) - struct tm 구조체를 날짜 및 시간 표시 문자열로 변환(thread-safe) (0) | 2019.09.30 |
ctime_r(3) - 초단위 시간을 문자열로 변환(thread-safe) (0) | 2019.09.30 |
ctime(3) - 초단위 시간을 문자열로 변환 (0) | 2019.09.30 |
gmtime_r(3) - 초단위의 시간을 국제표준시(UTC) struct tm으로 변환(thread-safe) (0) | 2019.09.30 |