현재 시각을 컨솔에 보여주는 간단한 애플리케이션의 C 언어 소스 코드이다.
UTC란 1970년 1월 1일 0시 0분 0초를 기준으로 하여 경과된 초 단위의 총 시간을 의미한다.
* UTC(Universal Time  Coordinated, 협정세계시, 協定世界時)

  1. /*
  2.  *  Filename: testCTime.c
  3.  *
  4.  *  Compile: cl testCTime.c
  5.  *
  6.  *  Execute: testCTime
  7.  */
  8. #include <stdio.h>
  9. #include <time.h>
  10. char weekNames[7][3] = {
  11.  "일", "월", "화", "수", "목", "금", "토"
  12. };
  13. int main() {
  14.     time_t timer;
  15.     struct tm *t;
  16.     timer = time(NULL); // 현재 시각을 초 단위로 얻기
  17.     t = localtime(&timer); // 초 단위의 시간을 분리하여 구조체에 넣기
  18.     // 1970년 1월 1일 0시 0분 0초부터 시작하여 현재까지의 초
  19.     printf("UTC: %d초\n", timer);
  20.     // 현재 시각 표시: 200x년 x월 xx일 (x요일) xx시 xx분 xx초
  21.     printf("%d년 %d월 %d일 (%s요일) ", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, weekNames[t->tm_wday]);
  22.     printf("%d시 %d분 %d초\n", t->tm_hour, t->tm_min, t->tm_sec);
  23.     // t->tm_yday의 값: 1월 1일은 0, 1월 2일은 1
  24.     printf("올해 몇 번째 날: %d, ", 1 + t->tm_yday);
  25.     // t->tm_isdst의 값: 0 이면 서머타임 없음
  26.     printf("서머타임 적용 여부: %s\n", (t->tm_isdst == 0) ? "안함" : "함");
  27.     return 0;
  28. }



컴파일> cl testCTime.c

실행> testCTime
UTC: 1206322402초
2008년 3월 24일 (월요일) 10시 33분 22초
올해 몇 번째 날: 84, 서머타임 적용 여부: 안함



Posted by Scripter
,