Hi there!

I am a student studying computer science.

시스템 프로그래밍

시스템 프로그래밍 14장 - Time Management

만능성구 2020. 6. 18. 15:13
728x90

Calendar Time & Process Time

Calendar time

• UTC(Coordinated Universal Time) / GMT(Greenwich Mean Time)   국제표준시

– (in English) Coordinated Universal Time,

– (in France) Temps Universel Coordonné

• Epoch time: 1970. 1. 1. 00:00 UTC 이후 초단위 시간

• Data type: time_t

Process/CPU time

• In clock tick unit //cpu에서 시간 기준 1tick

• 1 tick = 1 ms (Linux) or 10 ms

– 1 ms = 1/1000 sec

• time(1): 어떤 program을 실행했을 때 걸리는 시간

– $ time ./a.out

real 0m0.002s

user 0m0.000s

sys 0m0.000s


Time 관련된 system function들의 상관관계 도식화

system에서는 UTC를 기준으로 관리한다.

time() : user level에서는 time정보를 tim_t type으로 UTC, epoch time을 얻어온다

stime() : 자기가 원하는 시간으로 설정해서 가져온다. UTC epoch time을 계산해서 가져온다.

 

epoch time은 초로 사용되기 때문에 표현을 쉽게 calender 형식의 time으로 표현하는 interface를 가지고 있다.

tm : calendr time을 표현하는 data 구조체

 

data type 변환 function (tm<->time_t) 

gmtime

gmtime_r

localtime

localtime_r

mktime : calender time -> epoch time

 

tm, tiem_t type을 string type으로 변환 function

- tm 

asctime

asctime_r 

strftime

- tiem_t

ctime

ctime_r

 

#include <time.h>
time_t time (time_t *tloc);
kenel의 UTC time을 알아낼 수 있는 함수
tloc : buffer for time_t (epoch time)  -> 여기로도 반환 받는다.
return 현재 epoch time / -1
#include <time.h>
int stime (const time_t *tp); 
본인 설정하고 싶은 시간을 설정한 다음 그 변수를  system에 설정
tp : the epoch time to set 
return 0 / -1

setTime.c

+/-를 입력받고 정수형 숫자를 받아서

현재 시간 + 입력 숫자 연산.

atoi


tm 구조체

struct tm {
  int tm_sec; /* seconds (0~59) */ 초
  int tm_min; /* minutes (0~59) */ 분
  int tm_hour; /* hours (0~23) */ 시
  int tm_mday; /* day of the month (1~31) */ 일
  int tm_mon; /* month from 0 to 11 : 0 for Jan. */ 월
  int tm_year; /* year from 1900 */ 년
  int tm_wday; /* day of the week (0~6): 0 for Sunday */ 요일
  int tm_yday; /* day of the year (0~365) */ 일년중 몇번째 day
  int tm_isdst; /* daylight savings time */ summer time saving time?
};


#include <time.h>
struct tm *localtime (const time_t *clock); // Local time
epoch을 tm으로 변화, 지역마다 달라짐 현지시간!
clock : epoch time
return pointer to tm
#include <time.h>
struct tm *gmtime (const time_t *clock); // Greenwich mean time 
epoch을 tm으로 변화, 그리니치 time으로 바꿔줌
clock : epoch time 
return pointer to tm

timeF.c


#include <time.h>
time_t mktime (struct tm *timeptr);
tm을 epoch으로 변환
timeptr : pointer to tm structure
return epoch time / -1

toUTC.c


#include <time.h>
char *ctime (const time_t *clock);
epoch을 출력형태로 표현하는 함수
clock : epoch time
Return : time string
char *asctime( const struct tm *tm); 
calender을 출력형태로 표현하는 함수
tm : pointer to tm structure  
Return : time string

timeToStr.c


#include <time.h>
size_t strftime (char *s, size_t maxsize, const char *format, const struct tm *timeptr)
시간을 표준 데이터 형식을 원하는 형태의 string format으로 변환
s : string저장 할 buffer
maxsize : buffer size
format : 변환할 형식
timeptr : 변환할 값 tm
return # of characters stored in “s” / 0

Formats for strftime()

변환기호 설명  예제
%% '%'문자를 출력하는데 사용 %
%a 축약된 요일명 Sun
%A 축약하지 않은 요일명 Sunday
%b 축약된 월명 Jan
%B 축약하지 않은 월명 January
%c 날짜와 시간 Sun Jan 09 18 : 33 : 19 1994
%C date(1)명령이 만들어내는 날짜와 시간 Sun Jan 09 18 : 33 : 19 KST 1994
%d 일(01 에서 31) 09
%D %m/%d/%y 형태의 날짜 01/09/94
%e 일(1에서 31로서 필요한 숫자만 표시) 9
%h 축약된 월명 Jan
%H 시간(00에서 23) 18
%I(아이) 시간(01에서 12) 06
%j 한해의 날수(001에서 366) 009
%k 시간(0에서 23) 18
%l 시간(1에서 12) 06
%m 월 번호(01에서 12) 009
%M 분(00에서 59) 33
%n '\n'과 동일 새로운 행  
%p AM이나 PM중 하나와 동일 PM
%r %I : %M : %S [AM|PM]으로 나타낸 시간 06 : 33 :19 PM
%R %H : %M으로 나타낸 시간 18 : 33
%S 윤초를 지원하는 초(00에서 61) 19
%t 탭문자와 동일
%T %H : %M : %S로 나타낸 시간 18 : 33 : 19
%U 한해의 주 번호(00에서 53), 일요일로 시작하는 주만 세어서 나타냄 02
%w 요일 번호(0에서 6까지로 일요일은 0) 0
%W 한해의 주번호(00에서 53) 월요일로 시작하는 주만 세어서 나타냄 01
%x 날짜 표시 01/09/94
%X 시간 표시 18:33:!9
%y 그 세기의 해표시(00에서 99) 94
%Y 년도 표시 1994
%Z Time zone 이름 KST
728x90