시간과 날짜와 관련된 라이브러리 함수

구분

함수 원형과 인자

함수 설명

시간

계산

time_t time(time_t *timeptr);

1970 1 1일 자정부터 경과된 현재 시간을 초단위로 계산

시간을

문자열로

변환

char *asctime(strcut tm *time);

구조체 tm형식의 시간을 문자열로 변환

char *ctime(time_t *time);

함수 time()로부터 계산된 현재 시간을 문자열로 변환

시간을

구조체로

변환

struct tm *localtime(time_t *time);

지역 시간(local time)을 구조체 tm의 형식으로 가져오는 함수

struct tm *gmtime(time_t *time);

Greenwich Meam Time(GMT)을 구조체 tm 형식으로 가져옴

시간

차이

계산

clock_t clock(void);

clock tick으로 경과된 시간

double difftime(time_t time2, time_t time1);

두 시간의 차이를 초단위로 계산

시간

지연

void Sleep(unsigned millisecond);

인자가 지정하는 만큼의 1/1000초 단위의 시간을 지연

void delay(unsigned millisecond);

 

예제

  

#include<stdio.h>

#include<time.h>

 

int main(void)

{

    time_t now;

    time(&now);

    printf("현재날짜와시간: %s", asctime(localtime(&now)));

    printf("현재날짜와시간: %s", ctime(&now));

 

    return 0;

}

 

 

 

strcut tm은 <time.h>에 다음과 같이 정의되어 있다.

 

struct tm {

        int tm_sec;     /* (seconds)       - [0,59] */

        int tm_min;     /* (minutes)       - [0,59] */

        int tm_hour;    /* 시간(hour)        - [0,23] */

        int tm_mday;    /* 날짜(day)         - [1,31] */

        int tm_mon;     /* (month)         - [0,11] */

        int tm_year;    /* 1900년이후의연도수*/

        int tm_wday;    /* dydlf(second)     - [0,6] */

        int tm_yday;    /* 연중날짜(day)     - [0,365] */

        int tm_isdst;   /* 일광절약시간      - [0, 59] */

        };

 

예제

 

#include<stdio.h>

#include<time.h>

int main(void)

{

    time_t curr;

    struct tm *d;

    curr=time(NULL);

    d=localtime(&curr);

    printf("현재날짜\n");

    printf("%d%d%d\n", d->tm_year+1900, d->tm_mon+1, d->tm_mday);

    printf("현재시간\n");

    printf("%d%d%d\n", d->tm_hour, d->tm_min, d->tm_sec);

    return 0;

}

 

 

 

 

time과 clock의 차이

구분

함수 time

함수 clock

원형

time_t time(time_t *timer);

clock_t clock(void);

기능

절대시간 또는 달력시간(calendar time)을 계산

프로세서 시간을 계산

반환 값

1970 1 1일 자정 이후 현재까지 경과된 시간을 초(second)로 반환

프로세서 시간을 반환

 

예제 - 함수 time 사용

  

#include<stdio.h>

#include<time.h>

 

int main(void)

{

    time_t start, end;

    long i=0;

    double pst;

 

    start=time(NULL);

 

    while(i<30000000)

    {

        i++;

    }

 

    end=time(NULL);

    pst=difftime(end, start);

    printf("time: %f\n", pst);

 

    return 0;

}

/**초단위의시간을계산하는함수time()의결과는0초**/

 

예제 - 함수 clock 사용

  

#include<stdio.h>

#include<time.h>

 

int main(void)

{

    time_t start, end;

    long i=0;

    double pst;

 

    start=clock();

 

    while(i<30000000)

    {

        i++;

    }

 

    end=clock();

    pst=(double)(end-start)/CLK_TCK;

    printf("time: %f\n", pst);

 

    return 0;

}

 

 

시간을 지연시키는 함수 Sleep 예제

  

#include<stdio.h>

#include<time.h>

#include<windows.h>    //Sleep()

 

int main(void)

{

    clock_t start, end;

    double pst;

 

    start = clock();

    printf("start!\n");

    Sleep(3500);

    end = clock();

    printf("end\n");

    pst = (double)(end-start)/CLK_TCK;

    printf("경과시간: %f\n", pst);

 

    return 0;

}

 

 

 

시간을 처리하는 함수와는 다른 개념의 함수이지만 유용하게 사용할 수 있는 함수로 kbhit가 있다. 이 함수의 원형은 다음과 같이 함수의 인자가 없으며, 키보드 상의 어떤 키를 누르면 0이 아닌 값을, 누르지 않은 상태라면 0값을 반환하는 함수다. 함수 kbhit는 <conio.h>를 필요로 한다.

kbhit

함수원형

int kbhit(void);

반환 값

입력된 키가 있으면 0이 아닌 값을, 입력된 키가 없으면 0을 반환

 

프로그램 실행중에 아무키나 누르기 전까지만 프로그램을 계속 반복시키고자 한다면 반복문 while과 함께 다음과 같이 사용할 수 있다.

while(!kbhit())

{

    //반복할프로그램

}

 

예제 - 현재 시간을 연속적으로 출력(kbhit, localtime)

 

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#include<conio.h>

int main(void)

{

    time_t now;

    struct tm *d;

 

    while(!kbhit())

    {

        system("cls");

        now=time(NULL);

        d=localtime(&now);

        printf("현재날짜와시간: %s\n", asctime(d));

    }

    return 0;

}

 

 

 

날짜 수와 요일 계산

기준일(1년 1월 1일)로부터 특정일 사이의 날짜 수의 계싼 함수 total_days

long total_days(int year, int month, int day)

{

    int months[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int i;

    long total=0L;

 

    total=(year-1)*365L+(year-1)/4-(year-1)/100+(year-1)/400;

    if(!(year%4) && year%100 || !(ytear%400))

        months[1]++;

    for(i=0;i<month-1;i++)

        total += months[i];

    total += day;

 

    return total;

}

 

특정일의 요일 계산

  

#include<stdio.h>

 

long total_days(int year, int month, int day);

 

int main(void)

{

    int year, month, day;

    char *week_day[] = {"", "", "", "", "", "", ""};

    long total;

 

    printf("특정일의요일구하는프로그램\n\n");

    printf("입력될숫자는space bar로분리하고\n");

    printf("Enter 키를누릅니다.\n");

    printf("예로2005 5 1 Enter\n");

    printf("년월일입력: ");

    scanf("%d %d %d", &year, &month, &day);

 

    total=total_days(year, month, day);

 

    printf("%s 요일입니다.\n", week_day[total%7]);

 

    return 0;

}

 

long total_days(int year, int month, int day)

{

    int months[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int i;

    long total=0L;

 

    total=(year-1)*365L+(year-1)/4-(year-1)/100+(year-1)/400;

    if(!(year%4) && year%100 || !(year%400))

        months[1]++;

    for(i=0;i<month-1;i++)

        total += months[i];

    total += day;

 

    return total;

}

 

 

특정일 사이의 날짜 수를 계산

  

#include<stdio.h>

 

long total_days(int year, int month, int day);

 

int main(void)

{

    long total;

   

    total=total_days(2010, 7, 21) - total_days(1987, 4, 16);

 

    printf("두날짜사이의날짜수: %ld\n", total);

 

    return 0;

}

 

long total_days(int year, int month, int day)

{

    int months[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int i;

    long total=0L;

 

    total=(year-1)*365L+(year-1)/4-(year-1)/100+(year-1)/400;

    if(!(year%4) && year%100 || !(year%400))

        months[1]++;

    for(i=0;i<month-1;i++)

        total += months[i];

    total += day;

 

    return total;

}

 

 

 


'library > CRT' 카테고리의 다른 글

errno  (0) 2011.01.08
유니코드를 위한 함수  (0) 2011.01.08
문자열 함수들의 버퍼 체크 방법 차이  (0) 2010.11.24
코딩시 Unicode 관련 고려사항  (0) 2010.11.23
디버그 시 malloc 메모리 누수 탐지  (0) 2010.07.16
블로그 이미지

란마12

,