티스토리 뷰
시간 관련 라이브러리
헤퍼 파일 : time.h
시간 계산
- clock : 시간 계산 함수
- difftime : 두 시간 사이의 차이 계산
- mktime : strcut tm 구조체를 통해 epoch time을 구해내는 함수
- time : 현재 시간
시간 변환
- asctime : 시간구조체를 문자로 변환
- ctime : 시간변수를 문자로 변환
- gmtime : UTC 시간으로 변환
- localtime : 지역 시간으로 변환
- strftime : 날짜와 시간으로 이루어진 문자열을 strct tm으로 변환
라이브러리 변수
변수 | 설명 |
size_t | 부호 없는 정수형 |
clock_t | 프로세서 시간 저장 변수 타입 |
time_t | 캘린더 시간 저장 변수 타입 |
struct tm | 날짜 , 시간 처리 구조체 |
tm 구조체
struct tm {
int tm_sec ; /* seconds, range 0 to 59 */
int tm_min ; /* minutes, range 0 to 59 */
int tm_hour ; /* hours, range 0 to 23 */
int tm_mday ; /* day of the month, range 1 to 31 */
int tm_mon ; /* month, range 0 to 11 */
int tm_year ; /* The number of years since 1900 */
int tm_wday ; /* day of the week, range 0 to 6 */
int tm_yday ; /* day in the year, range 0 to 365 */
int tm_isdst ; /* daylight saving time */
};
관련 용어
* Epoch Time : 1970년 01월 01일 00시 00분 00초를 기점으로 흐르는 시간
* UTC 타입 (Coordinated Universal Time) : 영국 그리니치 천문대 (경도 0) 를 기준으로 하는 세계의 표준 시간대
* Greenwich Mean Time, GMT : 영국 런던을 기점 , 뉴질랜드 웰링턴을 종점으로 하는 협정 세계시
clock
항목 | 내용 |
함수원형 | clock_t clock(void); |
헤더 | time.h |
기능 | 프로그램 실행 후의 시간을 계산 |
매개변수 | void |
반환값 | 프로그램 실행 후 경과된 시간을 반환 |
#include
#include
#include
#define setClock () (clock() + 500000)
int main(void) {
clock_t clk_start = setClock();
while ( 1 ) {
if (clock() >clk_start ) {
clk_start = setClock();
printf("%ld..\n”, clk_start);
}
}
return 0;
}
time
항목 | 내용 |
함수원형 | time_t time( time_t *t); |
헤더 | time.h |
기능 | 시스템의 시간 |
매개변수 | time_t*t -> 시간 정보를 받을 변수 |
반환값 | time_t -> 1970 년 1 월 1 일 0 시부터 함수를 호출할때까지의 초 |
difftime
항목 | 내용 |
함수원형 | 시간의 차이를 계산 |
헤더 | time.h |
기능 | double difftime(time_t time1, time_t time0); |
매개변수 | time_t time1 -> 시간 계산에서 빼지는 시간 (after) time_t time0 -> 시간 계산에서 빼는 시간 (before) |
반환값 | double -> 두 시간의 차이 |
#include
#include
int main ()
{
time_t start,end;
char szInput [256];
double dif;
time (&start);
printf(“Please, enter your name:")
gets (szInput);
time (&end);
dif = difftime end,start);
printf(“Hi %s.\n”, szInput);
printf(“It took you %.2lf seconds to type your \n”, dif);
return 0;
}
ctime
항목 | 내용 |
함수원형 | char *ctime(const time_t *t); |
헤더 | time.h |
기능 | time_t 시간 값을 일반적인 시간 문자열로 변환 |
매개변수 | time_t*t -> 1970 년 1 월 1 일 0 시부터 함수를 호출할 때 까지의 초 |
반환값 | char * -> 초 단위의 시간을 읽기 편한 문자열로 변환한 문자열 포인터 |
#include
#include
int main ()
{
time_t current_time;
time( ¤t_time);
printf( ldn ”, current_time );
printf( ctime ( current_time ));
return 0;
}
strftime
항목 | 내용 |
함수원형 | size_t strftime (char *restrict s, size_t maxsize , const char *restrict format, const struct tm *restrict timeptr); |
헤더 | time.h |
기능 | struct tm 값으로 포맷에 맞춘 시간 문자열 |
매개변수 | char *restrict s -> 문자열을 받을 버퍼 포인터 size_t maxsize ->버퍼의 크기 const char *restrict format -> 날짜와 시간 정보를 나열하 기 위한 문자열 포맷 const struct tm *restrict timeptr -> 날짜와 시간 정 보 |
반환값 | size_t ->문자열의 길이 |
#include
#include
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time (&rawtime);
timeinfo = localtime rawtime);
strftime (buffer,80,“Now it's %I:% M%p timeinfo);
puts (buffer);
return 0;
}
localtime
항목 | 내용 |
함수원형 | struct tm * localtime (const time_t *t); |
헤더 | time.h |
기능 | time_t 값에서 표준 시간 지역의 시간 값을 구하며, 시간정보는 아래와 같은 struct 값 |
매개변수 | time_t *t -> 시간 time_t 값 |
반환값 | struct tim * -> 시간에 대한 strcut tim 값의 포인터 |
asctime
항목 | 내용 |
함수원형 | char * asctime (const struct tm * timeptr ); |
헤더 | time.h |
기능 | tm 구조체를 문자열로 변환 |
매개변수 | timeptr -> tm구조체 포인터 |
반환값 | Www Mmm dd hh:mm:ss yyyy |
- 표준 출력 문장으로 많이 사용되는 함수 : printf ();
- 두 시간 사이의 차이를 구하는 함수 : difftime
- 현재 시스템의 시간을 구할 수 있는 함수 : time
- 초 단위 시간을 시간문자열로 변환하는 함수 : ctime
- 지역 시간을 구하는 함수 : localtime
- tm 구조체를 문자열로 변환하는 함수 : asctime
'JAVA기반 스마트웹 개발2021 > 프로그래밍 언어활용' 카테고리의 다른 글
변화·랜덤 라이브러리 (0) | 2021.08.08 |
---|---|
주소록 관리 시스템 (0) | 2021.08.08 |
도서관리 시스템 고도화(파일 처리) (0) | 2021.08.08 |
파일 입출력 라이브러리(응용) (0) | 2021.08.08 |
파일 입출력 라이브러리(기초) (0) | 2021.08.08 |
댓글
© 2018 webstoryboy