C/C Library

asctime

memora 2024. 7. 16. 23:42

Index로 돌아가기

 

 【기  능】 tm구조체를 문자열로 변환
 
 【소  속】 time.h 
   #include <time.h>
 
 【서  식】  
   char *asctime(const struct tm *pTime);
 
 【설  명】  
    tm구조체인   pTime 에 보존되어 있는 시각정보를 문자열로 변환하여 돌려준다.
   문자열 예   : "Mon July 15 07:12:48 2024\n\0"
   의미         : 요일    월   일 시:분:초:  년          \n이 들어있는 점에 주의
 
 【인  수】  
   const struct tm * pTime : tm구조체의 어드레스

  구조체 tm은 time.h 파일 안에 선언되어 있고 다음과 같은 구조를 가진다.
   struct tm {
         int tm_sec;       /* 초 [0-61] 최대 2초까지 윤초를 고려 */
         int tm_min;       /* 분 [0-59] */
         int tm_hour;      /* 시 [0-23] */
         int tm_mday;     /* 일 [1-31] */
         int tm_mon;      /* 월 [0-11] 0부터 시작된다 */
         int tm_year;      /* 년 [1900년 부터의 경과년수] */
         int tm_wday;     /* 요일 [0:일 1:월 ... 6:토] */
         int tm_yday;      /* 년내의 연속번호 [0-365] 0부터 시작한다*/
         int tm_isdst;      /* 서머타임을 사용하지 않을때는 0 */
   };
 
  【리턴 값】
  변환후의 문자열의 선두 어드레스
 
  【사용 예】
  #include <stdio.h>
  #include <time.h>

  int main(void)
  {
       time_t     time_now;
       struct tm *pstrTime;

       /* 현재시간을 취득 */
       time(&time_now);

       /* 현재시간을 구조체로 변환 */
       pstrTime = localtime(&time_now);

       /* tm구조체를 문자열로 변환 */
       printf("%s", asctime(pstrTime));

      return 0;
  }

  【결   과】
    Tue Jul 16 23:33:00 2024


Index로 돌아가기

반응형