C/C++ 콘솔(console) 타임 출력

반응형
728x90
반응형
 
Introduction

 오래된 자료 포스팅 중입니다. 필요하신 분들이 계실지는 모르겠지만, C/C++기반 콘솔에서 시간 출력하는 예제입니다. 
 
 
소스코드

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


int main( void)
{
   char      *week[] = { "일", "월", "화", "수", "목", "금", "토"};
   time_t     current_time;
   struct tm *struct_time;


   time( &current_time);


   struct_time = localtime( &current_time);


   printf( "%4d 년n",       struct_time->tm_year +1900);
   printf( "  %2d 월(0-11)n", struct_time->tm_mon  +1   );
   printf( "  %2d 일(1-31)n", struct_time->tm_mday      );
   printf( "%s요일n"        , week[struct_time->tm_wday]);
   printf( "  %2d 시(0-23)n", struct_time->tm_hour      );
   printf( "  %2d 분(0-59)n", struct_time->tm_min       );
   printf( "  %2d 초(0-59)n", struct_time->tm_sec       );
   printf( "1월 1일 이후의 날짜 수: %3d n", struct_time->tm_yday);


   if      ( 0 <  struct_time->tm_isdst)  printf( "썸머 타임 사용n"     );
   else if ( 0 == struct_time->tm_isdst)  printf( "썸머 타임 사용 안함n");
   else                                   printf( "썸머 타임 사용 불가n");


   return 0;
}
 
 
 
결과

]$ ./a.out
2007 년
   7 월(0-11)
  22 일(1-31)
일요일
  22 시(0-23)
  37 분(0-59)
  20 초(0-59)
1월 1일 이후의 날짜 수: 202
썸머 타임 사용 안함
]$
 
 
Console 에서 milliseconds 출력하기

 
method 1
#include <sys/time.h>


...


timeval tv;
gettimeofday (&tv, NULL);return double (tv.tv_sec) + 0.000001 * tv.tv_usec;
 
method 2
struct timeval detail_time;
gettimeofday(&detail_time,NULL);
printf("%d %d",
detail_time.tv_usec /1000,  /* milliseconds */
detail_time.tv_usec); /* microseconds */
 
(Reference Link 깨짐으로 인해 추가 하지 못하였습니다)
 
 
Reference

 
 
728x90
반응형

댓글

Designed by JB FACTORY