asctime_r() — 시간을 문자 스트링으로 변환(재시작 가능)

형식

#include <time.h>
char *asctime_r(const struct tm *tm, char *buf);

언어 레벨

XPG4

스레드세이프

설명

이 함수는 asctime() 함수의 재시작 가능 버전입니다.

asctime_r() 함수는 tm이 가리키는 구조로 저장된 시간을 문자 스트링으로 변환합니다. gmtime_r(), gmtime64_r(), localtime_r() 또는 localtime64_r()을 호출하여 tm 값을 얻을 수 있습니다.

asctime_r()이 생성하는 스트링 결과는 정확히 26자를 포함하며 형식은 다음과 같습니다.
   "%.3s %.3s%3d %.2d:%.2d:%.2d %d\n"
리턴된 스트링 예는 다음과 같습니다.
   Sat Jul 16 02:03:55 1994\n\0
or
   Sat Jul 16  2:03:55 1994\n\0

asctime_r() 함수는 24 시 시계 형식을 사용합니다. 요일은 Sun, Mon, Tue, Wed, Thu, Fri, Sat로 약어화됩니다. 월은 Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec로 약어화됩니다. 모든 필드는 일정한 너비를 갖습니다. 숫자가 하나뿐인 날짜 앞에는 0 또는 공백이 옵니다. 줄 바꾸기 문자(\n)와 널(null) 문자(\0)는 스트링의 마지막 두 위치를 차지합니다.

시간 및 날짜 함수는 1970년 1월 1일 00:00:00 세계시에서 시작됩니다.

리턴값

asctime_r() 함수는 결과 문자 스트링을 가리키는 포인터를 리턴합니다. 함수가 실패하면 널(NULL)을 리턴합니다.

이 예는 시스템 시계를 폴링하고 현재 시간을 제공하는 메세지를 인쇄합니다.
#include <time.h>
#include <stdio.h>
 
int main(void)
{
    struct tm *newtime;
    time_t ltime;
    char  mybuf[50];
 
/* Get the time in seconds */
    time(&ltime);
/* Convert it to the structure tm */
    newtime = localtime_r(&ltime());
/* Print the local time as a string */
    printf("The current date and time are %s",
             asctime_r(newtime, mybuf));
}
 
/****************  Output should be similar to  ******************
The current date and time are Fri Sep 16 132951 1994
*/