관리 메뉴

드럼치는 프로그래머

[C/C++] localtime time_t형 값에서 지역 시간 정보 구하기 본문

★─Programing/☆─C | C++

[C/C++] localtime time_t형 값에서 지역 시간 정보 구하기

드럼치는한동이 2013. 4. 22. 09:50

설명

time_t 값에서 표준시간지역 시간 값을 구하며, 시간 정보는 아래와 같은 struct 값으로 구해집니다.

struct tm
{
  int tm_sec;			/* Seconds.	[0-60] (1 leap second) */
  int tm_min;			/* Minutes.	[0-59] */
  int tm_hour;			/* Hours.	[0-23] */
  int tm_mday;			/* Day.		[1-31] */
  int tm_mon;			/* Month.	[0-11] */
  int tm_year;			/* Year	- 1900.  */
  int tm_wday;			/* Day of week.	[0-6] */
  int tm_yday;			/* Days in year.[0-365]	*/
  int tm_isdst;			/* DST.		[-1/0/1]*/

#ifdef	__USE_BSD
  long int tm_gmtoff;		/* Seconds east of UTC.  */
  __const char *tm_zone;	/* Timezone abbreviation.  */
#else
  long int __tm_gmtoff;		/* Seconds east of UTC.  */
  __const char *__tm_zone;	/* Timezone abbreviation.  */
#endif
};
헤더 time.h
형태 struct tm *localtime(const time_t *t);
인수 time_t *t 시간 time_t 값
반환 struct tm * 시간에 대한 struct tm 값의 포인터
예제
#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 
썸머 타임 사용 안함
]$
[출처] http://forum.falinux.com/zbxe/index.php?document_srl=408349&mid=C_LIB

Comments